2 * See the file LICENSE for redistribution information.
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid
[] = "@(#)db_os_dir.c 10.10 (Sleepycat) 9/17/97";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
19 # define NAMLEN(dirent) strlen((dirent)->d_name)
21 # define dirent direct
22 # define NAMLEN(dirent) (dirent)->d_namlen
24 # include <sys/ndir.h>
42 #include "common_ext.h"
46 * Return a list of the files in a directory.
48 * PUBLIC: int __db_dir __P((DB_ENV *, const char *, char ***, int *));
51 __db_dir(dbenv
, dir
, namesp
, cntp
)
60 struct _finddata_t fdata
;
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
));
73 for (arraysz
= cnt
= 0; finished
!= 1; ++cnt
) {
76 names
= (char **)(names
== NULL
?
77 malloc(arraysz
* sizeof(names
[0])) :
78 realloc(names
, arraysz
* sizeof(names
[0])));
82 if ((names
[cnt
] = (char *)strdup(fdata
.name
)) == NULL
)
84 if (_findnext(dirhandle
,&fdata
) != 0)
87 _findclose(dirhandle
);
92 if ((dirp
= opendir(dir
)) == NULL
) {
93 __db_err(dbenv
, "%s: %s", dir
, strerror(errno
));
97 for (arraysz
= cnt
= 0; (dp
= readdir(dirp
)) != NULL
; ++cnt
) {
100 names
= (char **)(names
== NULL
?
101 malloc(arraysz
* sizeof(names
[0])) :
102 realloc(names
, arraysz
* sizeof(names
[0])));
106 if ((names
[cnt
] = (char *)strdup(dp
->d_name
)) == NULL
)
109 (void)closedir(dirp
);
116 nomem
: if (names
!= NULL
)
117 __db_dirf(dbenv
, names
, cnt
);
118 __db_err(dbenv
, "%s", strerror(ENOMEM
));
124 * Free the list of files.
126 * PUBLIC: void __db_dirf __P((DB_ENV *, char **, int));
129 __db_dirf(dbenv
, names
, cnt
)
134 dbenv
= dbenv
; /* XXX: Shut the compiler up. */