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