/************************************************************************/
/* Filename: COOKIE.C					Main Program	*/
/*									*/
/* Author: Rick Stevens, Small Scale Systems of Southern California	*/
/* Last Edit Date: 4 April 1987						*/
/*									*/
/* Revisions:								*/
/*									*/
/*	Version 1.0f -	Edited to use logical assign COOKIES: and to	*/
/*			run under Lattice 3.10.  4-Apr-87, Fred Fish	*/
/*	Version 1.0  -	First release of program. No modifications	*/
/*									*/
/* This program requires the file "COOKIE.DAT" to be in the "COOKIES:"	*/
/* directory or else you will be given the message:			*/
/*	"All the cookies are  broken".					*/
/*									*/
/* This program released to the public domain by Rick Stevens, Small	*/
/* Scale Systems of Southern California. It may be copied and		*/
/* distributed freely without fee. The copyright notice MUST be		*/
/* included in all future copies.					*/
/*									*/
/* Written in Lattice C, Version 3.03B.					*/
/*									*/
/* Copyright (C) 1987, Richard P. Stevens, II and			*/
/* 		       Small Scale Systems of Southern California	*/
/************************************************************************/

#include <stdio.h>			/* Main header file		*/
#include <fcntl.h>			/* File control header file	*/
#define FILSIZ 37028			/* Current COOKIE.DAT size	*/

#ifdef ORIGCODE
extern int Enable_Abort;
#endif

main() {

    int fd;				/* File descriptor for data file*/
    int ok;				/* OK flag			*/
    int temp;				/* Scratch			*/
    int i;				/* Scratch			*/
    char ch;				/* Scratch			*/
    char buf[600];			/* Destination for fortune	*/
    unsigned long seed[3];		/* Seed for random number	*/
    long pos;				/* For file positioning		*/

    long lseek();			/* LSEEK() returns a long	*/
#ifdef ORIGCODE
    long lrand48();			/* So does LRAND48()		*/
    Enable_Abort = 1;			/* Enable CTRL/C & CTRL/D abort	*/
#else
    int rand ();
#endif
    
    fd = open("COOKIES:cookie.dat",O_RDONLY);	/* Open the data file	*/
    if (fd <= 0) {
        printf("\nSorry, all of the cookies are broken!\n");
	exit(0);
    }
    
    ok = 0;
    DateStamp(seed);			/* Get a seed value		*/
#ifdef ORIGCODE
    srand48(seed[2]);			/* Seed the random number gen.	*/
#else
    srand ((unsigned int) seed[2]);
#endif
    while (!ok) {			/* While there's no fortune...	*/
#ifdef ORIGCODE
	pos = lrand48();		/* Get a random number		*/
#else
	pos = rand ();
#endif
	pos = (long) (pos/FILSIZ);	/* Make pos more reasonable	*/
	seed[0] = lseek(fd,pos,0);	/* Can we seek to it?		*/
        if (seed[0] <= 0L)
	    continue;			/* Nope, back to WHILE (!OK)	*/

	while ((temp = read(fd,&ch,1)) > 0) {	/* Find the next <FF>	*/
	    if (ch == 0x0c)		/* Was it a form feed?		*/
	        break;			/* Yes, stop the search		*/
	}

	if (temp <= 0)			/* Did we get a <FF> really?	*/
	    continue;			/* Nope, back to WHILE (!OK)	*/
	    
	i = 0;				/* Clear the buffer counter	*/
	while ((temp = read(fd,&ch,1)) > 0) {	/* Read fortune	*/
	    if (ch == 0x0c) {		/* Did we read a form feed?	*/
	        buf[i] = '\0';		/* Yes, stuff in a null char.	*/
		ok = 1;			/* Set the OK flag		*/
		break;			/* Break out of read loop	*/
	    } else {			/* Not a form feed, so...	*/
		buf[i] = ch;		/* ...stuff it into the buffer	*/
	        ++i;			/* increment the counter	*/
	    }
	}
	if (ok)
	    break;
    }					/* End of WHILE (!OK) loop	*/

    temp = close(fd);			/* Close the data file		*/
    printf("(C) 1987, Rick Stevens and ");
    printf("Small Scale Systems of Southern California");
    printf("\n%s\n",buf);		/* Print out the buffer		*/
}
/************************************************************************/
/* 	End of file: COOKIE.C						*/
/************************************************************************/
