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