home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include "ctype.h"
-
- /* zippy.c
- *
- * Print a quotation from Zippy the Pinhead.
- * Qux <Kaufman-David@Yale> March 6, 1986
- *
- */
-
- /* Modified by Kurt Wessels 1/16/89 to compile on Amiga with Manx C:
- o Commented out the odd #define (first one below)
- o Changed environment variable finding function to Manx one (meaning
- you will need "yowlines" file in current directory when you run
- yow, unless you have Manx C's "set" command. If you do have it,
- use it via, e.g. "set YOW=dh1:utils/yowlines").
- o Stripped out Messy-DOS carriage returns and ^Z's in this file.
- o Changed timesecs() to time() for srand() seeding.
- o Added #include ctype.h so isspace() macro is defined.
- o Changed some incorrect longs to ints; due to this, removed
- the 1000 divider in the line with rand() in yow() routine.
-
- # Manx makefile for yow (includes SDB options)
-
- yow: yow.o
- ln +q -g yow.o -lc
- yow.o: yow.c
- cc -n -o yow.o yow.c
- */
-
- /* #define void int */ /* what the heck was this for???? kw */
- #define BUFSIZE 2000
- #define SEP '|'
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
- FILE *fp;
- char *file;
- void yow();
- void srand();
- /* unsigned long timesecs(); not Amiga */
- long time(); /* Manx time routine */
- FILE *fopen();
- /* char *envfind(); not Amiga */
- char * getenv(); /* Manx environment variable routine */
-
- /* try to find YOW quotes filename in environment. If not found, default to
- file called "yowlines" in current directory. */
-
- /* if ((file=envfind("YOW"))==NULL) file="yowlines"; not valid on Amiga */
- if ((file=getenv("YOW"))==NULL) file="yowlines"; /* Manx "set" cmd only */
-
- if ((fp = fopen(file, "r")) == NULL) {
- fprintf(stderr, "Yow! Can't open quotes file %s\n", file);
- exit(1);
- }
-
- /* initialize random seed from clock */
- /* srand(timesecs()); not the Amiga way! */
- srand((unsigned int) time(NULL)); /* Manx time routine */
-
- yow(fp);
- fclose(fp);
- exit(0);
- }
-
- void yow (fp)
- FILE *fp;
- {
- static long len = -1;
- long offset;
- int c, i = 0;
- char buf[BUFSIZE];
- /* long rand(); should be int */
- int rand();
- /* long fseek(); should be int */
- int fseek();
- long ftell();
-
- /* Get length of file, go to a random place in it */
- if (len == -1) {
- if (fseek(fp, 0L, 2) == -1) {
- fprintf(stderr, "Yow! fseek 1 failed");
- exit(1);
- }
- len = ftell(fp);
- }
- /* offset = (rand()/1000) % len; this was for longs, changed for ints */
- offset = rand() % len;
- if (fseek(fp, offset, 0) == -1) {
- fprintf(stderr, "Yow! fseek 2 failed");
- exit(1);
- }
-
- /* Read until SEP, read next line, print it.
- (Note that we will never print anything before the first seperator.)
- If we hit EOF looking for the first SEP, just recurse. */
- while ((c = getc(fp)) != SEP)
- if (c == EOF) {
- yow(fp);
- return;
- }
-
- /* Skip leading whitespace, then read in a quotation.
- If we hit EOF before we find a non-whitespace char, recurse. */
- while (isspace(c = getc(fp)))
- ;
- if (c == EOF) {
- yow(fp);
- return;
- }
- buf[i++] = c;
- while ((c = getc(fp)) != SEP && c != EOF) {
- buf[i++] = c;
-
- if (i == BUFSIZE-1)
- /* Yow! Is this quotation too long yet? */
- break;
- }
- buf[i++] = 0;
- printf("%s\n", buf);
- }
-