1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
7
8 #include "config.h"
9
10 #ifndef lint
11 static const char sccsid[] = "@(#)os_dir.c 10.19 (Sleepycat) 10/12/98";
12 #endif /* not lint */
13
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
16
17 #if HAVE_DIRENT_H
18 # include <dirent.h>
19 # define NAMLEN(dirent) strlen((dirent)->d_name)
20 #else
21 # define dirent direct
22 # define NAMLEN(dirent) (dirent)->d_namlen
23 # if HAVE_SYS_NDIR_H
24 # include <sys/ndir.h>
25 # endif
26 # if HAVE_SYS_DIR_H
27 # include <sys/dir.h>
28 # endif
29 # if HAVE_NDIR_H
30 # include <ndir.h>
31 # endif
32 #endif
33
34 #include <errno.h>
35 #endif
36
37 #include "db_int.h"
38 #include "os_jump.h"
39
40 /*
41 * __os_dirlist --
42 * Return a list of the files in a directory.
43 *
44 * PUBLIC: int __os_dirlist __P((const char *, char ***, int *));
45 */
46 int
__os_dirlist(dir,namesp,cntp)47 __os_dirlist(dir, namesp, cntp)
48 const char *dir;
49 char ***namesp;
50 int *cntp;
51 {
52 struct dirent *dp;
53 DIR *dirp;
54 int arraysz, cnt, ret;
55 char **names;
56
57 if (__db_jump.j_dirlist != NULL)
58 return (__db_jump.j_dirlist(dir, namesp, cntp));
59
60 if ((dirp = opendir(dir)) == NULL)
61 return (errno);
62 names = NULL;
63 for (arraysz = cnt = 0; (dp = readdir(dirp)) != NULL; ++cnt) {
64 if (cnt >= arraysz) {
65 arraysz += 100;
66 if ((ret = __os_realloc(&names,
67 arraysz * sizeof(names[0]))) != 0)
68 goto nomem;
69 }
70 if ((ret = __os_strdup(dp->d_name, &names[cnt])) != 0)
71 goto nomem;
72 }
73 (void)closedir(dirp);
74
75 *namesp = names;
76 *cntp = cnt;
77 return (0);
78
79 nomem: if (names != NULL)
80 __os_dirfree(names, cnt);
81 return (ret);
82 }
83
84 /*
85 * __os_dirfree --
86 * Free the list of files.
87 *
88 * PUBLIC: void __os_dirfree __P((char **, int));
89 */
90 void
__os_dirfree(names,cnt)91 __os_dirfree(names, cnt)
92 char **names;
93 int cnt;
94 {
95 if (__db_jump.j_dirfree != NULL)
96 __db_jump.j_dirfree(names, cnt);
97
98 while (cnt > 0)
99 __os_free(names[--cnt], 0);
100 __os_free(names, 0);
101 }
102