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