xref: /freebsd/share/examples/sunrpc/dir/dir_proc.c (revision 3494f7c0)
1 /*
2  * dir_proc.c: remote readdir implementation
3  */
4 #include <rpc/rpc.h>
5 #include <sys/dir.h>
6 #include "dir.h"
7 
8 extern int errno;
9 extern char *malloc();
10 extern char *strcpy();
11 
12 readdir_res *
13 readdir_1(dirname)
14 	nametype *dirname;
15 {
16 	DIR *dirp;
17 	struct direct *d;
18 	namelist nl;
19 	namelist *nlp;
20 	static readdir_res res; /* must be static! */
21 
22 	/*
23 	 * Open directory
24 	 */
25 	dirp = opendir(*dirname);
26 	if (dirp == NULL) {
27 		res.errno = errno;
28 		return (&res);
29 	}
30 
31 	/*
32 	 * Free previous result
33 	 */
34 	xdr_free(xdr_readdir_res, &res);
35 
36 	/*
37 	 * Collect directory entries
38 	 */
39 	nlp = &res.readdir_res_u.list;
40 	while (d = readdir(dirp)) {
41 		nl = *nlp = (namenode *) malloc(sizeof(namenode));
42 		nl->name = malloc(strlen(d->d_name)+1);
43 		strcpy(nl->name, d->d_name);
44 		nlp = &nl->next;
45 	}
46 	*nlp = NULL;
47 
48 	/*
49 	 * Return the result
50 	 */
51 	res.errno = 0;
52 	closedir(dirp);
53 	return (&res);
54 }
55