Update.
[glibc.git] / db2 / os / os_dir.c
blob14a10ad23f7379d5154a8cd4d12f2f8ceb5837d8
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)os_dir.c 10.15 (Sleepycat) 4/26/98";
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 #endif
37 #include "db_int.h"
40 * __os_dirlist --
41 * Return a list of the files in a directory.
43 * PUBLIC: int __os_dirlist __P((const char *, char ***, int *));
45 int
46 __os_dirlist(dir, namesp, cntp)
47 const char *dir;
48 char ***namesp;
49 int *cntp;
51 struct dirent *dp;
52 DIR *dirp;
53 int arraysz, cnt;
54 char **names;
56 if ((dirp = opendir(dir)) == NULL)
57 return (errno);
58 names = NULL;
59 for (arraysz = cnt = 0; (dp = readdir(dirp)) != NULL; ++cnt) {
60 if (cnt >= arraysz) {
61 arraysz += 100;
62 names = (char **)(names == NULL ?
63 __db_malloc(arraysz * sizeof(names[0])) :
64 __db_realloc(names, arraysz * sizeof(names[0])));
65 if (names == NULL)
66 goto nomem;
68 if ((names[cnt] = (char *)__db_strdup(dp->d_name)) == NULL)
69 goto nomem;
71 (void)closedir(dirp);
73 *namesp = names;
74 *cntp = cnt;
75 return (0);
77 nomem: if (names != NULL)
78 __os_dirfree(names, cnt);
79 return (ENOMEM);
83 * __os_dirfree --
84 * Free the list of files.
86 * PUBLIC: void __os_dirfree __P((char **, int));
88 void
89 __os_dirfree(names, cnt)
90 char **names;
91 int cnt;
93 while (cnt > 0)
94 __db_free(names[--cnt]);
95 __db_free(names);