home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 554b.lha / Yow / src / YOW.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-10  |  3.3 KB  |  125 lines

  1. #include <stdio.h>
  2. #include "ctype.h"
  3.  
  4. /* zippy.c
  5.  * 
  6.  * Print a quotation from Zippy the Pinhead.
  7.  * Qux <Kaufman-David@Yale> March 6, 1986
  8.  * 
  9.  */
  10.  
  11. /*  Modified by Kurt Wessels 1/16/89 to compile on Amiga with Manx C:
  12.     o   Commented out the odd #define (first one below)
  13.     o   Changed environment variable finding function to Manx one (meaning
  14.         you will need "yowlines" file in current directory when you run
  15.         yow, unless you have Manx C's "set" command.  If you do have it,
  16.         use it via, e.g. "set YOW=dh1:utils/yowlines").
  17.     o   Stripped out Messy-DOS carriage returns and ^Z's in this file.
  18.     o   Changed timesecs() to time() for srand() seeding.
  19.     o   Added #include ctype.h so isspace() macro is defined.
  20.     o   Changed some incorrect longs to ints; due to this, removed
  21.         the 1000 divider in the line with rand() in yow() routine.
  22.  
  23. # Manx makefile for yow (includes SDB options)
  24.  
  25. yow:        yow.o
  26.         ln +q -g yow.o -lc
  27. yow.o:    yow.c
  28.         cc -n -o yow.o yow.c
  29.  */
  30.  
  31. /* #define void int */  /* what the heck was this for???? kw */
  32. #define BUFSIZE  2000
  33. #define SEP      '|'
  34.  
  35. main (argc, argv)
  36.      int argc;
  37.      char *argv[];
  38. {
  39.   FILE *fp;
  40.   char *file;
  41.   void yow();
  42.   void srand();
  43.   /* unsigned long timesecs();  not Amiga */
  44.   long time();  /* Manx time routine */
  45.   FILE *fopen();
  46.   /* char *envfind();   not Amiga  */
  47.   char * getenv();  /* Manx environment variable routine */
  48.  
  49.   /* try to find YOW quotes filename in environment.  If not found, default to
  50.      file called "yowlines" in current directory. */
  51.  
  52.   /* if ((file=envfind("YOW"))==NULL) file="yowlines";   not valid on Amiga */
  53.   if ((file=getenv("YOW"))==NULL) file="yowlines"; /* Manx "set" cmd only */
  54.  
  55.   if ((fp = fopen(file, "r")) == NULL) {
  56.     fprintf(stderr, "Yow!  Can't open quotes file %s\n", file);
  57.     exit(1);
  58.   }
  59.  
  60.   /* initialize random seed from clock */
  61.   /* srand(timesecs());    not the Amiga way! */
  62.   srand((unsigned int) time(NULL));  /* Manx time routine */
  63.  
  64.   yow(fp);
  65.   fclose(fp);
  66.   exit(0);
  67. }
  68.  
  69. void yow (fp)
  70.      FILE *fp;
  71. {
  72.   static long len = -1;
  73.   long offset;
  74.   int c, i = 0;
  75.   char buf[BUFSIZE];
  76.   /* long rand();   should be int */
  77.   int rand();
  78.   /* long fseek();  should be int */
  79.   int fseek();
  80.   long ftell();
  81.  
  82.   /* Get length of file, go to a random place in it */
  83.   if (len == -1) {
  84.     if (fseek(fp, 0L, 2) == -1) {
  85.       fprintf(stderr, "Yow!  fseek 1 failed");
  86.       exit(1);
  87.     }
  88.     len = ftell(fp);
  89.   }
  90. /* offset = (rand()/1000) % len;  this was for longs, changed for ints */
  91.   offset = rand() % len;
  92.   if (fseek(fp, offset, 0) == -1) {
  93.     fprintf(stderr, "Yow!  fseek 2 failed");
  94.     exit(1);
  95.   }
  96.  
  97.   /* Read until SEP, read next line, print it.
  98.      (Note that we will never print anything before the first seperator.)
  99.      If we hit EOF looking for the first SEP, just recurse. */
  100.   while ((c = getc(fp)) != SEP)
  101.     if (c == EOF) {
  102.       yow(fp);
  103.       return;
  104.     }
  105.  
  106.   /* Skip leading whitespace, then read in a quotation.
  107.      If we hit EOF before we find a non-whitespace char, recurse. */
  108.   while (isspace(c = getc(fp)))
  109.     ;
  110.   if (c == EOF) {
  111.     yow(fp);
  112.     return;
  113.   }
  114.   buf[i++] = c;
  115.   while ((c = getc(fp)) != SEP && c != EOF) {
  116.     buf[i++] = c;
  117.  
  118.     if (i == BUFSIZE-1)
  119.       /* Yow! Is this quotation too long yet? */
  120.       break;
  121.   }
  122.   buf[i++] = 0;
  123.   printf("%s\n", buf);
  124. }
  125.