2 * Copyright (c) 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1986, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 #include <sys/endian.h>
64 #include "pathnames.h"
69 #define MINW 6 /* minimum wait if desired */
70 #define CPERS 20 /* # of chars for each sec */
71 #define SLEN 160 /* # of chars in short fortune */
73 #define POS_UNKNOWN ((uint32_t) -1) /* pos for file unknown */
74 #define NO_PROB (-1) /* no prob specified for file */
77 #define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
91 char *datfile, *posfile;
96 struct fd *child, *parent;
97 struct fd *next, *prev;
100 static bool Found_one; /* did we find a match? */
101 static bool Find_files = FALSE; /* just find a list of proper fortune files */
102 static bool Fortunes_only = FALSE; /* check only "fortunes" files */
103 static bool Wait = FALSE; /* wait desired after fortune */
104 static bool Short_only = FALSE; /* short fortune desired */
105 static bool Long_only = FALSE; /* long fortune desired */
106 static bool Offend = FALSE; /* offensive fortunes only */
107 static bool All_forts = FALSE; /* any fortune allowed */
108 static bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
109 static bool Match = FALSE; /* dump fortunes matching a pattern */
110 static bool WriteToDisk = false; /* use files on disk to save state */
112 static int Debug = 0; /* print debug messages */
115 static char *Fortbuf = NULL; /* fortune buffer for -m */
117 static int Fort_len = 0;
119 static off_t Seekpts[2]; /* seek pointers to fortunes */
121 static FILEDESC *File_list = NULL, /* Head of file list */
122 *File_tail = NULL; /* Tail of file list */
123 static FILEDESC *Fortfile; /* Fortune file to use */
125 static STRFILE Noprob_tbl; /* sum of data for all no prob files */
127 static const char *Fortune_path;
128 static char **Fortune_path_arr;
130 static int add_dir(FILEDESC *);
131 static int add_file(int, const char *, const char *, FILEDESC **,
132 FILEDESC **, FILEDESC *);
133 static void all_forts(FILEDESC *, char *);
134 static char *copy(const char *, u_int);
135 static void display(FILEDESC *);
136 static void do_free(void *);
137 static void *do_malloc(u_int);
138 static int form_file_list(char **, int);
139 static int fortlen(void);
140 static void get_fort(void);
141 static void get_pos(FILEDESC *);
142 static void get_tbl(FILEDESC *);
143 static void getargs(int, char *[]);
144 static void getpath(void);
145 static void init_prob(void);
146 static int is_dir(const char *);
147 static int is_fortfile(const char *, char **, char **, int);
148 static int is_off_name(const char *);
149 static int max(int, int);
150 static FILEDESC *new_fp(void);
151 static char *off_name(const char *);
152 static void open_dat(FILEDESC *);
153 static void open_fp(FILEDESC *);
154 static FILEDESC *pick_child(FILEDESC *);
155 static void print_file_list(void);
156 static void print_list(FILEDESC *, int);
157 static void sum_noprobs(FILEDESC *);
158 static void sum_tbl(STRFILE *, STRFILE *);
159 static void usage(void);
160 static void zero_tbl(STRFILE *);
162 static char *conv_pat(char *);
163 static int find_matches(void);
164 static void matches_in_list(FILEDESC *);
165 static int maxlen_in_list(FILEDESC *);
167 static regex_t Re_pat;
170 main(int argc, char *argv[])
174 if (getenv("FORTUNE_SAVESTATE") != NULL)
177 (void) setlocale(LC_ALL, "");
183 exit(find_matches() != 0);
188 } while ((Short_only && fortlen() > SLEN) ||
189 (Long_only && fortlen() <= SLEN));
194 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
195 perror(Fortfile->posfile);
199 * if we can, we exclusive lock, but since it isn't very
200 * important, we just punt if we don't have easy locking
204 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
205 if (!Fortfile->was_pos_file)
206 chmod(Fortfile->path, 0666);
212 sleep((unsigned int) max(Fort_len / CPERS, MINW));
219 display(FILEDESC *fp)
226 fseeko(fp->inf, Seekpts[0], SEEK_SET);
227 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
228 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
229 if (fp->tbl.str_flags & STR_ROTATED)
230 for (p = line; (ch = *p) != '\0'; ++p) {
233 *p = 'A' + (ch - 'A' + 13) % 26;
234 else if (islower(ch))
235 *p = 'a' + (ch - 'a' + 13) % 26;
238 if (fp->tbl.str_flags & STR_COMMENTS
239 && line[0] == fp->tbl.str_delim
240 && line[1] == fp->tbl.str_delim)
244 (void) fflush(stdout);
249 * Return the length of the fortune.
257 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
258 nchar = (int)(Seekpts[1] - Seekpts[0]);
261 fseeko(Fortfile->inf, Seekpts[0], SEEK_SET);
263 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
264 !STR_ENDSTRING(line, Fortfile->tbl))
265 nchar += strlen(line);
273 * This routine evaluates the arguments on the command line
276 getargs(int argc, char *argv[])
286 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
288 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
291 case 'a': /* any fortune */
300 Equal_probs++; /* scatter un-allocted prob equally */
302 case 'f': /* find fortune files */
305 case 'l': /* long ones only */
309 case 'o': /* offensive ones only */
312 case 's': /* short ones only */
316 case 'w': /* give time to read */
319 case 'm': /* dump out the fortunes */
323 case 'i': /* case-insensitive match */
333 if (!form_file_list(argv, argc))
334 exit(1); /* errors printed through form_file_list() */
349 error = regcomp(&Re_pat, pat, REG_BASIC);
351 fprintf(stderr, "regcomp(%s) fails\n", pat);
359 * Form the file list from the file specifications.
362 form_file_list(char **files, int file_cnt)
370 Fortunes_only = TRUE;
371 pstr = Fortune_path_arr;
374 i += add_file(NO_PROB, *pstr++, NULL,
375 &File_list, &File_tail, NULL);
377 Fortunes_only = FALSE;
379 fprintf(stderr, "No fortunes found in %s.\n",
384 pstr = Fortune_path_arr;
387 i += add_file(NO_PROB, "fortunes", *pstr++,
388 &File_list, &File_tail, NULL);
391 fprintf(stderr, "No fortunes found in %s.\n",
397 for (i = 0; i < file_cnt; i++) {
399 if (!isdigit((unsigned char)files[i][0]))
403 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
404 percent = percent * 10 + *sp - '0';
406 fprintf(stderr, "percentages must be <= 100\n");
410 fprintf(stderr, "percentages must be integers\n");
414 * If the number isn't followed by a '%', then
415 * it was not a percentage, just the first part
416 * of a file name which starts with digits.
422 else if (*++sp == '\0') {
423 if (++i >= file_cnt) {
424 fprintf(stderr, "percentages must precede files\n");
430 if (strcmp(sp, "all") == 0) {
431 pstr = Fortune_path_arr;
434 i += add_file(NO_PROB, *pstr++, NULL,
435 &File_list, &File_tail, NULL);
438 fprintf(stderr, "No fortunes found in %s.\n",
442 } else if (!add_file(percent, sp, NULL, &File_list,
453 * Add a file to the file list.
456 add_file(int percent, const char *file, const char *dir, FILEDESC **head,
457 FILEDESC **tail, FILEDESC *parent)
462 char *tpath, *offensive;
472 tpath = do_malloc((unsigned int)(strlen(dir) + strlen(file) + 2));
473 strcat(strcat(strcpy(tpath, dir), "/"), file);
477 if ((isdir = is_dir(path)) && parent != NULL) {
480 return (FALSE); /* don't recurse */
483 if (!isdir && parent == NULL && (All_forts || Offend) &&
484 !is_off_name(path)) {
485 offensive = off_name(path);
492 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
493 file = off_name(file);
497 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
499 if ((fd = open(path, O_RDONLY)) < 0) {
501 * This is a sneak. If the user said -a, and if the
502 * file we're given isn't a file, we check to see if
503 * there is a -o version. If there is, we treat it as
504 * if *that* were the file given. We only do this for
505 * individual files -- if we're scanning a directory,
506 * we'll pick up the -o file anyway.
508 if (All_forts && offensive != NULL) {
514 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
515 file = off_name(file);
518 if (dir == NULL && file[0] != '/') {
520 char **pstr = Fortune_path_arr;
523 i += add_file(percent, file, *pstr++,
527 fprintf(stderr, "No '%s' found in %s.\n",
541 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
545 fp->percent = percent;
550 if ((isdir && !add_dir(fp)) ||
552 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
556 "fortune:%s not a fortune file or directory\n",
560 do_free(fp->datfile);
561 do_free(fp->posfile);
567 * If the user said -a, we need to make this node a pointer to
568 * both files, if there are two. We don't need to do this if
569 * we are scanning a directory, since the scan will pick up the
572 if (All_forts && parent == NULL && !is_off_name(path))
573 all_forts(fp, offensive);
576 else if (fp->percent == NO_PROB) {
587 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
594 * Return a pointer to an initialized new FILEDESC.
601 fp = do_malloc(sizeof(*fp));
603 fp->pos = POS_UNKNOWN;
606 fp->percent = NO_PROB;
607 fp->read_tbl = FALSE;
620 * Return a pointer to the offensive version of a file of this name.
623 off_name(const char *file)
627 new = copy(file, (unsigned int) (strlen(file) + 2));
629 return (strcat(new, "-o"));
634 * Is the file an offensive-style name?
637 is_off_name(const char *file)
643 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
648 * Modify a FILEDESC element to be the parent of two children if
649 * there are two children to be a parent of.
652 all_forts(FILEDESC *fp, char *offensive)
655 FILEDESC *scene, *obscene;
657 char *datfile, *posfile;
659 if (fp->child != NULL) /* this is a directory, not a file */
661 if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
663 if ((fd = open(offensive, O_RDONLY)) < 0)
665 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
670 fp->num_children = 2;
672 scene->next = obscene;
673 obscene->next = NULL;
674 scene->child = obscene->child = NULL;
675 scene->parent = obscene->parent = fp;
678 scene->percent = obscene->percent = NO_PROB;
682 obscene->path = offensive;
683 if ((sp = strrchr(offensive, '/')) == NULL)
684 obscene->name = offensive;
686 obscene->name = ++sp;
687 obscene->datfile = datfile;
688 obscene->posfile = posfile;
689 obscene->read_tbl = false;
691 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
696 * Add the contents of an entire directory.
699 add_dir(FILEDESC *fp)
702 struct dirent *dirent;
706 (void) close(fp->fd);
708 if ((dir = opendir(fp->path)) == NULL) {
713 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
714 fp->num_children = 0;
715 while ((dirent = readdir(dir)) != NULL) {
716 if (dirent->d_namlen == 0)
718 name = copy(dirent->d_name, dirent->d_namlen);
719 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
724 if (fp->num_children == 0) {
725 (void) fprintf(stderr,
726 "fortune: %s: No fortune files in directory.\n", fp->path);
735 * Return TRUE if the file is a directory, FALSE otherwise.
738 is_dir(const char *file)
742 if (stat(file, &sbuf) < 0)
745 return (sbuf.st_mode & S_IFDIR);
750 * Return TRUE if the file is a fortune database file. We try and
751 * exclude files without reading them if possible to avoid
752 * overhead. Files which start with ".", or which have "illegal"
753 * suffixes, as contained in suflist[], are ruled out.
757 is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
762 static const char *suflist[] = {
763 /* list of "illegal" suffixes" */
764 "dat", "pos", "c", "h", "p", "i", "f",
765 "pas", "ftn", "ins.c", "ins,pas",
770 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
773 * Preclude any -o files for offendable people, and any non -o
774 * files for completely offensive people.
776 if (check_for_offend && !All_forts) {
778 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
779 DPRINTF(2, (stderr, "FALSE (offending file)\n"));
784 if ((sp = strrchr(file, '/')) == NULL)
789 DPRINTF(2, (stderr, "FALSE (file starts with '.')\n"));
792 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
793 DPRINTF(2, (stderr, "FALSE (check fortunes only)\n"));
796 if ((sp = strrchr(sp, '.')) != NULL) {
798 for (i = 0; suflist[i] != NULL; i++)
799 if (strcmp(sp, suflist[i]) == 0) {
800 DPRINTF(2, (stderr, "FALSE (file has suffix \".%s\")\n", sp));
805 datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
806 strcat(datfile, ".dat");
807 if (access(datfile, R_OK) < 0) {
808 DPRINTF(2, (stderr, "FALSE (no readable \".dat\" file)\n"));
811 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
822 *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
823 strcat(*posp, ".pos");
829 DPRINTF(2, (stderr, "TRUE\n"));
836 * Return a malloc()'ed copy of the string
839 copy(const char *str, unsigned int len)
843 new = do_malloc(len + 1);
854 * Do a malloc, checking for NULL return.
857 do_malloc(unsigned int size)
861 if ((new = malloc(size)) == NULL) {
862 (void) fprintf(stderr, "fortune: out of memory.\n");
871 * Free malloc'ed space, if any.
882 * Initialize the fortune probabilities.
887 FILEDESC *fp, *last = NULL;
888 int percent, num_noprob, frac;
891 * Distribute the residual probability (if any) across all
892 * files with unspecified probability (i.e., probability of 0)
898 for (fp = File_tail; fp != NULL; fp = fp->prev)
899 if (fp->percent == NO_PROB) {
904 percent += fp->percent;
905 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
906 percent, num_noprob));
908 (void) fprintf(stderr,
909 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
911 } else if (percent < 100 && num_noprob == 0) {
912 (void) fprintf(stderr,
913 "fortune: no place to put residual probability (%d%% < 100%%)\n",
916 } else if (percent == 100 && num_noprob != 0) {
917 (void) fprintf(stderr,
918 "fortune: no probability left to put in residual files (100%%)\n");
921 percent = 100 - percent;
923 if (num_noprob != 0) {
924 if (num_noprob > 1) {
925 frac = percent / num_noprob;
926 DPRINTF(1, (stderr, ", frac = %d%%", frac));
927 for (fp = File_tail; fp != last; fp = fp->prev)
928 if (fp->percent == NO_PROB) {
933 last->percent = percent;
934 DPRINTF(1, (stderr, ", residual = %d%%", percent));
938 ", %d%% distributed over remaining fortunes\n",
941 DPRINTF(1, (stderr, "\n"));
951 * Get the fortune data file's seek pointer for the next fortune.
959 if (File_list->next == NULL || File_list->percent == NO_PROB)
962 choice = arc4random_uniform(100);
963 DPRINTF(1, (stderr, "choice = %d\n", choice));
964 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
965 if (choice < fp->percent)
968 choice -= fp->percent;
970 " skip \"%s\", %d%% (choice = %d)\n",
971 fp->name, fp->percent, choice));
974 "using \"%s\", %d%% (choice = %d)\n",
975 fp->name, fp->percent, choice));
977 if (fp->percent != NO_PROB)
980 if (fp->next != NULL) {
982 choice = arc4random_uniform(Noprob_tbl.str_numstr);
983 DPRINTF(1, (stderr, "choice = %d (of %u) \n", choice,
984 Noprob_tbl.str_numstr));
985 while ((unsigned int)choice >= fp->tbl.str_numstr) {
986 choice -= fp->tbl.str_numstr;
989 " skip \"%s\", %u (choice = %d)\n",
990 fp->name, fp->tbl.str_numstr,
993 DPRINTF(1, (stderr, "using \"%s\", %u\n", fp->name,
994 fp->tbl.str_numstr));
998 if (fp->child != NULL) {
999 DPRINTF(1, (stderr, "picking child\n"));
1000 fp = pick_child(fp);
1006 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), SEEK_SET);
1007 read(fp->datfd, Seekpts, sizeof Seekpts);
1008 Seekpts[0] = be64toh(Seekpts[0]);
1009 Seekpts[1] = be64toh(Seekpts[1]);
1014 * Pick a child from a chosen parent.
1017 pick_child(FILEDESC *parent)
1023 choice = arc4random_uniform(parent->num_children);
1024 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1025 choice, parent->num_children));
1026 for (fp = parent->child; choice--; fp = fp->next)
1028 DPRINTF(1, (stderr, " using %s\n", fp->name));
1033 choice = arc4random_uniform(parent->tbl.str_numstr);
1034 DPRINTF(1, (stderr, " choice = %d (of %u)\n",
1035 choice, parent->tbl.str_numstr));
1036 for (fp = parent->child; (unsigned)choice >= fp->tbl.str_numstr;
1038 choice -= fp->tbl.str_numstr;
1039 DPRINTF(1, (stderr, "\tskip %s, %u (choice = %d)\n",
1040 fp->name, fp->tbl.str_numstr, choice));
1042 DPRINTF(1, (stderr, " using %s, %u\n", fp->name,
1043 fp->tbl.str_numstr));
1050 * Sum up all the noprob probabilities, starting with fp.
1053 sum_noprobs(FILEDESC *fp)
1055 static bool did_noprobs = FALSE;
1059 zero_tbl(&Noprob_tbl);
1060 while (fp != NULL) {
1062 sum_tbl(&Noprob_tbl, &fp->tbl);
1071 return (i >= j ? i : j);
1076 * Assocatiate a FILE * with the given FILEDESC.
1079 open_fp(FILEDESC *fp)
1081 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1089 * Open up the dat file if we need to.
1092 open_dat(FILEDESC *fp)
1094 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY)) < 0) {
1095 perror(fp->datfile);
1102 * Get the position from the pos file, if there is one. If not,
1103 * return a random number.
1106 get_pos(FILEDESC *fp)
1110 assert(fp->read_tbl);
1111 if (fp->pos == POS_UNKNOWN) {
1113 if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
1114 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1115 fp->pos = arc4random_uniform(fp->tbl.str_numstr);
1116 else if (fp->pos >= fp->tbl.str_numstr)
1117 fp->pos %= fp->tbl.str_numstr;
1122 fp->pos = arc4random_uniform(fp->tbl.str_numstr);
1124 if (++(fp->pos) >= fp->tbl.str_numstr)
1125 fp->pos -= fp->tbl.str_numstr;
1126 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, (long)fp->pos));
1131 * Get the tbl data file the datfile.
1134 get_tbl(FILEDESC *fp)
1141 if (fp->child == NULL) {
1142 if ((fd = open(fp->datfile, O_RDONLY)) < 0) {
1143 perror(fp->datfile);
1146 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1147 (void)fprintf(stderr,
1148 "fortune: %s corrupted\n", fp->path);
1151 /* fp->tbl.str_version = be32toh(fp->tbl.str_version); */
1152 fp->tbl.str_numstr = be32toh(fp->tbl.str_numstr);
1153 fp->tbl.str_longlen = be32toh(fp->tbl.str_longlen);
1154 fp->tbl.str_shortlen = be32toh(fp->tbl.str_shortlen);
1155 fp->tbl.str_flags = be32toh(fp->tbl.str_flags);
1160 for (child = fp->child; child != NULL; child = child->next) {
1162 sum_tbl(&fp->tbl, &child->tbl);
1165 fp->read_tbl = TRUE;
1170 * Zero out the fields we care about in a tbl structure.
1173 zero_tbl(STRFILE *tp)
1176 tp->str_longlen = 0;
1177 tp->str_shortlen = ~0;
1182 * Merge the tbl data of t2 into t1.
1185 sum_tbl(STRFILE *t1, STRFILE *t2)
1187 t1->str_numstr += t2->str_numstr;
1188 if (t1->str_longlen < t2->str_longlen)
1189 t1->str_longlen = t2->str_longlen;
1190 if (t1->str_shortlen > t2->str_shortlen)
1191 t1->str_shortlen = t2->str_shortlen;
1194 #define STR(str) ((str) == NULL ? "NULL" : (str))
1198 * Print out the file list
1201 print_file_list(void)
1203 print_list(File_list, 0);
1208 * Print out the actual list, recursively.
1211 print_list(FILEDESC *list, int lev)
1213 while (list != NULL) {
1214 fprintf(stderr, "%*s", lev * 4, "");
1215 if (list->percent == NO_PROB)
1216 fprintf(stderr, "___%%");
1218 fprintf(stderr, "%3d%%", list->percent);
1219 fprintf(stderr, " %s", STR(list->name));
1220 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1221 STR(list->datfile), STR(list->posfile)));
1222 fprintf(stderr, "\n");
1223 if (list->child != NULL)
1224 print_list(list->child, lev + 1);
1231 * Convert the pattern to an ignore-case equivalent.
1234 conv_pat(char *orig)
1240 cnt = 1; /* allow for '\0' */
1241 for (sp = orig; *sp != '\0'; sp++)
1242 if (isalpha((unsigned char)*sp))
1246 if ((new = malloc(cnt)) == NULL) {
1247 fprintf(stderr, "pattern too long for ignoring case\n");
1251 for (sp = new; *orig != '\0'; orig++) {
1252 if (islower((unsigned char)*orig)) {
1255 *sp++ = toupper((unsigned char)*orig);
1258 else if (isupper((unsigned char)*orig)) {
1261 *sp++ = tolower((unsigned char)*orig);
1274 * Find all the fortunes which match the pattern we've been given.
1279 Fort_len = maxlen_in_list(File_list);
1280 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1281 /* extra length, "%\n" is appended */
1282 Fortbuf = do_malloc((unsigned int) Fort_len + 10);
1285 matches_in_list(File_list);
1292 * Return the maximum fortune len in the file list.
1295 maxlen_in_list(FILEDESC *list)
1301 for (fp = list; fp != NULL; fp = fp->next) {
1302 if (fp->child != NULL) {
1303 if ((len = maxlen_in_list(fp->child)) > maxlen)
1308 if (fp->tbl.str_longlen > (unsigned int)maxlen)
1309 maxlen = fp->tbl.str_longlen;
1318 * Print out the matches from the files in the list.
1321 matches_in_list(FILEDESC *list)
1328 for (fp = list; fp != NULL; fp = fp->next) {
1329 if (fp->child != NULL) {
1330 matches_in_list(fp->child);
1333 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1337 while (fgets(sp, Fort_len, fp->inf) != NULL)
1338 if (fp->tbl.str_flags & STR_COMMENTS
1339 && sp[0] == fp->tbl.str_delim
1340 && sp[1] == fp->tbl.str_delim)
1342 else if (!STR_ENDSTRING(sp, fp->tbl))
1346 if (fp->tbl.str_flags & STR_ROTATED)
1347 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1350 *p = 'A' + (ch - 'A' + 13) % 26;
1351 else if (islower(ch))
1352 *p = 'a' + (ch - 'a' + 13) % 26;
1355 if (regexec(&Re_pat, Fortbuf, 0, NULL, 0) != REG_NOMATCH) {
1356 printf("%c%c", fp->tbl.str_delim,
1359 printf(" (%s)", fp->name);
1364 (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1374 (void) fprintf(stderr, "fortune [-a");
1376 (void) fprintf(stderr, "D");
1378 (void) fprintf(stderr, "efilosw]");
1379 (void) fprintf(stderr, " [-m pattern]");
1380 (void) fprintf(stderr, " [[N%%] file/directory/all]\n");
1386 * Set up file search patch from environment var FORTUNE_PATH;
1387 * if not set, use the compiled in FORTDIR.
1394 char *pch, **ppch, *str, *path;
1397 Fortune_path = getenv("FORTUNE_PATH");
1398 if (Fortune_path == NULL) {
1399 Fortune_path = FORTDIR;
1402 path = strdup(Fortune_path);
1404 for (nstr = 2, pch = path; *pch != '\0'; pch++) {
1409 ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *));
1412 str = strtok(path, ":");
1418 str = strtok(NULL, ":");
1422 if (foundenv == 1) {
1424 "fortune: FORTUNE_PATH: None of the specified "
1425 "directories found.\n");
1429 Fortune_path_arr[0] = strdup(FORTDIR);