home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume1 / rpc / part06 < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  2.6 KB

  1. Date: Wed, 3 Apr 85 00:00:27 pst
  2. From: decvax!sun!pumpkinseed!blyon (Bob Lyon)
  3. Subject: Sun RPC part 6 of 10
  4.  
  5. mkdir toys; cd toys
  6. echo x - Makefile
  7. sed 's/^X//' >Makefile <<'!Funky!Stuff!'
  8. CFLAGS= -O
  9.  
  10. all: sortit sort_service
  11.  
  12. sortit: sortit.o sort_prot.o
  13.     ${CC} ${CFLAGS} sortit.o sort_prot.o -o sortit
  14.  
  15. sort_service: sort_service.o sort_prot.o
  16.     ${CC} ${CFLAGS} sort_service.o sort_prot.o -o sort_service
  17.  
  18. X.c.o:
  19.     ${CC} ${CFLAGS} -c $*.c
  20.  
  21. !Funky!Stuff!
  22. echo x - sort_prot.c
  23. sed 's/^X//' >sort_prot.c <<'!Funky!Stuff!'
  24. /*
  25.  * sort_prot.c
  26.  * Implements the protcol filter for the toy sort service.
  27.  */
  28.  
  29. #include <rpc/rpc.h>
  30. #include "sort_prot.h"
  31.  
  32. int
  33. xdr_sortstrings(xdrs, ssp)
  34.     XDR *xdrs;
  35.     struct sortstrings *ssp;
  36. {
  37.  
  38.     return (xdr_array(xdrs, &ssp->s, &ssp->ns, MAXSORTSIZE,
  39.         sizeof (char *), xdr_wrapstring));
  40. }
  41. !Funky!Stuff!
  42. echo x - sort_prot.h
  43. sed 's/^X//' >sort_prot.h <<'!Funky!Stuff!'
  44. /*
  45.  * Protocol for a sorting service.
  46.  */
  47.  
  48. #define SORTPROG    ((long) 22855)
  49. #define SORTVERS    ((long) 1)
  50. #define SORT        ((long) 1)
  51.  
  52. /*
  53.  * The sort procedure receives an array of strings and returns an array
  54.  * of strings.  This toy service handles a maximum of 64 strings.
  55.  */
  56. #define MAXSORTSIZE    ((long) 64)
  57.  
  58. struct sortstrings {
  59.     long    ns;  /* number of strings in the array */
  60.     char    **s; /* pointer to the array of strings */
  61. };
  62.  
  63. int xdr_sortstrings();
  64.  
  65. !Funky!Stuff!
  66. echo x - sort_service.c
  67. sed 's/^X//' >sort_service.c <<'!Funky!Stuff!'
  68. /*
  69.  * sort_service.c
  70.  * Implements the server side of the sort_service.
  71.  */
  72.  
  73. #include <rpc/rpc.h>
  74. #include "sort_prot.h"
  75.  
  76. static int
  77. comparestrings(sp1, sp2) 
  78.     char **sp1, **sp2;
  79. {
  80.  
  81.     return (strcmp(*sp1, *sp2));
  82. }
  83.  
  84. static struct sortstrings *
  85. sort(ssp)
  86.     struct sortstrings *ssp;
  87. {
  88.  
  89.     qsort(ssp->s, ssp->ns, sizeof (char *), comparestrings);
  90.     return(ssp);
  91. }
  92.  
  93. main()
  94. {
  95.  
  96.     /* register the serive */
  97.     registerrpc(SORTPROG, SORTVERS, SORT,
  98.         sort, xdr_sortstrings, xdr_sortstrings);
  99.  
  100.     /* run the service forever */
  101.     svc_run();  /* never returns */
  102.     exit(1);
  103. }
  104.  
  105. !Funky!Stuff!
  106. echo x - sortit.c
  107. sed 's/^X//' >sortit.c <<'!Funky!Stuff!'
  108. /*
  109.  * sortit.c
  110.  * Client side application which sorts argc, argv.
  111.  */
  112. #include <stdio.h>
  113. #include <rpc/rpc.h>
  114. #include "sort_prot.h"
  115.  
  116. main(argc, argv)
  117.     int argc;
  118.     char **argv;
  119. {
  120.     char *machinename;
  121.     struct sortstrings args, res;
  122.     int i;
  123.  
  124.     if (argc < 2) {
  125.         fprintf(stderr, "usage: %s machinename [s1 ...]\n", argv[0]);
  126.         exit(1);
  127.     }
  128.     machinename = argv[1];
  129.     args.ns = argc;
  130.     args.s = argv;
  131.     res.s = (char **)NULL;
  132.  
  133.     callrpc(machinename, SORTPROG, SORTVERS, SORT,
  134.         xdr_sortstrings, &args, xdr_sortstrings, &res);
  135.  
  136.     for (i = 0; i < res.ns; i++) {
  137.         printf("%s\n", res.s[i]);
  138.     }
  139.  
  140.     /* should free res here */
  141.     exit(0);
  142. }
  143.  
  144. !Funky!Stuff!
  145.  
  146. exit
  147.