]> sourceware.org Git - glibc.git/blob - db2/os/db_os_dir.c
Update.
[glibc.git] / db2 / os / db_os_dir.c
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997
5 * Sleepycat Software. All rights reserved.
6 */
7
8 #include "config.h"
9
10 #ifndef lint
11 static const char sccsid[] = "@(#)db_os_dir.c 10.7 (Sleepycat) 8/23/97";
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 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #endif
39
40 #include "db_int.h"
41 #include "os_ext.h"
42 #include "common_ext.h"
43
44 /*
45 * __db_dir --
46 * Return a list of the files in a directory.
47 *
48 * PUBLIC: int __db_dir __P((DB_ENV *, char *, char ***, int *));
49 */
50 int
51 __db_dir(dbenv, dir, namesp, cntp)
52 DB_ENV *dbenv;
53 const char *dir;
54 char ***namesp;
55 int *cntp;
56 {
57 int arraysz, cnt;
58 char **names;
59 #ifdef _WIN32
60 struct _finddata_t fdata;
61 long dirhandle;
62 int finished;
63
64 if ((dirhandle = _findfirst(dir,&fdata)) == -1) {
65 __db_err(dbenv, "%s: %s", dir, strerror(errno));
66 return (errno);
67 }
68
69 names = NULL;
70 finished = 0;
71 for (arraysz = cnt = 0; finished != 1; ++cnt) {
72 if (cnt >= arraysz) {
73 arraysz += 100;
74 names = (char **)(names == NULL ?
75 malloc(arraysz * sizeof(names[0])) :
76 realloc(names, arraysz * sizeof(names[0])));
77 if (names == NULL)
78 goto nomem;
79 }
80 if ((names[cnt] = (char *)strdup(fdata.name)) == NULL)
81 goto nomem;
82 if (_findnext(dirhandle,&fdata) != 0)
83 finished = 1;
84 }
85 _findclose(dirhandle);
86 #else /* !_WIN32 */
87 struct dirent *dp;
88 DIR *dirp;
89
90 if ((dirp = opendir(dir)) == NULL) {
91 __db_err(dbenv, "%s: %s", dir, strerror(errno));
92 return (errno);
93 }
94 names = NULL;
95 for (arraysz = cnt = 0; (dp = readdir(dirp)) != NULL; ++cnt) {
96 if (cnt >= arraysz) {
97 arraysz += 100;
98 names = (char **)(names == NULL ?
99 malloc(arraysz * sizeof(names[0])) :
100 realloc(names, arraysz * sizeof(names[0])));
101 if (names == NULL)
102 goto nomem;
103 }
104 if ((names[cnt] = (char *)strdup(dp->d_name)) == NULL)
105 goto nomem;
106 }
107 (void)closedir(dirp);
108 #endif /* !_WIN32 */
109
110 *namesp = names;
111 *cntp = cnt;
112 return (0);
113
114 nomem: if (names != NULL)
115 __db_dirf(dbenv, names, cnt);
116 __db_err(dbenv, "%s", strerror(ENOMEM));
117 return (ENOMEM);
118 }
119
120 /*
121 * __db_dirf --
122 * Free the list of files.
123 *
124 * PUBLIC: void __db_dirf __P((DB_ENV *, char **, int));
125 */
126 void
127 __db_dirf(dbenv, names, cnt)
128 DB_ENV *dbenv;
129 char **names;
130 int cnt;
131 {
132 dbenv = dbenv; /* XXX: Shut the compiler up. */
133 while (cnt > 0)
134 free(names[--cnt]);
135 free (names);
136 }
This page took 0.038446 seconds and 5 git commands to generate.