home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / GROENING.ZIP / HELL.C < prev    next >
C/C++ Source or Header  |  1991-11-07  |  2KB  |  96 lines

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