DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T c

⟦81063603c⟧ TextFile

    Length: 2654 (0xa5e)
    Types: TextFile
    Names: »cookie.c«

Derivation

└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
    └─⟦this⟧ »EUUGD18/General/Cookie/cookie.c« 

TextFile

/* cookie - print out an entry from the sayings file */
/*  %M%  %I%  %H%  */

#include <stdio.h>
#include "cookie.h"

#define ENTSIZE 7L
#define METACHAR '%'
#define YES 1
#define NO 0

char *sccs_id = "@(#) fortune cookie program %I% %H% by K. Lehenbauer";

extern long lseek(), time();
extern int rand();

char *cookiename = COOKIEFILE;
char *hashname = HASHFILE;

/* really_random - insure a good random return for a range, unlike an arbitrary
 * random() % n, thanks to Ken Arnold, Unix Review, October 1987
 * ...likely needs a little hacking to run under Berkely
 */
#define RANDOM_RANGE ((1 << 15) - 1)
int really_random(my_range)
int my_range;
{
	int max_multiple, rnum;

	max_multiple = RANDOM_RANGE / my_range;
	max_multiple *= my_range;
	while ((rnum = rand()) >= max_multiple)
		continue;
	return(rnum % my_range);
}

main(argc,argv)
int argc;
char *argv[];
{
	int nentries, oneiwant, c, sawmeta = 0;
	FILE *hashf, *cookief;
	long cookiepos;

	/* if we got exactly three arguments, use the cookie and hash
	 * files specified
	 */
	if (argc == 3)
	{
		cookiename = argv[1];
		hashname = argv[2];
	}
	/* otherwise if argc isn't one (no arguments, specifying the
	 * default cookie file), barf
	 */
	else if (argc != 1)
	{
		fputs("usage: cookie cookiefile hashfile\n",stderr);
		exit(1);
	}

	/* open the cookie file for read */
	if ((cookief = fopen(cookiename,"r")) == NULL)
	{
		perror(cookiename);
		exit(2);
	}

	/* open the hash file for read */
	if ((hashf = fopen(hashname,"r")) == NULL)
	{
		perror(hashname);
		exit(2);
	}

	/* compute number of cookie addresses in the hash file by
	 * dividing the file length by the size of a cookie address
	 */
	if (fseek(hashf,0L,2) != 0)
	{
		fputs("cookie: fseek failed\n",stderr);
		exit(3);
	}
	nentries = ftell(hashf) / 7L;

	/* seed the random number generator with time in seconds plus
	 * the program's process ID - it yields a pretty good seed
	 * again, thanks to Ken Arnold
	 */
	srand(getpid() + time(NULL));

	/* generate a not really random number */
	oneiwant = really_random(nentries);

	/* locate the one I want in the hash file and read the
	 * address found there
	 */
	fseek(hashf,(long)oneiwant * ENTSIZE, 0);
	fscanf(hashf,"%lx",&cookiepos);

	/* seek cookie file to cookie starting at address read from hash */
	fseek(cookief,cookiepos,0);

	/* get characters from the cookie file and write them out
	 * until finding the end-of-fortune sequence, '%%'
	 */
	while ((c = fgetc(cookief)) != EOF && sawmeta < 2)
	{
		if (c != METACHAR)
		{
			if (sawmeta)
				putchar(METACHAR);
			putchar(c);
			sawmeta = 0;
		}
		else
			sawmeta++;
	}
	exit(0);
}

/* end of cookie.c */