aboutsummaryrefslogtreecommitdiff
path: root/share/examples/sunrpc/dir
diff options
context:
space:
mode:
Diffstat (limited to 'share/examples/sunrpc/dir')
-rw-r--r--share/examples/sunrpc/dir/Makefile26
-rw-r--r--share/examples/sunrpc/dir/dir.x37
-rw-r--r--share/examples/sunrpc/dir/dir_proc.c55
-rw-r--r--share/examples/sunrpc/dir/rls.c81
4 files changed, 199 insertions, 0 deletions
diff --git a/share/examples/sunrpc/dir/Makefile b/share/examples/sunrpc/dir/Makefile
new file mode 100644
index 000000000000..592c9d6861e9
--- /dev/null
+++ b/share/examples/sunrpc/dir/Makefile
@@ -0,0 +1,26 @@
+#
+# @(#)Makefile 2.1 88/08/02 4.0 RPCSRC
+#
+BIN = dir_svc rls
+GEN = dir_clnt.c dir_svc.c dir_xdr.c dir.h
+LIB = -lrpclib
+RPCCOM = rpcgen
+
+all: $(BIN)
+
+$(GEN): dir.x
+ $(RPCCOM) dir.x
+
+dir_svc: dir_proc.o dir_svc.o dir_xdr.o
+ $(CC) -o $@ dir_proc.o dir_svc.o dir_xdr.o $(LIB)
+
+rls: rls.o dir_clnt.o dir_xdr.o
+ $(CC) -o $@ rls.o dir_clnt.o dir_xdr.o $(LIB)
+
+rls.o: rls.c dir.h
+
+dir_proc.o: dir_proc.c dir.h
+
+clean cleanup:
+ rm -f $(GEN) *.o $(BIN)
+
diff --git a/share/examples/sunrpc/dir/dir.x b/share/examples/sunrpc/dir/dir.x
new file mode 100644
index 000000000000..db4283cbfe00
--- /dev/null
+++ b/share/examples/sunrpc/dir/dir.x
@@ -0,0 +1,37 @@
+/* @(#)dir.x 2.1 88/08/02 4.0 RPCSRC */
+/*
+ * dir.x: Remote directory listing protocol
+ */
+const MAXNAMELEN = 255; /* maximum length of a directory entry */
+
+typedef string nametype<MAXNAMELEN>; /* a directory entry */
+
+typedef struct namenode *namelist; /* a link in the listing */
+
+/*
+ * A node in the directory listing
+ */
+struct namenode {
+ nametype name; /* name of directory entry */
+ namelist next; /* next entry */
+};
+
+/*
+ * The result of a READDIR operation.
+ */
+union readdir_res switch (int errno) {
+case 0:
+ namelist list; /* no error: return directory listing */
+default:
+ void; /* error occurred: nothing else to return */
+};
+
+/*
+ * The directory program definition
+ */
+program DIRPROG {
+ version DIRVERS {
+ readdir_res
+ READDIR(nametype) = 1;
+ } = 1;
+} = 76;
diff --git a/share/examples/sunrpc/dir/dir_proc.c b/share/examples/sunrpc/dir/dir_proc.c
new file mode 100644
index 000000000000..46221a2163cd
--- /dev/null
+++ b/share/examples/sunrpc/dir/dir_proc.c
@@ -0,0 +1,55 @@
+/* @(#)dir_proc.c 2.1 88/08/02 4.0 RPCSRC */
+/*
+ * dir_proc.c: remote readdir implementation
+ */
+#include <rpc/rpc.h>
+#include <sys/dir.h>
+#include "dir.h"
+
+extern int errno;
+extern char *malloc();
+extern char *strcpy();
+
+readdir_res *
+readdir_1(dirname)
+ nametype *dirname;
+{
+ DIR *dirp;
+ struct direct *d;
+ namelist nl;
+ namelist *nlp;
+ static readdir_res res; /* must be static! */
+
+ /*
+ * Open directory
+ */
+ dirp = opendir(*dirname);
+ if (dirp == NULL) {
+ res.errno = errno;
+ return (&res);
+ }
+
+ /*
+ * Free previous result
+ */
+ xdr_free(xdr_readdir_res, &res);
+
+ /*
+ * Collect directory entries
+ */
+ nlp = &res.readdir_res_u.list;
+ while (d = readdir(dirp)) {
+ nl = *nlp = (namenode *) malloc(sizeof(namenode));
+ nl->name = malloc(strlen(d->d_name)+1);
+ strcpy(nl->name, d->d_name);
+ nlp = &nl->next;
+ }
+ *nlp = NULL;
+
+ /*
+ * Return the result
+ */
+ res.errno = 0;
+ closedir(dirp);
+ return (&res);
+}
diff --git a/share/examples/sunrpc/dir/rls.c b/share/examples/sunrpc/dir/rls.c
new file mode 100644
index 000000000000..aa7f79f91b57
--- /dev/null
+++ b/share/examples/sunrpc/dir/rls.c
@@ -0,0 +1,81 @@
+/* @(#)rls.c 2.2 88/08/12 4.0 RPCSRC */
+/*
+ * rls.c: Remote directory listing client
+ */
+#include <stdio.h>
+#include <rpc/rpc.h> /* always need this */
+#include "dir.h" /* need this too: will be generated by rpcgen*/
+
+extern int errno;
+
+main(argc, argv)
+ int argc;
+ char *argv[];
+{
+ CLIENT *cl;
+ char *server;
+ char *dir;
+ readdir_res *result;
+ namelist nl;
+
+
+ if (argc != 3) {
+ fprintf(stderr, "usage: %s host directory\n", argv[0]);
+ exit(1);
+ }
+
+ /*
+ * Remember what our command line arguments refer to
+ */
+ server = argv[1];
+ dir = argv[2];
+
+ /*
+ * Create client "handle" used for calling DIRPROG on the
+ * server designated on the command line. We tell the rpc package
+ * to use the "tcp" protocol when contacting the server.
+ */
+ cl = clnt_create(server, DIRPROG, DIRVERS, "tcp");
+ if (cl == NULL) {
+ /*
+ * Couldn't establish connection with server.
+ * Print error message and die.
+ */
+ clnt_pcreateerror(server);
+ exit(1);
+ }
+
+ /*
+ * Call the remote procedure "readdir" on the server
+ */
+ result = readdir_1(&dir, cl);
+ if (result == NULL) {
+ /*
+ * An error occurred while calling the server.
+ * Print error message and die.
+ */
+ clnt_perror(cl, server);
+ exit(1);
+ }
+
+ /*
+ * Okay, we successfully called the remote procedure.
+ */
+ if (result->errno != 0) {
+ /*
+ * A remote system error occurred.
+ * Print error message and die.
+ */
+ errno = result->errno;
+ perror(dir);
+ exit(1);
+ }
+
+ /*
+ * Successfuly got a directory listing.
+ * Print it out.
+ */
+ for (nl = result->readdir_res_u.list; nl != NULL; nl = nl->next) {
+ printf("%s\n", nl->name);
+ }
+}