Update.
[glibc.git] / db2 / os / db_os_dir.c
blob1206e3faa74989aea9d51382a633d3430dd4cd87
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1997
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)db_os_dir.c 10.10 (Sleepycat) 9/17/97";
12 #endif /* not lint */
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
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
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #endif
40 #include "db_int.h"
41 #include "os_ext.h"
42 #include "common_ext.h"
45 * __db_dir --
46 * Return a list of the files in a directory.
48 * PUBLIC: int __db_dir __P((DB_ENV *, const char *, char ***, int *));
50 int
51 __db_dir(dbenv, dir, namesp, cntp)
52 DB_ENV *dbenv;
53 const char *dir;
54 char ***namesp;
55 int *cntp;
57 int arraysz, cnt;
58 char **names;
59 #ifdef _WIN32
60 struct _finddata_t fdata;
61 long dirhandle;
62 int finished;
63 char filespec[MAX_PATH];
65 (void)snprintf(filespec, sizeof(filespec), "%s/*", dir);
66 if ((dirhandle = _findfirst(filespec, &fdata)) == -1) {
67 __db_err(dbenv, "%s: %s", filespec, strerror(errno));
68 return (errno);
71 names = NULL;
72 finished = 0;
73 for (arraysz = cnt = 0; finished != 1; ++cnt) {
74 if (cnt >= arraysz) {
75 arraysz += 100;
76 names = (char **)(names == NULL ?
77 malloc(arraysz * sizeof(names[0])) :
78 realloc(names, arraysz * sizeof(names[0])));
79 if (names == NULL)
80 goto nomem;
82 if ((names[cnt] = (char *)strdup(fdata.name)) == NULL)
83 goto nomem;
84 if (_findnext(dirhandle,&fdata) != 0)
85 finished = 1;
87 _findclose(dirhandle);
88 #else /* !_WIN32 */
89 struct dirent *dp;
90 DIR *dirp;
92 if ((dirp = opendir(dir)) == NULL) {
93 __db_err(dbenv, "%s: %s", dir, strerror(errno));
94 return (errno);
96 names = NULL;
97 for (arraysz = cnt = 0; (dp = readdir(dirp)) != NULL; ++cnt) {
98 if (cnt >= arraysz) {
99 arraysz += 100;
100 names = (char **)(names == NULL ?
101 malloc(arraysz * sizeof(names[0])) :
102 realloc(names, arraysz * sizeof(names[0])));
103 if (names == NULL)
104 goto nomem;
106 if ((names[cnt] = (char *)strdup(dp->d_name)) == NULL)
107 goto nomem;
109 (void)closedir(dirp);
110 #endif /* !_WIN32 */
112 *namesp = names;
113 *cntp = cnt;
114 return (0);
116 nomem: if (names != NULL)
117 __db_dirf(dbenv, names, cnt);
118 __db_err(dbenv, "%s", strerror(ENOMEM));
119 return (ENOMEM);
123 * __db_dirf --
124 * Free the list of files.
126 * PUBLIC: void __db_dirf __P((DB_ENV *, char **, int));
128 void
129 __db_dirf(dbenv, names, cnt)
130 DB_ENV *dbenv;
131 char **names;
132 int cnt;
134 dbenv = dbenv; /* XXX: Shut the compiler up. */
135 while (cnt > 0)
136 free(names[--cnt]);
137 free (names);