home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / yp-quote / quote.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-07  |  1.5 KB  |  71 lines

  1. #include <stdio.h>
  2. #include <rpcsvc/ypclnt.h>
  3.  
  4. #define MAXLEN 4096
  5. #define randint(maxval) ((random() % (maxval)) + 1)
  6.  
  7. char dom_name[BUFSIZ];
  8.  
  9. main()
  10. {
  11.     char *quotebuf;
  12.     int quotelen;
  13.     char *maxbuf;
  14.     int maxlen;
  15.     int maxnum = 0;
  16.     int quotenum = 0;
  17.     char *c;
  18.     char *index();
  19.  
  20.     srandom(getpid()); /* initialize random numbers */
  21.  
  22.     /* who are we? */
  23.     if (getdomainname(dom_name, BUFSIZ)) {
  24.     fprintf(stderr, "No domainname set\n");
  25.     exit(1);
  26.     }
  27.  
  28.     /* Get the highest quote in the system */
  29.     get_match("MAX", &maxbuf, &maxlen);
  30.     maxnum = atoi(maxbuf);
  31.  
  32.     /* Generate a random number between 1 and maxnum */
  33.     quotenum = randint(maxnum);
  34.     if (quotenum < 0 || quotenum > maxnum) {
  35.     fprintf(stderr, "Illegal randint value!\n");
  36.     exit(1);
  37.     }
  38.  
  39.     /* now grab that quote */
  40.     sprintf(maxbuf,"%d",quotenum);
  41.     get_match(maxbuf, "ebuf, "elen);
  42.  
  43.     /* turn all ^A characters to newlines.
  44.      * turn all ^B characters to tabs.
  45.      * needed to allow proper insertion into a yp database, since makedbm
  46.      * uses both as delimiters.
  47.      */
  48.     while ((c = index(quotebuf,'\001')))
  49.     *c = '\n';
  50.  
  51.     while ((c = index(quotebuf,'\002')))
  52.     *c = '\t';
  53.     
  54.     /* print it out */
  55.     printf("%s",quotebuf);
  56. }
  57.  
  58. /* get the largest quote from key MAX */
  59.  
  60. get_match(key, retval, retlen)
  61.     char *key;
  62.     char **retval;
  63.     int *retlen;
  64. {
  65.  
  66.     if (yp_match(dom_name, "quotes", key, strlen(key), retval, retlen)) {
  67.     fprintf(stderr, "error in ypmatch\n");
  68.     exit(1);
  69.     }
  70. }
  71.