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