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