[mod.sources] Sun RPC part 6 of 10

sources-request@genrad.UUCP (04/15/85)

From: blyon@sun (Bob Lyon)

----------------- cut here --------------------
# run this script through "sh" to extract files
mkdir toys; cd toys
echo x - Makefile
sed 's/^X//' >Makefile <<'!Funky!Stuff!'
CFLAGS= -O

all: sortit sort_service

sortit: sortit.o sort_prot.o
	${CC} ${CFLAGS} sortit.o sort_prot.o -o sortit

sort_service: sort_service.o sort_prot.o
	${CC} ${CFLAGS} sort_service.o sort_prot.o -o sort_service

X.c.o:
	${CC} ${CFLAGS} -c $*.c

!Funky!Stuff!
echo x - sort_prot.c
sed 's/^X//' >sort_prot.c <<'!Funky!Stuff!'
/*
 * sort_prot.c
 * Implements the protcol filter for the toy sort service.
 */

#include <rpc/rpc.h>
#include "sort_prot.h"

int
xdr_sortstrings(xdrs, ssp)
	XDR *xdrs;
	struct sortstrings *ssp;
{

	return (xdr_array(xdrs, &ssp->s, &ssp->ns, MAXSORTSIZE,
	    sizeof (char *), xdr_wrapstring));
}
!Funky!Stuff!
echo x - sort_prot.h
sed 's/^X//' >sort_prot.h <<'!Funky!Stuff!'
/*
 * Protocol for a sorting service.
 */

#define SORTPROG	((long) 22855)
#define SORTVERS	((long) 1)
#define SORT		((long) 1)

/*
 * The sort procedure receives an array of strings and returns an array
 * of strings.  This toy service handles a maximum of 64 strings.
 */
#define MAXSORTSIZE	((long) 64)

struct sortstrings {
	long	ns;  /* number of strings in the array */
	char	**s; /* pointer to the array of strings */
};

int xdr_sortstrings();

!Funky!Stuff!
echo x - sort_service.c
sed 's/^X//' >sort_service.c <<'!Funky!Stuff!'
/*
 * sort_service.c
 * Implements the server side of the sort_service.
 */

#include <rpc/rpc.h>
#include "sort_prot.h"

static int
comparestrings(sp1, sp2) 
	char **sp1, **sp2;
{

	return (strcmp(*sp1, *sp2));
}

static struct sortstrings *
sort(ssp)
	struct sortstrings *ssp;
{

	qsort(ssp->s, ssp->ns, sizeof (char *), comparestrings);
	return(ssp);
}

main()
{

	/* register the serive */
	registerrpc(SORTPROG, SORTVERS, SORT,
	    sort, xdr_sortstrings, xdr_sortstrings);

	/* run the service forever */
	svc_run();  /* never returns */
	exit(1);
}

!Funky!Stuff!
echo x - sortit.c
sed 's/^X//' >sortit.c <<'!Funky!Stuff!'
/*
 * sortit.c
 * Client side application which sorts argc, argv.
 */
#include <stdio.h>
#include <rpc/rpc.h>
#include "sort_prot.h"

main(argc, argv)
	int argc;
	char **argv;
{
	char *machinename;
	struct sortstrings args, res;
	int i;

	if (argc < 2) {
		fprintf(stderr, "usage: %s machinename [s1 ...]\n", argv[0]);
		exit(1);
	}
	machinename = argv[1];
	args.ns = argc;
	args.s = argv;
	res.s = (char **)NULL;

	callrpc(machinename, SORTPROG, SORTVERS, SORT,
	    xdr_sortstrings, &args, xdr_sortstrings, &res);

	for (i = 0; i < res.ns; i++) {
		printf("%s\n", res.s[i]);
	}

	/* should free res here */
	exit(0);
}

!Funky!Stuff!

exit