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