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