home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / editors / mntemacs.zoo / etc / yow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-29  |  1.8 KB  |  100 lines

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