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
32 * @(#) Copyright (c) 1986, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)fortune.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: head/usr.bin/fortune/fortune/fortune.c 259057 2013-12-07 02:20:22Z marcel $
37 #include <sys/param.h>
39 #include <sys/endian.h>
56 #include "pathnames.h"
58 #define MINW 6 /* minimum wait if desired */
59 #define CPERS 20 /* # of chars for each sec */
60 #define SLEN 160 /* # of chars in short fortune */
62 #define POS_UNKNOWN ((uint32_t)-1) /* pos for file unknown */
63 #define NO_PROB (-1) /* no prob specified for file */
66 #define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
80 char *datfile, *posfile;
85 struct fd *child, *parent;
86 struct fd *next, *prev;
89 static bool Found_one; /* did we find a match? */
90 static bool Find_files = false; /* just find a list of proper fortune files */
91 static bool Fortunes_only = false; /* check only "fortunes" files */
92 static bool Wait = false; /* wait desired after fortune */
93 static bool Short_only = false; /* short fortune desired */
94 static bool Long_only = false; /* long fortune desired */
95 static bool Offend = false; /* offensive fortunes only */
96 static bool All_forts = false; /* any fortune allowed */
97 static bool Equal_probs = false; /* scatter un-allocted prob equally */
98 static bool Match = false; /* dump fortunes matching a pattern */
99 static bool WriteToDisk = false; /* use files on disk to save state */
101 static int Debug = 0; /* print debug messages */
104 static char *Fortbuf = NULL; /* fortune buffer for -m */
106 static int Fort_len = 0;
108 static off_t Seekpts[2]; /* seek pointers to fortunes */
110 static FILEDESC *File_list = NULL; /* Head of file list */
111 static FILEDESC *File_tail = NULL; /* Tail of file list */
112 static FILEDESC *Fortfile; /* Fortune file to use */
114 static STRFILE Noprob_tbl; /* sum of data for all no prob files */
116 static const char *Fortune_path;
117 static char **Fortune_path_arr;
119 static bool add_dir(FILEDESC *);
120 static int add_file(int, const char *, const char *,
121 FILEDESC **, FILEDESC **, FILEDESC *);
122 static void all_forts(FILEDESC *, char *);
123 static char *copy(const char *, u_int);
124 static void display(FILEDESC *);
125 static void do_free(void *);
126 static void *do_malloc(u_int);
127 static bool form_file_list(char **, int);
128 static int fortlen(void);
129 static void get_fort(void);
130 static void get_pos(FILEDESC *);
131 static void get_tbl(FILEDESC *);
132 static void getargs(int, char *[]);
133 static void getpath(void);
134 static void init_prob(void);
135 static bool is_dir(const char *);
136 static bool is_fortfile(const char *, char **, char **, int);
137 static bool is_off_name(const char *);
138 static int max(int, int);
139 static FILEDESC *new_fp(void);
140 static char *off_name(const char *);
141 static void open_dat(FILEDESC *);
142 static void open_fp(FILEDESC *);
143 static FILEDESC *pick_child(FILEDESC *);
144 static void print_file_list(void);
145 static void print_list(FILEDESC *, int);
146 static void sum_noprobs(FILEDESC *);
147 static void sum_tbl(STRFILE *, STRFILE *);
148 static void usage(void);
149 static void zero_tbl(STRFILE *);
151 static char *conv_pat(char *);
152 static int find_matches(void);
153 static void matches_in_list(FILEDESC *);
154 static int maxlen_in_list(FILEDESC *);
156 static regex_t Re_pat;
159 main(int argc, char *argv[])
163 if (getenv("FORTUNE_SAVESTATE") != NULL)
166 setlocale(LC_ALL, "");
172 exit(find_matches() != 0);
177 } while ((Short_only && fortlen() > SLEN) ||
178 (Long_only && fortlen() <= SLEN));
183 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
184 perror(Fortfile->posfile);
188 * if we can, we exclusive lock, but since it isn't very
189 * important, we just punt if we don't have easy locking
193 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
194 if (!Fortfile->was_pos_file)
195 chmod(Fortfile->path, 0666);
202 sleep((unsigned int)max(Fort_len / CPERS, MINW));
209 display(FILEDESC *fp)
216 fseeko(fp->inf, Seekpts[0], SEEK_SET);
217 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
218 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
219 if (fp->tbl.str_flags & STR_ROTATED)
220 for (p = line; (ch = *p) != '\0'; ++p) {
223 *p = 'A' + (ch - 'A' + 13) % 26;
224 else if (islower(ch))
225 *p = 'a' + (ch - 'a' + 13) % 26;
228 if (fp->tbl.str_flags & STR_COMMENTS
229 && line[0] == fp->tbl.str_delim
230 && line[1] == fp->tbl.str_delim)
239 * Return the length of the fortune.
247 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
248 nchar = (int)(Seekpts[1] - Seekpts[0]);
251 fseeko(Fortfile->inf, Seekpts[0], SEEK_SET);
253 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
254 !STR_ENDSTRING(line, Fortfile->tbl))
255 nchar += strlen(line);
263 * This routine evaluates the arguments on the command line
266 getargs(int argc, char *argv[])
276 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
278 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
281 case 'a': /* any fortune */
290 Equal_probs++; /* scatter un-allocted prob equally */
292 case 'f': /* find fortune files */
295 case 'l': /* long ones only */
299 case 'o': /* offensive ones only */
302 case 's': /* short ones only */
306 case 'w': /* give time to read */
309 case 'm': /* dump out the fortunes */
313 case 'i': /* case-insensitive match */
323 if (!form_file_list(argv, argc))
324 exit(1); /* errors printed through form_file_list() */
339 error = regcomp(&Re_pat, pat, REG_BASIC);
341 fprintf(stderr, "regcomp(%s) fails\n", pat);
349 * Form the file list from the file specifications.
352 form_file_list(char **files, int file_cnt)
361 Fortunes_only = true;
362 pstr = Fortune_path_arr;
365 k += add_file(NO_PROB, *pstr++, NULL,
366 &File_list, &File_tail, NULL);
368 Fortunes_only = false;
370 fprintf(stderr, "No fortunes found in %s.\n",
375 pstr = Fortune_path_arr;
378 k += add_file(NO_PROB, "fortunes", *pstr++,
379 &File_list, &File_tail, NULL);
382 fprintf(stderr, "No fortunes found in %s.\n",
388 for (i = 0; i < file_cnt; i++) {
390 if (!isdigit((unsigned char)files[i][0]))
394 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
395 percent = percent * 10 + *sp - '0';
397 fprintf(stderr, "percentages must be <= 100\n");
401 fprintf(stderr, "percentages must be integers\n");
405 * If the number isn't followed by a '%', then
406 * it was not a percentage, just the first part
407 * of a file name which starts with digits.
413 else if (*++sp == '\0') {
414 if (++i >= file_cnt) {
415 fprintf(stderr, "percentages must precede files\n");
421 if (strcmp(sp, "all") == 0) {
422 pstr = Fortune_path_arr;
425 k += add_file(NO_PROB, *pstr++, NULL,
426 &File_list, &File_tail, NULL);
429 fprintf(stderr, "No fortunes found in %s.\n",
433 } else if (!add_file(percent, sp, NULL, &File_list,
444 * Add a file to the file list.
447 add_file(int percent, const char *file, const char *dir,
448 FILEDESC **head, FILEDESC **tail, FILEDESC *parent)
453 char *tpath, *offensive;
463 tpath = do_malloc((unsigned int)(strlen(dir) + strlen(file) + 2));
464 strcat(strcat(strcpy(tpath, dir), "/"), file);
468 if ((isdir = is_dir(path)) && parent != NULL) {
471 return (0); /* don't recurse */
474 if (!isdir && parent == NULL && (All_forts || Offend) &&
475 !is_off_name(path)) {
476 offensive = off_name(path);
480 path = tpath = offensive;
483 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
484 file = off_name(file);
488 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
490 if ((fd = open(path, O_RDONLY)) < 0) {
492 * This is a sneak. If the user said -a, and if the
493 * file we're given isn't a file, we check to see if
494 * there is a -o version. If there is, we treat it as
495 * if *that* were the file given. We only do this for
496 * individual files -- if we're scanning a directory,
497 * we'll pick up the -o file anyway.
499 if (All_forts && offensive != NULL) {
502 path = tpath = offensive;
505 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
506 file = off_name(file);
509 if (dir == NULL && file[0] != '/') {
511 char **pstr = Fortune_path_arr;
514 i += add_file(percent, file, *pstr++,
518 fprintf(stderr, "No '%s' found in %s.\n",
532 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
536 fp->percent = percent;
541 if ((isdir && !add_dir(fp)) ||
543 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
547 "fortune:%s not a fortune file or directory\n",
551 do_free(fp->datfile);
552 do_free(fp->posfile);
558 * If the user said -a, we need to make this node a pointer to
559 * both files, if there are two. We don't need to do this if
560 * we are scanning a directory, since the scan will pick up the
563 if (All_forts && parent == NULL && !is_off_name(path))
564 all_forts(fp, offensive);
567 else if (fp->percent == NO_PROB) {
578 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
585 * Return a pointer to an initialized new FILEDESC.
592 fp = do_malloc(sizeof(*fp));
594 fp->pos = POS_UNKNOWN;
597 fp->percent = NO_PROB;
598 fp->read_tbl = false;
611 * Return a pointer to the offensive version of a file of this name.
614 off_name(const char *file)
618 new = copy(file, (unsigned int)(strlen(file) + 2));
620 return (strcat(new, "-o"));
625 * Is the file an offensive-style name?
628 is_off_name(const char *file)
634 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
639 * Modify a FILEDESC element to be the parent of two children if
640 * there are two children to be a parent of.
643 all_forts(FILEDESC *fp, char *offensive)
646 FILEDESC *scene, *obscene;
648 char *datfile, *posfile;
650 if (fp->child != NULL) /* this is a directory, not a file */
652 if (!is_fortfile(offensive, &datfile, &posfile, false))
654 if ((fd = open(offensive, O_RDONLY)) < 0)
656 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
661 fp->num_children = 2;
663 scene->next = obscene;
664 obscene->next = NULL;
665 scene->child = obscene->child = NULL;
666 scene->parent = obscene->parent = fp;
669 scene->percent = obscene->percent = NO_PROB;
673 obscene->path = offensive;
674 if ((sp = strrchr(offensive, '/')) == NULL)
675 obscene->name = offensive;
677 obscene->name = ++sp;
678 obscene->datfile = datfile;
679 obscene->posfile = posfile;
680 obscene->read_tbl = false;
682 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
687 * Add the contents of an entire directory.
690 add_dir(FILEDESC *fp)
693 struct dirent *dirent;
699 if ((dir = opendir(fp->path)) == NULL) {
704 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
705 fp->num_children = 0;
706 while ((dirent = readdir(dir)) != NULL) {
707 if (dirent->d_namlen == 0)
709 name = copy(dirent->d_name, dirent->d_namlen);
710 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
715 if (fp->num_children == 0) {
717 "fortune: %s: No fortune files in directory.\n", fp->path);
726 * Return true if the file is a directory, false otherwise.
729 is_dir(const char *file)
733 if (stat(file, &sbuf) < 0)
736 return (sbuf.st_mode & S_IFDIR);
741 * Return true if the file is a fortune database file. We try and
742 * exclude files without reading them if possible to avoid
743 * overhead. Files which start with ".", or which have "illegal"
744 * suffixes, as contained in suflist[], are ruled out.
748 is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
753 static const char *suflist[] = {
754 /* list of "illegal" suffixes" */
755 "dat", "pos", "c", "h", "p", "i", "f",
756 "pas", "ftn", "ins.c", "ins,pas",
761 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
764 * Preclude any -o files for offendable people, and any non -o
765 * files for completely offensive people.
767 if (check_for_offend && !All_forts) {
769 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
770 DPRINTF(2, (stderr, "false (offending file)\n"));
775 if ((sp = strrchr(file, '/')) == NULL)
780 DPRINTF(2, (stderr, "false (file starts with '.')\n"));
783 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
784 DPRINTF(2, (stderr, "false (check fortunes only)\n"));
787 if ((sp = strrchr(sp, '.')) != NULL) {
789 for (i = 0; suflist[i] != NULL; i++)
790 if (strcmp(sp, suflist[i]) == 0) {
791 DPRINTF(2, (stderr, "false (file has suffix \".%s\")\n", sp));
796 datfile = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */
797 strcat(datfile, ".dat");
798 if (access(datfile, R_OK) < 0) {
799 DPRINTF(2, (stderr, "false (no readable \".dat\" file)\n"));
802 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
813 *posp = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */
814 strcat(*posp, ".pos");
819 DPRINTF(2, (stderr, "true\n"));
826 * Return a malloc()'ed copy of the string
829 copy(const char *str, unsigned int len)
833 new = do_malloc(len + 1);
844 * Do a malloc, checking for NULL return.
847 do_malloc(unsigned int size)
851 if ((new = malloc(size)) == NULL) {
852 fprintf(stderr, "fortune: out of memory.\n");
861 * Free malloc'ed space, if any.
872 * Initialize the fortune probabilities.
877 FILEDESC *fp, *last = NULL;
878 int percent, num_noprob, frac;
881 * Distribute the residual probability (if any) across all
882 * files with unspecified probability (i.e., probability of 0)
888 for (fp = File_tail; fp != NULL; fp = fp->prev)
889 if (fp->percent == NO_PROB) {
894 percent += fp->percent;
895 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
896 percent, num_noprob));
899 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
901 } else if (percent < 100 && num_noprob == 0) {
903 "fortune: no place to put residual probability (%d%% < 100%%)\n",
906 } else if (percent == 100 && num_noprob != 0) {
908 "fortune: no probability left to put in residual files (100%%)\n");
911 percent = 100 - percent;
913 if (num_noprob != 0) {
914 if (num_noprob > 1) {
915 frac = percent / num_noprob;
916 DPRINTF(1, (stderr, ", frac = %d%%", frac));
917 for (fp = File_tail; fp != last; fp = fp->prev)
918 if (fp->percent == NO_PROB) {
923 last->percent = percent;
924 DPRINTF(1, (stderr, ", residual = %d%%", percent));
927 ", %d%% distributed over remaining fortunes\n",
931 DPRINTF(1, (stderr, "\n"));
941 * Get the fortune data file's seek pointer for the next fortune.
949 if (File_list->next == NULL || File_list->percent == NO_PROB)
952 choice = arc4random_uniform(100);
953 DPRINTF(1, (stderr, "choice = %d\n", choice));
954 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
955 if (choice < fp->percent)
958 choice -= fp->percent;
960 " skip \"%s\", %d%% (choice = %d)\n",
961 fp->name, fp->percent, choice));
963 DPRINTF(1, (stderr, "using \"%s\", %d%% (choice = %d)\n",
964 fp->name, fp->percent, choice));
966 if (fp->percent != NO_PROB)
969 if (fp->next != NULL) {
971 choice = arc4random_uniform(Noprob_tbl.str_numstr);
972 DPRINTF(1, (stderr, "choice = %d (of %u) \n", choice,
973 Noprob_tbl.str_numstr));
974 while ((unsigned int)choice >= fp->tbl.str_numstr) {
975 choice -= fp->tbl.str_numstr;
978 " skip \"%s\", %u (choice = %d)\n",
979 fp->name, fp->tbl.str_numstr,
982 DPRINTF(1, (stderr, "using \"%s\", %u\n", fp->name,
983 fp->tbl.str_numstr));
987 if (fp->child != NULL) {
988 DPRINTF(1, (stderr, "picking child\n"));
995 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), SEEK_SET);
996 read(fp->datfd, Seekpts, sizeof Seekpts);
997 Seekpts[0] = be64toh(Seekpts[0]);
998 Seekpts[1] = be64toh(Seekpts[1]);
1003 * Pick a child from a chosen parent.
1006 pick_child(FILEDESC *parent)
1012 choice = arc4random_uniform(parent->num_children);
1013 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1014 choice, parent->num_children));
1015 for (fp = parent->child; choice--; fp = fp->next)
1017 DPRINTF(1, (stderr, " using %s\n", fp->name));
1022 choice = arc4random_uniform(parent->tbl.str_numstr);
1023 DPRINTF(1, (stderr, " choice = %d (of %u)\n",
1024 choice, parent->tbl.str_numstr));
1025 for (fp = parent->child; (unsigned int)choice >= fp->tbl.str_numstr;
1027 choice -= fp->tbl.str_numstr;
1028 DPRINTF(1, (stderr, "\tskip %s, %u (choice = %d)\n",
1029 fp->name, fp->tbl.str_numstr, choice));
1031 DPRINTF(1, (stderr, " using %s, %u\n", fp->name,
1032 fp->tbl.str_numstr));
1039 * Sum up all the noprob probabilities, starting with fp.
1042 sum_noprobs(FILEDESC *fp)
1044 static bool did_noprobs = false;
1048 zero_tbl(&Noprob_tbl);
1049 while (fp != NULL) {
1051 sum_tbl(&Noprob_tbl, &fp->tbl);
1060 return (i >= j ? i : j);
1065 * Assocatiate a FILE * with the given FILEDESC.
1068 open_fp(FILEDESC *fp)
1070 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1078 * Open up the dat file if we need to.
1081 open_dat(FILEDESC *fp)
1083 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY)) < 0) {
1084 perror(fp->datfile);
1091 * Get the position from the pos file, if there is one. If not,
1092 * return a random number.
1095 get_pos(FILEDESC *fp)
1099 assert(fp->read_tbl);
1100 if (fp->pos == POS_UNKNOWN) {
1102 if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
1103 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1104 fp->pos = arc4random_uniform(fp->tbl.str_numstr);
1105 else if ((unsigned int)fp->pos >= fp->tbl.str_numstr)
1106 fp->pos %= fp->tbl.str_numstr;
1110 fp->pos = arc4random_uniform(fp->tbl.str_numstr);
1113 if ((unsigned int)++(fp->pos) >= fp->tbl.str_numstr)
1114 fp->pos -= fp->tbl.str_numstr;
1115 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, (long)fp->pos));
1120 * Get the tbl data file the datfile.
1123 get_tbl(FILEDESC *fp)
1130 if (fp->child == NULL) {
1131 if ((fd = open(fp->datfile, O_RDONLY)) < 0) {
1132 perror(fp->datfile);
1135 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1137 "fortune: %s corrupted\n", fp->path);
1140 /* fp->tbl.str_version = be32toh(fp->tbl.str_version); */
1141 fp->tbl.str_numstr = be32toh(fp->tbl.str_numstr);
1142 fp->tbl.str_longlen = be32toh(fp->tbl.str_longlen);
1143 fp->tbl.str_shortlen = be32toh(fp->tbl.str_shortlen);
1144 fp->tbl.str_flags = be32toh(fp->tbl.str_flags);
1149 for (child = fp->child; child != NULL; child = child->next) {
1151 sum_tbl(&fp->tbl, &child->tbl);
1154 fp->read_tbl = true;
1159 * Zero out the fields we care about in a tbl structure.
1162 zero_tbl(STRFILE *tp)
1165 tp->str_longlen = 0;
1166 tp->str_shortlen = ~0;
1171 * Merge the tbl data of t2 into t1.
1174 sum_tbl(STRFILE *t1, STRFILE *t2)
1176 t1->str_numstr += t2->str_numstr;
1177 if (t1->str_longlen < t2->str_longlen)
1178 t1->str_longlen = t2->str_longlen;
1179 if (t1->str_shortlen > t2->str_shortlen)
1180 t1->str_shortlen = t2->str_shortlen;
1183 #define STR(str) ((str) == NULL ? "NULL" : (str))
1187 * Print out the file list
1190 print_file_list(void)
1192 print_list(File_list, 0);
1197 * Print out the actual list, recursively.
1200 print_list(FILEDESC *list, int lev)
1202 while (list != NULL) {
1203 fprintf(stderr, "%*s", lev * 4, "");
1204 if (list->percent == NO_PROB)
1205 fprintf(stderr, "___%%");
1207 fprintf(stderr, "%3d%%", list->percent);
1208 fprintf(stderr, " %s", STR(list->name));
1209 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1210 STR(list->datfile), STR(list->posfile)));
1211 fprintf(stderr, "\n");
1212 if (list->child != NULL)
1213 print_list(list->child, lev + 1);
1220 * Convert the pattern to an ignore-case equivalent.
1223 conv_pat(char *orig)
1229 cnt = 1; /* allow for '\0' */
1230 for (sp = orig; *sp != '\0'; sp++)
1231 if (isalpha((unsigned char)*sp))
1235 if ((new = malloc(cnt)) == NULL) {
1236 fprintf(stderr, "pattern too long for ignoring case\n");
1240 for (sp = new; *orig != '\0'; orig++) {
1241 if (islower((unsigned char)*orig)) {
1244 *sp++ = toupper((unsigned char)*orig);
1247 else if (isupper((unsigned char)*orig)) {
1250 *sp++ = tolower((unsigned char)*orig);
1263 * Find all the fortunes which match the pattern we've been given.
1268 Fort_len = maxlen_in_list(File_list);
1269 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1270 /* extra length, "%\n" is appended */
1271 Fortbuf = do_malloc((unsigned int)Fort_len + 10);
1274 matches_in_list(File_list);
1281 * Return the maximum fortune len in the file list.
1284 maxlen_in_list(FILEDESC *list)
1290 for (fp = list; fp != NULL; fp = fp->next) {
1291 if (fp->child != NULL) {
1292 if ((len = maxlen_in_list(fp->child)) > maxlen)
1297 if (fp->tbl.str_longlen > (unsigned int)maxlen)
1298 maxlen = fp->tbl.str_longlen;
1307 * Print out the matches from the files in the list.
1310 matches_in_list(FILEDESC *list)
1317 for (fp = list; fp != NULL; fp = fp->next) {
1318 if (fp->child != NULL) {
1319 matches_in_list(fp->child);
1322 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1326 while (fgets(sp, Fort_len, fp->inf) != NULL)
1327 if (fp->tbl.str_flags & STR_COMMENTS
1328 && sp[0] == fp->tbl.str_delim
1329 && sp[1] == fp->tbl.str_delim)
1331 else if (!STR_ENDSTRING(sp, fp->tbl))
1335 if (fp->tbl.str_flags & STR_ROTATED)
1336 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1339 *p = 'A' + (ch - 'A' + 13) % 26;
1340 else if (islower(ch))
1341 *p = 'a' + (ch - 'a' + 13) % 26;
1344 if (regexec(&Re_pat, Fortbuf, 0, NULL, 0) != REG_NOMATCH) {
1345 printf("%c%c", fp->tbl.str_delim,
1348 printf(" (%s)", fp->name);
1353 fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1363 fprintf(stderr, "fortune [-a");
1365 fprintf(stderr, "D");
1367 fprintf(stderr, "efilosw]");
1368 fprintf(stderr, " [-m pattern]");
1369 fprintf(stderr, " [[N%%] file/directory/all]\n");
1375 * Set up file search patch from environment var FORTUNE_PATH;
1376 * if not set, use the compiled in FORTDIR.
1383 char *pch, **ppch, *str, *path;
1386 Fortune_path = getenv("FORTUNE_PATH");
1387 if (Fortune_path == NULL) {
1388 Fortune_path = FORTDIR;
1391 path = strdup(Fortune_path);
1393 for (nstr = 2, pch = path; *pch != '\0'; pch++) {
1398 ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *));
1401 str = strtok(path, ":");
1407 str = strtok(NULL, ":");
1411 if (foundenv == 1) {
1413 "fortune: FORTUNE_PATH: None of the specified "
1414 "directories found.\n");
1418 Fortune_path_arr[0] = strdup(FORTDIR);