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