diff options
Diffstat (limited to 'share/examples/sunrpc/sort')
-rw-r--r-- | share/examples/sunrpc/sort/Makefile | 34 | ||||
-rw-r--r-- | share/examples/sunrpc/sort/rsort.c | 42 | ||||
-rw-r--r-- | share/examples/sunrpc/sort/sort.x | 18 | ||||
-rw-r--r-- | share/examples/sunrpc/sort/sort_proc.c | 26 |
4 files changed, 120 insertions, 0 deletions
diff --git a/share/examples/sunrpc/sort/Makefile b/share/examples/sunrpc/sort/Makefile new file mode 100644 index 000000000000..9ea4044b0b67 --- /dev/null +++ b/share/examples/sunrpc/sort/Makefile @@ -0,0 +1,34 @@ +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/sunrpc/sort +BIN = rsort sort_svc +GEN = sort_clnt.c sort_svc.c sort_xdr.c sort.h +LIB = -lrpclib +RPCCOM = rpcgen + +all: $(BIN) + +rsort: rsort.o sort_clnt.o sort_xdr.o + $(CC) $(LDFLAGS) -o $@ rsort.o sort_clnt.o sort_xdr.o $(LIB) + +rsort.o: rsort.c sort.h + +sort_clnt.c: + $(RPCCOM) -l sort.x >$@ + +sort_svc: sort_proc.o sort_svc.o sort_xdr.o + $(CC) $(LDFLAGS) -o $@ sort_proc.o sort_svc.o sort_xdr.o $(LIB) + +sort_proc.o: sort_proc.c sort.h + +sort_svc.c: + $(RPCCOM) -s udp sort.x >$@ + +sort_xdr.c: + $(RPCCOM) -c sort.x >$@ + +sort.h: + $(RPCCOM) -h sort.x >$@ + +clean cleanup: + rm -f $(GEN) *.o $(BIN) + diff --git a/share/examples/sunrpc/sort/rsort.c b/share/examples/sunrpc/sort/rsort.c new file mode 100644 index 000000000000..1a96d826d096 --- /dev/null +++ b/share/examples/sunrpc/sort/rsort.c @@ -0,0 +1,42 @@ +/* + * rsort.c + * Client side application which sorts argc, argv. + */ +#include <stdio.h> +#include <rpc/rpc.h> +#include "sort.h" + +main(argc, argv) + int argc; + char **argv; +{ + char *machinename; + struct sortstrings args, res; + int i; + + if (argc < 3) { + fprintf(stderr, "usage: %s machinename [s1 ...]\n", argv[0]); + exit(1); + } + machinename = argv[1]; + args.ss.ss_len = argc - 2; /* substract off progname, machinename */ + args.ss.ss_val = &argv[2]; + res.ss.ss_val = (char **)NULL; + + if ((i = callrpc(machinename, SORTPROG, SORTVERS, SORT, + xdr_sortstrings, &args, xdr_sortstrings, &res))) + { + fprintf(stderr, "%s: call to sort service failed. ", argv[0]); + clnt_perrno(i); + fprintf(stderr, "\n"); + exit(1); + } + + for (i = 0; i < res.ss.ss_len; i++) { + printf("%s\n", res.ss.ss_val[i]); + } + + /* should free res here */ + exit(0); +} + diff --git a/share/examples/sunrpc/sort/sort.x b/share/examples/sunrpc/sort/sort.x new file mode 100644 index 000000000000..9db635fa0eb5 --- /dev/null +++ b/share/examples/sunrpc/sort/sort.x @@ -0,0 +1,18 @@ +/* + * The sort procedure receives an array of strings and returns an array + * of strings. This toy service handles a maximum of 64 strings. + */ +const MAXSORTSIZE = 64; +const MAXSTRINGLEN = 64; + +typedef string str<MAXSTRINGLEN>; /* the string itself */ + +struct sortstrings { + str ss<MAXSORTSIZE>; +}; + +program SORTPROG { + version SORTVERS { + sortstrings SORT(sortstrings) = 1; + } = 1; +} = 22855; diff --git a/share/examples/sunrpc/sort/sort_proc.c b/share/examples/sunrpc/sort/sort_proc.c new file mode 100644 index 000000000000..d09df48ca883 --- /dev/null +++ b/share/examples/sunrpc/sort/sort_proc.c @@ -0,0 +1,26 @@ +#include <rpc/rpc.h> +#include "sort.h" + +static int +comparestrings(sp1, sp2) + char **sp1, **sp2; +{ + return (strcmp(*sp1, *sp2)); +} + +struct sortstrings * +sort_1(ssp) + struct sortstrings *ssp; +{ + static struct sortstrings ss_res; + + if (ss_res.ss.ss_val != (str *)NULL) + free(ss_res.ss.ss_val); + + qsort(ssp->ss.ss_val, ssp->ss.ss_len, sizeof (char *), comparestrings); + ss_res.ss.ss_len = ssp->ss.ss_len; + ss_res.ss.ss_val = (str *)malloc(ssp->ss.ss_len * sizeof(str *)); + bcopy(ssp->ss.ss_val, ss_res.ss.ss_val, + ssp->ss.ss_len * sizeof(str *)); + return(&ss_res); +} |