]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/fortune.c
2 static char rcsid
[] = "$Id: fortune.c,v 1.2 1993/08/02 17:20:00 mycroft Exp $";
5 /* fortune.c Larn is copyrighted 1986 by Noah Morgan. */
12 * function to return a random fortune from the fortune file
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 */
23 register int lines
,tmp
;
28 if ((fd
=open(file
,O_RDONLY
)) < 0) /* open the file */
29 return(0); /* can't find file */
31 /* find out how big fortune file is and get memory for it */
33 if ((fstat(fd
,&stat
) < 0) || ((base
=malloc(1+stat
.st_size
)) == 0))
35 close(fd
); fd
= -1; free((char*)base
); return(0); /* can't stat file */
38 /* read in the entire fortune file */
39 if (read(fd
,base
,stat
.st_size
) != stat
.st_size
)
41 close(fd
); fd
= -1; free((char*)base
); return(0); /* can't read file */
43 close(fd
); base
[stat
.st_size
]=0; /* final NULL termination */
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
++;
50 /* get memory for array of pointers to each fortune */
51 if ((flines
=(char**)malloc(nlines
*sizeof(char*))) == 0)
53 free((char*)base
); fd
= -1; return(0); /* malloc() failure */
56 /* now assign each pointer to a line */
57 for (p
=base
,tmp
=0; tmp
<nlines
; tmp
++)
59 flines
[tmp
]=p
; while (*p
++); /* advance to next line */
63 if (fd
> 2) /* if we have a database to look at */
64 return(flines
[rund((nlines
<=0)?1:nlines
)]);