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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char copyright[] =
40 "@(#) Copyright (c) 1986, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 # include <sys/stat.h>
52 # include <sys/endian.h>
66 # include "pathnames.h"
72 # define MINW 6 /* minimum wait if desired */
73 # define CPERS 20 /* # of chars for each sec */
74 # define SLEN 160 /* # of chars in short fortune */
76 # define POS_UNKNOWN ((uint32_t) -1) /* pos for file unknown */
77 # define NO_PROB (-1) /* no prob specified for file */
80 # define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
94 char *datfile, *posfile;
99 struct fd *child, *parent;
100 struct fd *next, *prev;
103 bool Found_one; /* did we find a match? */
104 bool Find_files = FALSE; /* just find a list of proper fortune files */
105 bool Fortunes_only = FALSE; /* check only "fortunes" files */
106 bool Wait = FALSE; /* wait desired after fortune */
107 bool Short_only = FALSE; /* short fortune desired */
108 bool Long_only = FALSE; /* long fortune desired */
109 bool Offend = FALSE; /* offensive fortunes only */
110 bool All_forts = FALSE; /* any fortune allowed */
111 bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
112 bool Match = FALSE; /* dump fortunes matching a pattern */
114 bool Debug = FALSE; /* print debug messages */
117 char *Fortbuf = NULL; /* fortune buffer for -m */
121 off_t Seekpts[2]; /* seek pointers to fortunes */
123 FILEDESC *File_list = NULL, /* Head of file list */
124 *File_tail = NULL; /* Tail of file list */
125 FILEDESC *Fortfile; /* Fortune file to use */
127 STRFILE Noprob_tbl; /* sum of data for all no prob files */
130 char **Fortune_path_arr;
132 int add_dir(FILEDESC *);
133 int add_file __P((int,
134 char *, char *, FILEDESC **, FILEDESC **, FILEDESC *));
135 void all_forts(FILEDESC *, char *);
136 char *copy(char *, u_int);
137 void display(FILEDESC *);
138 void do_free(void *);
139 void *do_malloc(u_int);
140 int form_file_list(char **, int);
143 void get_pos(FILEDESC *);
144 void get_tbl(FILEDESC *);
145 void getargs(int, char *[]);
147 void init_prob(void);
149 int is_fortfile(char *, char **, char **, int);
150 int is_off_name(char *);
154 char *off_name(char *);
155 void open_dat(FILEDESC *);
156 void open_fp(FILEDESC *);
158 pick_child(FILEDESC *);
159 void print_file_list(void);
160 void print_list(FILEDESC *, int);
161 void sum_noprobs(FILEDESC *);
162 void sum_tbl(STRFILE *, STRFILE *);
164 void zero_tbl(STRFILE *);
166 char *conv_pat(char *);
167 int find_matches(void);
168 void matches_in_list(FILEDESC *);
169 int maxlen_in_list(FILEDESC *);
171 static regex_t Re_pat;
178 #ifdef OK_TO_WRITE_DISK
180 #endif /* OK_TO_WRITE_DISK */
182 (void) setlocale(LC_ALL, "");
188 exit(find_matches() != 0);
193 } while ((Short_only && fortlen() > SLEN) ||
194 (Long_only && fortlen() <= SLEN));
198 #ifdef OK_TO_WRITE_DISK
199 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
200 perror(Fortfile->posfile);
205 * if we can, we exclusive lock, but since it isn't very
206 * important, we just punt if we don't have easy locking
209 (void) flock(fd, LOCK_EX);
211 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
212 if (!Fortfile->was_pos_file)
213 (void) chmod(Fortfile->path, 0666);
215 (void) flock(fd, LOCK_UN);
217 #endif /* OK_TO_WRITE_DISK */
221 sleep((unsigned int) max(Fort_len / CPERS, MINW));
236 (void) fseeko(fp->inf, Seekpts[0], 0);
237 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
238 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
239 if (fp->tbl.str_flags & STR_ROTATED)
240 for (p = line; (ch = *p) != '\0'; ++p) {
243 *p = 'A' + (ch - 'A' + 13) % 26;
244 else if (islower(ch))
245 *p = 'a' + (ch - 'a' + 13) % 26;
248 if (fp->tbl.str_flags & STR_COMMENTS
249 && line[0] == fp->tbl.str_delim
250 && line[1] == fp->tbl.str_delim)
254 (void) fflush(stdout);
259 * Return the length of the fortune.
267 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
268 nchar = (int)(Seekpts[1] - Seekpts[0]);
271 (void) fseeko(Fortfile->inf, Seekpts[0], 0);
273 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
274 !STR_ENDSTRING(line, Fortfile->tbl))
275 nchar += strlen(line);
282 * This routine evaluates the arguments on the command line
299 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
301 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
304 case 'a': /* any fortune */
313 Equal_probs++; /* scatter un-allocted prob equally */
315 case 'f': /* find fortune files */
318 case 'l': /* long ones only */
322 case 'o': /* offensive ones only */
325 case 's': /* short ones only */
329 case 'w': /* give time to read */
332 case 'm': /* dump out the fortunes */
336 case 'i': /* case-insensitive match */
346 if (!form_file_list(argv, argc))
347 exit(1); /* errors printed through form_file_list() */
362 error = regcomp(&Re_pat, pat, REG_BASIC);
364 fprintf(stderr, "regcomp(%s) fails\n", pat);
372 * Form the file list from the file specifications.
375 form_file_list(files, file_cnt)
385 Fortunes_only = TRUE;
386 pstr = Fortune_path_arr;
389 i += add_file(NO_PROB, *pstr++, NULL,
390 &File_list, &File_tail, NULL);
392 Fortunes_only = FALSE;
394 fprintf(stderr, "No fortunes found in %s.\n",
399 pstr = Fortune_path_arr;
402 i += add_file(NO_PROB, "fortunes", *pstr++,
403 &File_list, &File_tail, NULL);
406 fprintf(stderr, "No fortunes found in %s.\n",
412 for (i = 0; i < file_cnt; i++) {
414 if (!isdigit((unsigned char)files[i][0]))
418 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
419 percent = percent * 10 + *sp - '0';
421 fprintf(stderr, "percentages must be <= 100\n");
425 fprintf(stderr, "percentages must be integers\n");
429 * If the number isn't followed by a '%', then
430 * it was not a percentage, just the first part
431 * of a file name which starts with digits.
437 else if (*++sp == '\0') {
438 if (++i >= file_cnt) {
439 fprintf(stderr, "percentages must precede files\n");
445 if (strcmp(sp, "all") == 0) {
446 pstr = Fortune_path_arr;
449 i += add_file(NO_PROB, *pstr++, NULL,
450 &File_list, &File_tail, NULL);
453 fprintf(stderr, "No fortunes found in %s.\n",
457 } else if (!add_file(percent, sp, NULL, &File_list,
467 * Add a file to the file list.
470 add_file(percent, file, dir, head, tail, parent)
474 FILEDESC **head, **tail;
479 char *path, *offensive;
488 path = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2));
489 (void) strcat(strcat(strcpy(path, dir), "/"), file);
492 if ((isdir = is_dir(path)) && parent != NULL) {
495 return FALSE; /* don't recurse */
498 if (!isdir && parent == NULL && (All_forts || Offend) &&
499 !is_off_name(path)) {
500 offensive = off_name(path);
507 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
508 file = off_name(file);
512 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
514 if ((fd = open(path, 0)) < 0) {
516 * This is a sneak. If the user said -a, and if the
517 * file we're given isn't a file, we check to see if
518 * there is a -o version. If there is, we treat it as
519 * if *that* were the file given. We only do this for
520 * individual files -- if we're scanning a directory,
521 * we'll pick up the -o file anyway.
523 if (All_forts && offensive != NULL) {
529 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
530 file = off_name(file);
533 if (dir == NULL && file[0] != '/') {
535 char **pstr = Fortune_path_arr;
538 i += add_file(percent, file, *pstr++,
542 fprintf(stderr, "No '%s' found in %s.\n",
556 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
560 fp->percent = percent;
565 if ((isdir && !add_dir(fp)) ||
567 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
571 "fortune:%s not a fortune file or directory\n",
575 do_free(fp->datfile);
576 do_free(fp->posfile);
582 * If the user said -a, we need to make this node a pointer to
583 * both files, if there are two. We don't need to do this if
584 * we are scanning a directory, since the scan will pick up the
587 if (All_forts && parent == NULL && !is_off_name(path))
588 all_forts(fp, offensive);
591 else if (fp->percent == NO_PROB) {
601 #ifdef OK_TO_WRITE_DISK
602 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
603 #endif /* OK_TO_WRITE_DISK */
610 * Return a pointer to an initialized new FILEDESC.
617 fp = (FILEDESC *) do_malloc(sizeof *fp);
619 fp->pos = POS_UNKNOWN;
622 fp->percent = NO_PROB;
623 fp->read_tbl = FALSE;
635 * Return a pointer to the offensive version of a file of this name.
643 new = copy(file, (unsigned int) (strlen(file) + 2));
644 return strcat(new, "-o");
649 * Is the file an offensive-style name?
658 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
663 * Modify a FILEDESC element to be the parent of two children if
664 * there are two children to be a parent of.
667 all_forts(fp, offensive)
672 FILEDESC *scene, *obscene;
674 auto char *datfile, *posfile;
676 if (fp->child != NULL) /* this is a directory, not a file */
678 if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
680 if ((fd = open(offensive, 0)) < 0)
682 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
687 fp->num_children = 2;
689 scene->next = obscene;
690 obscene->next = NULL;
691 scene->child = obscene->child = NULL;
692 scene->parent = obscene->parent = fp;
695 scene->percent = obscene->percent = NO_PROB;
699 obscene->path = offensive;
700 if ((sp = rindex(offensive, '/')) == NULL)
701 obscene->name = offensive;
703 obscene->name = ++sp;
704 obscene->datfile = datfile;
705 obscene->posfile = posfile;
706 obscene->read_tbl = FALSE;
707 #ifdef OK_TO_WRITE_DISK
708 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
709 #endif /* OK_TO_WRITE_DISK */
714 * Add the contents of an entire directory.
721 struct dirent *dirent;
722 auto FILEDESC *tailp;
725 (void) close(fp->fd);
727 if ((dir = opendir(fp->path)) == NULL) {
732 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
733 fp->num_children = 0;
734 while ((dirent = readdir(dir)) != NULL) {
735 if (dirent->d_namlen == 0)
737 name = copy(dirent->d_name, dirent->d_namlen);
738 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
743 if (fp->num_children == 0) {
744 (void) fprintf(stderr,
745 "fortune: %s: No fortune files in directory.\n", fp->path);
753 * Return TRUE if the file is a directory, FALSE otherwise.
759 auto struct stat sbuf;
761 if (stat(file, &sbuf) < 0)
763 return (sbuf.st_mode & S_IFDIR);
768 * Return TRUE if the file is a fortune database file. We try and
769 * exclude files without reading them if possible to avoid
770 * overhead. Files which start with ".", or which have "illegal"
771 * suffixes, as contained in suflist[], are ruled out.
775 is_fortfile(file, datp, posp, check_for_offend)
776 char *file, **datp, **posp;
777 int check_for_offend;
782 static char *suflist[] = { /* list of "illegal" suffixes" */
783 "dat", "pos", "c", "h", "p", "i", "f",
784 "pas", "ftn", "ins.c", "ins,pas",
789 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
792 * Preclude any -o files for offendable people, and any non -o
793 * files for completely offensive people.
795 if (check_for_offend && !All_forts) {
797 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
798 DPRINTF(2, (stderr, "FALSE (offending file)\n"));
803 if ((sp = rindex(file, '/')) == NULL)
808 DPRINTF(2, (stderr, "FALSE (file starts with '.')\n"));
811 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
812 DPRINTF(2, (stderr, "FALSE (check fortunes only)\n"));
815 if ((sp = rindex(sp, '.')) != NULL) {
817 for (i = 0; suflist[i] != NULL; i++)
818 if (strcmp(sp, suflist[i]) == 0) {
819 DPRINTF(2, (stderr, "FALSE (file has suffix \".%s\")\n", sp));
824 datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
825 strcat(datfile, ".dat");
826 if (access(datfile, R_OK) < 0) {
827 DPRINTF(2, (stderr, "FALSE (no readable \".dat\" file)\n"));
830 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
840 #ifdef OK_TO_WRITE_DISK
841 *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
842 (void) strcat(*posp, ".pos");
845 #endif /* OK_TO_WRITE_DISK */
847 DPRINTF(2, (stderr, "TRUE\n"));
853 * Return a malloc()'ed copy of the string
862 new = do_malloc(len + 1);
872 * Do a malloc, checking for NULL return.
880 if ((new = malloc(size)) == NULL) {
881 (void) fprintf(stderr, "fortune: out of memory.\n");
889 * Free malloc'ed space, if any.
901 * Initialize the fortune probabilities.
906 FILEDESC *fp, *last = NULL;
907 int percent, num_noprob, frac;
910 * Distribute the residual probability (if any) across all
911 * files with unspecified probability (i.e., probability of 0)
917 for (fp = File_tail; fp != NULL; fp = fp->prev)
918 if (fp->percent == NO_PROB) {
924 percent += fp->percent;
925 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
926 percent, num_noprob));
928 (void) fprintf(stderr,
929 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
932 else if (percent < 100 && num_noprob == 0) {
933 (void) fprintf(stderr,
934 "fortune: no place to put residual probability (%d%% < 100%%)\n",
938 else if (percent == 100 && num_noprob != 0) {
939 (void) fprintf(stderr,
940 "fortune: no probability left to put in residual files (100%%)\n");
943 percent = 100 - percent;
945 if (num_noprob != 0) {
946 if (num_noprob > 1) {
947 frac = percent / num_noprob;
948 DPRINTF(1, (stderr, ", frac = %d%%", frac));
949 for (fp = File_tail; fp != last; fp = fp->prev)
950 if (fp->percent == NO_PROB) {
955 last->percent = percent;
956 DPRINTF(1, (stderr, ", residual = %d%%", percent));
960 ", %d%% distributed over remaining fortunes\n",
963 DPRINTF(1, (stderr, "\n"));
973 * Get the fortune data file's seek pointer for the next fortune.
981 if (File_list->next == NULL || File_list->percent == NO_PROB)
984 choice = arc4random_uniform(100);
985 DPRINTF(1, (stderr, "choice = %d\n", choice));
986 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
987 if (choice < fp->percent)
990 choice -= fp->percent;
992 " skip \"%s\", %d%% (choice = %d)\n",
993 fp->name, fp->percent, choice));
996 "using \"%s\", %d%% (choice = %d)\n",
997 fp->name, fp->percent, choice));
999 if (fp->percent != NO_PROB)
1002 if (fp->next != NULL) {
1004 choice = arc4random_uniform(Noprob_tbl.str_numstr);
1005 DPRINTF(1, (stderr, "choice = %d (of %u) \n", choice,
1006 Noprob_tbl.str_numstr));
1007 while (choice >= fp->tbl.str_numstr) {
1008 choice -= fp->tbl.str_numstr;
1011 " skip \"%s\", %u (choice = %d)\n",
1012 fp->name, fp->tbl.str_numstr,
1015 DPRINTF(1, (stderr, "using \"%s\", %u\n", fp->name,
1016 fp->tbl.str_numstr));
1020 if (fp->child != NULL) {
1021 DPRINTF(1, (stderr, "picking child\n"));
1022 fp = pick_child(fp);
1027 (void) lseek(fp->datfd,
1028 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0);
1029 read(fp->datfd, Seekpts, sizeof Seekpts);
1030 Seekpts[0] = be64toh(Seekpts[0]);
1031 Seekpts[1] = be64toh(Seekpts[1]);
1036 * Pick a child from a chosen parent.
1046 choice = arc4random_uniform(parent->num_children);
1047 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1048 choice, parent->num_children));
1049 for (fp = parent->child; choice--; fp = fp->next)
1051 DPRINTF(1, (stderr, " using %s\n", fp->name));
1056 choice = arc4random_uniform(parent->tbl.str_numstr);
1057 DPRINTF(1, (stderr, " choice = %d (of %u)\n",
1058 choice, parent->tbl.str_numstr));
1059 for (fp = parent->child; choice >= fp->tbl.str_numstr;
1061 choice -= fp->tbl.str_numstr;
1062 DPRINTF(1, (stderr, "\tskip %s, %u (choice = %d)\n",
1063 fp->name, fp->tbl.str_numstr, choice));
1065 DPRINTF(1, (stderr, " using %s, %u\n", fp->name,
1066 fp->tbl.str_numstr));
1073 * Sum up all the noprob probabilities, starting with fp.
1079 static bool did_noprobs = FALSE;
1083 zero_tbl(&Noprob_tbl);
1084 while (fp != NULL) {
1086 sum_tbl(&Noprob_tbl, &fp->tbl);
1096 return (i >= j ? i : j);
1101 * Assocatiate a FILE * with the given FILEDESC.
1107 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1115 * Open up the dat file if we need to.
1121 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, 0)) < 0) {
1122 perror(fp->datfile);
1129 * Get the position from the pos file, if there is one. If not,
1130 * return a random number.
1136 #ifdef OK_TO_WRITE_DISK
1138 #endif /* OK_TO_WRITE_DISK */
1140 assert(fp->read_tbl);
1141 if (fp->pos == POS_UNKNOWN) {
1142 #ifdef OK_TO_WRITE_DISK
1143 if ((fd = open(fp->posfile, 0)) < 0 ||
1144 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1145 fp->pos = arc4random_uniform(fp->tbl.str_numstr);
1146 else if (fp->pos >= fp->tbl.str_numstr)
1147 fp->pos %= fp->tbl.str_numstr;
1151 fp->pos = arc4random_uniform(fp->tbl.str_numstr);
1152 #endif /* OK_TO_WRITE_DISK */
1154 if (++(fp->pos) >= fp->tbl.str_numstr)
1155 fp->pos -= fp->tbl.str_numstr;
1156 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, (long)fp->pos));
1161 * Get the tbl data file the datfile.
1172 if (fp->child == NULL) {
1173 if ((fd = open(fp->datfile, 0)) < 0) {
1174 perror(fp->datfile);
1177 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1178 (void)fprintf(stderr,
1179 "fortune: %s corrupted\n", fp->path);
1182 /* fp->tbl.str_version = be32toh(fp->tbl.str_version); */
1183 fp->tbl.str_numstr = be32toh(fp->tbl.str_numstr);
1184 fp->tbl.str_longlen = be32toh(fp->tbl.str_longlen);
1185 fp->tbl.str_shortlen = be32toh(fp->tbl.str_shortlen);
1186 fp->tbl.str_flags = be32toh(fp->tbl.str_flags);
1191 for (child = fp->child; child != NULL; child = child->next) {
1193 sum_tbl(&fp->tbl, &child->tbl);
1196 fp->read_tbl = TRUE;
1201 * Zero out the fields we care about in a tbl structure.
1208 tp->str_longlen = 0;
1209 tp->str_shortlen = ~0;
1214 * Merge the tbl data of t2 into t1.
1220 t1->str_numstr += t2->str_numstr;
1221 if (t1->str_longlen < t2->str_longlen)
1222 t1->str_longlen = t2->str_longlen;
1223 if (t1->str_shortlen > t2->str_shortlen)
1224 t1->str_shortlen = t2->str_shortlen;
1227 #define STR(str) ((str) == NULL ? "NULL" : (str))
1231 * Print out the file list
1236 print_list(File_list, 0);
1241 * Print out the actual list, recursively.
1244 print_list(list, lev)
1248 while (list != NULL) {
1249 fprintf(stderr, "%*s", lev * 4, "");
1250 if (list->percent == NO_PROB)
1251 fprintf(stderr, "___%%");
1253 fprintf(stderr, "%3d%%", list->percent);
1254 fprintf(stderr, " %s", STR(list->name));
1255 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1256 STR(list->datfile), STR(list->posfile)));
1257 fprintf(stderr, "\n");
1258 if (list->child != NULL)
1259 print_list(list->child, lev + 1);
1266 * Convert the pattern to an ignore-case equivalent.
1276 cnt = 1; /* allow for '\0' */
1277 for (sp = orig; *sp != '\0'; sp++)
1278 if (isalpha((unsigned char)*sp))
1282 if ((new = malloc(cnt)) == NULL) {
1283 fprintf(stderr, "pattern too long for ignoring case\n");
1287 for (sp = new; *orig != '\0'; orig++) {
1288 if (islower((unsigned char)*orig)) {
1291 *sp++ = toupper((unsigned char)*orig);
1294 else if (isupper((unsigned char)*orig)) {
1297 *sp++ = tolower((unsigned char)*orig);
1309 * Find all the fortunes which match the pattern we've been given.
1314 Fort_len = maxlen_in_list(File_list);
1315 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1316 /* extra length, "%\n" is appended */
1317 Fortbuf = do_malloc((unsigned int) Fort_len + 10);
1320 matches_in_list(File_list);
1327 * Return the maximum fortune len in the file list.
1330 maxlen_in_list(list)
1337 for (fp = list; fp != NULL; fp = fp->next) {
1338 if (fp->child != NULL) {
1339 if ((len = maxlen_in_list(fp->child)) > maxlen)
1344 if (fp->tbl.str_longlen > maxlen)
1345 maxlen = fp->tbl.str_longlen;
1353 * Print out the matches from the files in the list.
1356 matches_in_list(list)
1364 for (fp = list; fp != NULL; fp = fp->next) {
1365 if (fp->child != NULL) {
1366 matches_in_list(fp->child);
1369 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1373 while (fgets(sp, Fort_len, fp->inf) != NULL)
1374 if (fp->tbl.str_flags & STR_COMMENTS
1375 && sp[0] == fp->tbl.str_delim
1376 && sp[1] == fp->tbl.str_delim)
1378 else if (!STR_ENDSTRING(sp, fp->tbl))
1382 if (fp->tbl.str_flags & STR_ROTATED)
1383 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1386 *p = 'A' + (ch - 'A' + 13) % 26;
1387 else if (islower(ch))
1388 *p = 'a' + (ch - 'a' + 13) % 26;
1391 if (regexec(&Re_pat, Fortbuf, 0, NULL, 0) != REG_NOMATCH) {
1392 printf("%c%c", fp->tbl.str_delim,
1395 printf(" (%s)", fp->name);
1400 (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1410 (void) fprintf(stderr, "fortune [-a");
1412 (void) fprintf(stderr, "D");
1414 (void) fprintf(stderr, "efilosw]");
1415 (void) fprintf(stderr, " [-m pattern]");
1416 (void) fprintf(stderr, " [[N%%] file/directory/all]\n");
1422 * Set up file search patch from environment var FORTUNE_PATH;
1423 * if not set, use the compiled in FORTDIR.
1430 char *pch, **ppch, *str, *path;
1433 Fortune_path = getenv("FORTUNE_PATH");
1434 if (Fortune_path == NULL) {
1435 Fortune_path = FORTDIR;
1438 path = strdup(Fortune_path);
1440 for (nstr = 2, pch = path; *pch != '\0'; pch++) {
1445 ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *));
1448 str = strtok(path, ":");
1454 str = strtok(NULL, ":");
1458 if (foundenv == 1) {
1460 "fortune: FORTUNE_PATH: None of the specified "
1461 "directories found.\n");
1465 Fortune_path_arr[0] = FORTDIR;