home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 554b.lha / Yow / src / YOW.c-msdos next >
Encoding:
Text File  |  1991-09-10  |  2.1 KB  |  97 lines

  1. #include <stdio.h>
  2.  
  3. /* zippy.c
  4.  * 
  5.  * Print a quotation from Zippy the Pinhead.
  6.  * Qux <Kaufman-David@Yale> March 6, 1986
  7.  * 
  8.  */
  9.  
  10. #define void int
  11. #define BUFSIZE  2000
  12. #define SEP      '|'
  13.  
  14. main (argc, argv)
  15.      int argc;
  16.      char *argv[];
  17. {
  18.   FILE *fp;
  19.   char *file;
  20.   void yow();
  21.   void srand();
  22.   unsigned long timesecs();
  23.   FILE *fopen();
  24.   char *envfind();
  25.  
  26.   /* try to find YOW quotes filename in environment.  If not found, default to
  27.      file called "yowlines" in current directory. */
  28.  
  29.   if ((file=envfind("YOW"))==NULL) file="yowlines";
  30.  
  31.   if ((fp = fopen(file, "r")) == NULL) {
  32.     fprintf(stderr, "Yow!  Can't open quotes file %s\n", file);
  33.     exit(1);
  34.   }
  35.  
  36.   /* initialize random seed from clock */
  37.   srand(timesecs());
  38.  
  39.   yow(fp);
  40.   fclose(fp);
  41.   exit(0);
  42. }
  43.  
  44. void yow (fp)
  45.      FILE *fp;
  46. {
  47.   static long len = -1;
  48.   long offset;
  49.   int c, i = 0;
  50.   char buf[BUFSIZE];
  51.   long rand();
  52.   long fseek(), ftell();
  53.  
  54.   /* Get length of file, go to a random place in it */
  55.   if (len == -1) {
  56.     if (fseek(fp, 0L, 2) == -1) {
  57.       fprintf(stderr, "Yow!  fseek 1 failed");
  58.       exit(1);
  59.     }
  60.     len = ftell(fp);
  61.   }
  62.   offset = (rand()/1000) % len;
  63.   if (fseek(fp, offset, 0) == -1) {
  64.     fprintf(stderr, "Yow!  fseek 2 failed");
  65.     exit(1);
  66.   }
  67.  
  68.   /* Read until SEP, read next line, print it.
  69.      (Note that we will never print anything before the first seperator.)
  70.      If we hit EOF looking for the first SEP, just recurse. */
  71.   while ((c = getc(fp)) != SEP)
  72.     if (c == EOF) {
  73.       yow(fp);
  74.       return;
  75.     }
  76.  
  77.   /* Skip leading whitespace, then read in a quotation.
  78.      If we hit EOF before we find a non-whitespace char, recurse. */
  79.   while (isspace(c = getc(fp)))
  80.     ;
  81.   if (c == EOF) {
  82.     yow(fp);
  83.     return;
  84.   }
  85.   buf[i++] = c;
  86.   while ((c = getc(fp)) != SEP && c != EOF) {
  87.     buf[i++] = c;
  88.  
  89.     if (i == BUFSIZE-1)
  90.       /* Yow! Is this quotation too long yet? */
  91.       break;
  92.   }
  93.   buf[i++] = 0;
  94.   printf("%s\n", buf);
  95. }
  96.  
  97.