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

Defined functions

main defined in line 16; never used
yow defined in line 39; used 4 times

Defined macros

BUFSIZE defined in line 12; used 1 times
  • in line 45
SEP defined in line 13; used 2 times
YOW_FILE defined in line 14; used 1 times
  • in line 24
Last modified: 1986-03-28
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1055
Valid CSS Valid XHTML 1.0 Strict