summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/db/os_dir.c
blob: 812256f1f2ca3089c1b3721980041166a096c476 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997, 1998, 1999
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
static const char sccsid[] = "@(#)os_dir.c	11.1 (Sleepycat) 7/25/99";
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#if HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# if HAVE_SYS_NDIR_H
#  include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
#  include <sys/dir.h>
# endif
# if HAVE_NDIR_H
#  include <ndir.h>
# endif
#endif

#endif

#ifdef _MSC_VER /* _WIN32 */
#include "dirent_local.h"
#endif

#include "db_int.h"
#include "os_jump.h"

/*
 * CDB___os_dirlist --
 *	Return a list of the files in a directory.
 *
 * PUBLIC: int CDB___os_dirlist __P((const char *, char ***, int *));
 */
int
CDB___os_dirlist(dir, namesp, cntp)
	const char *dir;
	char ***namesp;
	int *cntp;
{
	struct dirent *dp;
	DIR *dirp;
	int arraysz, cnt, ret;
	char **names;

	if (CDB___db_jump.j_dirlist != NULL)
		return (CDB___db_jump.j_dirlist(dir, namesp, cntp));

	if ((dirp = opendir(dir)) == NULL)
		return (CDB___os_get_errno());
	names = NULL;
	for (arraysz = cnt = 0; (dp = readdir(dirp)) != NULL; ++cnt) {
		if (cnt >= arraysz) {
			arraysz += 100;
			if ((ret = CDB___os_realloc(
			    arraysz * sizeof(names[0]), NULL, &names)) != 0)
				goto nomem;
		}
		if ((ret = CDB___os_strdup(dp->d_name, &names[cnt])) != 0)
			goto nomem;
	}
	(void)closedir(dirp);

	*namesp = names;
	*cntp = cnt;
	return (0);

nomem:	if (names != NULL)
		CDB___os_dirfree(names, cnt);
	return (ret);
}

/*
 * CDB___os_dirfree --
 *	Free the list of files.
 *
 * PUBLIC: void CDB___os_dirfree __P((char **, int));
 */
void
CDB___os_dirfree(names, cnt)
	char **names;
	int cnt;
{
	if (CDB___db_jump.j_dirfree != NULL)
		CDB___db_jump.j_dirfree(names, cnt);

	while (cnt > 0)
		CDB___os_free(names[--cnt], 0);
	CDB___os_free(names, 0);
}