xref: /freebsd/share/examples/sunrpc/dir/dir_proc.c (revision 97759ccc715c4b365432c16d763c50eecfcb1100)
1944fcc15SGarrett Wollman /*
2944fcc15SGarrett Wollman  * dir_proc.c: remote readdir implementation
3944fcc15SGarrett Wollman  */
4944fcc15SGarrett Wollman #include <rpc/rpc.h>
5944fcc15SGarrett Wollman #include <sys/dir.h>
6944fcc15SGarrett Wollman #include "dir.h"
7944fcc15SGarrett Wollman 
8944fcc15SGarrett Wollman extern int errno;
9944fcc15SGarrett Wollman extern char *malloc();
10944fcc15SGarrett Wollman extern char *strcpy();
11944fcc15SGarrett Wollman 
12944fcc15SGarrett Wollman readdir_res *
readdir_1(dirname)13944fcc15SGarrett Wollman readdir_1(dirname)
14944fcc15SGarrett Wollman 	nametype *dirname;
15944fcc15SGarrett Wollman {
16944fcc15SGarrett Wollman 	DIR *dirp;
17944fcc15SGarrett Wollman 	struct direct *d;
18944fcc15SGarrett Wollman 	namelist nl;
19944fcc15SGarrett Wollman 	namelist *nlp;
20944fcc15SGarrett Wollman 	static readdir_res res; /* must be static! */
21944fcc15SGarrett Wollman 
22944fcc15SGarrett Wollman 	/*
23944fcc15SGarrett Wollman 	 * Open directory
24944fcc15SGarrett Wollman 	 */
25944fcc15SGarrett Wollman 	dirp = opendir(*dirname);
26944fcc15SGarrett Wollman 	if (dirp == NULL) {
27944fcc15SGarrett Wollman 		res.errno = errno;
28944fcc15SGarrett Wollman 		return (&res);
29944fcc15SGarrett Wollman 	}
30944fcc15SGarrett Wollman 
31944fcc15SGarrett Wollman 	/*
32944fcc15SGarrett Wollman 	 * Free previous result
33944fcc15SGarrett Wollman 	 */
34944fcc15SGarrett Wollman 	xdr_free(xdr_readdir_res, &res);
35944fcc15SGarrett Wollman 
36944fcc15SGarrett Wollman 	/*
37944fcc15SGarrett Wollman 	 * Collect directory entries
38944fcc15SGarrett Wollman 	 */
39944fcc15SGarrett Wollman 	nlp = &res.readdir_res_u.list;
40944fcc15SGarrett Wollman 	while (d = readdir(dirp)) {
41944fcc15SGarrett Wollman 		nl = *nlp = (namenode *) malloc(sizeof(namenode));
42944fcc15SGarrett Wollman 		nl->name = malloc(strlen(d->d_name)+1);
43944fcc15SGarrett Wollman 		strcpy(nl->name, d->d_name);
44944fcc15SGarrett Wollman 		nlp = &nl->next;
45944fcc15SGarrett Wollman 	}
46944fcc15SGarrett Wollman 	*nlp = NULL;
47944fcc15SGarrett Wollman 
48944fcc15SGarrett Wollman 	/*
49944fcc15SGarrett Wollman 	 * Return the result
50944fcc15SGarrett Wollman 	 */
51944fcc15SGarrett Wollman 	res.errno = 0;
52944fcc15SGarrett Wollman 	closedir(dirp);
53944fcc15SGarrett Wollman 	return (&res);
54944fcc15SGarrett Wollman }
55