xref: /freebsd/share/examples/sunrpc/dir/rls.c (revision e0c4386e)
1 /*
2  * rls.c: Remote directory listing client
3  */
4 #include <stdio.h>
5 #include <rpc/rpc.h>		/* always need this */
6 #include "dir.h"		/* need this too: will be generated by rpcgen*/
7 
8 extern int errno;
9 
10 main(argc, argv)
11 	int argc;
12 	char *argv[];
13 {
14 	CLIENT *cl;
15 	char *server;
16 	char *dir;
17 	readdir_res *result;
18 	namelist nl;
19 
20 
21 	if (argc != 3) {
22 		fprintf(stderr, "usage: %s host directory\n", argv[0]);
23 		exit(1);
24 	}
25 
26 	/*
27 	 * Remember what our command line arguments refer to
28 	 */
29 	server = argv[1];
30 	dir = argv[2];
31 
32 	/*
33 	 * Create client "handle" used for calling DIRPROG on the
34 	 * server designated on the command line. We tell the rpc package
35 	 * to use the "tcp" protocol when contacting the server.
36 	 */
37 	cl = clnt_create(server, DIRPROG, DIRVERS, "tcp");
38 	if (cl == NULL) {
39 		/*
40 		 * Couldn't establish connection with server.
41 		 * Print error message and die.
42 		 */
43 		clnt_pcreateerror(server);
44 		exit(1);
45 	}
46 
47 	/*
48 	 * Call the remote procedure "readdir" on the server
49 	 */
50 	result = readdir_1(&dir, cl);
51 	if (result == NULL) {
52 		/*
53 		 * An error occurred while calling the server.
54 	 	 * Print error message and die.
55 		 */
56 		clnt_perror(cl, server);
57 		exit(1);
58 	}
59 
60 	/*
61 	 * Okay, we successfully called the remote procedure.
62 	 */
63 	if (result->errno != 0) {
64 		/*
65 		 * A remote system error occurred.
66 		 * Print error message and die.
67 		 */
68 		errno = result->errno;
69 		perror(dir);
70 		exit(1);
71 	}
72 
73 	/*
74 	 * Successfuly got a directory listing.
75 	 * Print it out.
76 	 */
77 	for (nl = result->readdir_res_u.list; nl != NULL; nl = nl->next) {
78 		printf("%s\n", nl->name);
79 	}
80 }
81