Home | History | Annotate | Line # | Download | only in larn
fortune.c revision 1.2
      1  1.2  mycroft #ifndef lint
      2  1.2  mycroft static char rcsid[] = "$Id: fortune.c,v 1.2 1993/08/02 17:20:00 mycroft Exp $";
      3  1.2  mycroft #endif /* not lint */
      4  1.2  mycroft 
      5  1.1      cgd /* fortune.c		 Larn is copyrighted 1986 by Noah Morgan. */
      6  1.1      cgd #include <sys/types.h>
      7  1.1      cgd #include <sys/stat.h>
      8  1.1      cgd #include <fcntl.h>
      9  1.1      cgd 
     10  1.1      cgd #include "header.h"
     11  1.1      cgd /*
     12  1.1      cgd  *	function to return a random fortune from the fortune file
     13  1.1      cgd  */
     14  1.1      cgd static char *base=0;	/* pointer to the fortune text */
     15  1.1      cgd static char **flines=0;	/* array of pointers to each fortune */
     16  1.1      cgd static int fd=0;		/* true if we have load the fortune info */
     17  1.1      cgd static int nlines=0;	/* # lines in fortune database */
     18  1.1      cgd 
     19  1.1      cgd char *fortune(file)
     20  1.1      cgd 	char *file;
     21  1.1      cgd 	{
     22  1.1      cgd 	register char *p;
     23  1.1      cgd 	register int lines,tmp;
     24  1.1      cgd 	struct stat stat;
     25  1.1      cgd 	char *malloc();
     26  1.1      cgd 	if (fd==0)
     27  1.1      cgd 		{
     28  1.1      cgd 		if ((fd=open(file,O_RDONLY)) < 0)	/* open the file */
     29  1.1      cgd 			return(0); /* can't find file */
     30  1.1      cgd 
     31  1.1      cgd 	/* find out how big fortune file is and get memory for it */
     32  1.1      cgd 		stat.st_size = 16384;
     33  1.1      cgd 		if ((fstat(fd,&stat) < 0) || ((base=malloc(1+stat.st_size)) == 0))
     34  1.1      cgd 			{
     35  1.1      cgd 			close(fd); fd= -1; free((char*)base); return(0); 	/* can't stat file */
     36  1.1      cgd 			}
     37  1.1      cgd 
     38  1.1      cgd 	/* read in the entire fortune file */
     39  1.1      cgd 		if (read(fd,base,stat.st_size) != stat.st_size)
     40  1.1      cgd 			{
     41  1.1      cgd 			close(fd); fd= -1; free((char*)base); return(0); 	/* can't read file */
     42  1.1      cgd 			}
     43  1.1      cgd 		close(fd);  base[stat.st_size]=0;	/* final NULL termination */
     44  1.1      cgd 
     45  1.1      cgd 	/* count up all the lines (and NULL terminate) to know memory needs */
     46  1.1      cgd 		for (p=base,lines=0; p<base+stat.st_size; p++) /* count lines */
     47  1.1      cgd 			if (*p == '\n') *p=0,lines++;
     48  1.1      cgd 		nlines = lines;
     49  1.1      cgd 
     50  1.1      cgd 	/* get memory for array of pointers to each fortune */
     51  1.1      cgd 		if ((flines=(char**)malloc(nlines*sizeof(char*))) == 0)
     52  1.1      cgd 			{
     53  1.1      cgd 			free((char*)base); fd= -1; return(0); /* malloc() failure */
     54  1.1      cgd 			}
     55  1.1      cgd 
     56  1.1      cgd 	/* now assign each pointer to a line */
     57  1.1      cgd 		for (p=base,tmp=0; tmp<nlines; tmp++)
     58  1.1      cgd 			{
     59  1.1      cgd 			flines[tmp]=p;  while (*p++); /* advance to next line */
     60  1.1      cgd 			}
     61  1.1      cgd 		}
     62  1.1      cgd 
     63  1.1      cgd 	if (fd > 2)	/* if we have a database to look at */
     64  1.1      cgd 		return(flines[rund((nlines<=0)?1:nlines)]);
     65  1.1      cgd 	else
     66  1.1      cgd 		return(0);
     67  1.1      cgd 	}
     68