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: src/games/fortune/fortune/fortune.c,v 1.18.2.1 2001/07/02 00:35:27 dd Exp $
37 #include <sys/param.h>
39 #include <netinet/in.h>
55 #include "pathnames.h"
57 #define MINW 6 /* minimum wait if desired */
58 #define CPERS 20 /* # of chars for each sec */
59 #define SLEN 160 /* # of chars in short fortune */
61 #define POS_UNKNOWN ((long)-1) /* pos for file unknown */
62 #define NO_PROB (-1) /* no prob specified for file */
65 #define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
79 char *datfile, *posfile;
84 struct fd *child, *parent;
85 struct fd *next, *prev;
88 bool Found_one; /* did we find a match? */
89 bool Find_files = false; /* just find a list of proper fortune files */
90 bool Fortunes_only = false; /* check only "fortunes" files */
91 bool Wait = false; /* wait desired after fortune */
92 bool Short_only = false; /* short fortune desired */
93 bool Long_only = false; /* long fortune desired */
94 bool Offend = false; /* offensive fortunes only */
95 bool All_forts = false; /* any fortune allowed */
96 bool Equal_probs= false; /* scatter un-allocted prob equally */
98 bool Match = false; /* dump fortunes matching a pattern */
101 bool Debug = false; /* print debug messages */
104 char *Fortbuf = NULL; /* fortune buffer for -m */
108 long Seekpts[2]; /* seek pointers to fortunes */
110 FILEDESC *File_list = NULL, /* Head of file list */
111 *File_tail = NULL; /* Tail of file list */
112 FILEDESC *Fortfile; /* Fortune file to use */
114 STRFILE Noprob_tbl; /* sum of data for all no prob files */
116 bool add_dir(FILEDESC *);
118 const char *, const char *, FILEDESC **, FILEDESC **, FILEDESC *);
119 void all_forts(FILEDESC *, char *);
120 char *copy(const char *, u_int);
121 void display(FILEDESC *);
122 void do_free(void *);
123 void *do_malloc(u_int);
124 bool form_file_list(char **, int);
127 void get_pos(FILEDESC *);
128 void get_tbl(FILEDESC *);
129 void getargs(int, char *[]);
130 void init_prob(void);
131 bool is_dir(const char *);
132 bool is_fortfile(const char *, char **, char **, int);
133 bool is_off_name(const char *);
135 FILEDESC *new_fp(void);
136 char *off_name(const char *);
137 void open_dat(FILEDESC *);
138 void open_fp(FILEDESC *);
139 FILEDESC *pick_child(FILEDESC *);
140 void print_file_list(void);
141 void print_list(FILEDESC *, int);
142 void sum_noprobs(FILEDESC *);
143 void sum_tbl(STRFILE *, STRFILE *);
145 void zero_tbl(STRFILE *);
148 char *conv_pat(char *);
149 int find_matches(void);
150 void matches_in_list(FILEDESC *);
151 int maxlen_in_list(FILEDESC *);
156 #define RE_COMP(p) (Re_pat = regcmp(p, NULL))
157 #define BAD_COMP(f) ((f) == NULL)
158 #define RE_EXEC(p) regex(Re_pat, (p))
161 char *regcmp(), *regex();
163 #define RE_COMP(p) (p = re_comp(p))
164 #define BAD_COMP(f) ((f) != NULL)
165 #define RE_EXEC(p) re_exec(p)
170 main(int ac, char *av[])
172 #ifdef OK_TO_WRITE_DISK
174 #endif /* OK_TO_WRITE_DISK */
176 setlocale(LC_ALL, "");
182 exit(find_matches() != 0);
189 } while ((Short_only && fortlen() > SLEN) ||
190 (Long_only && fortlen() <= SLEN));
194 #ifdef OK_TO_WRITE_DISK
195 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
196 perror(Fortfile->posfile);
201 * if we can, we exclusive lock, but since it isn't very
202 * important, we just punt if we don't have easy locking
207 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
208 if (!Fortfile->was_pos_file)
209 chmod(Fortfile->path, 0666);
213 #endif /* OK_TO_WRITE_DISK */
217 sleep((unsigned int)max(Fort_len / CPERS, MINW));
224 display(FILEDESC *fp)
231 fseek(fp->inf, Seekpts[0], SEEK_SET);
232 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
233 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
234 if (fp->tbl.str_flags & STR_ROTATED)
235 for (p = line; (ch = *p) != '\0'; ++p) {
238 *p = 'A' + (ch - 'A' + 13) % 26;
239 else if (islower(ch))
240 *p = 'a' + (ch - 'a' + 13) % 26;
243 if (fp->tbl.str_flags & STR_COMMENTS
244 && line[0] == fp->tbl.str_delim
245 && line[1] == fp->tbl.str_delim)
254 * Return the length of the fortune.
262 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
263 nchar = (Seekpts[1] - Seekpts[0] <= SLEN);
266 fseek(Fortfile->inf, Seekpts[0], SEEK_SET);
268 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
269 !STR_ENDSTRING(line, Fortfile->tbl))
270 nchar += strlen(line);
278 * This routine evaluates the arguments on the command line
281 getargs(int argc, char **argv)
286 #endif /* NO_REGEX */
292 #endif /* NO_REGEX */
295 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
297 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
300 case 'a': /* any fortune */
309 Equal_probs++; /* scatter un-allocted prob equally */
311 case 'f': /* find fortune files */
314 case 'l': /* long ones only */
318 case 'o': /* offensive ones only */
321 case 's': /* short ones only */
325 case 'w': /* give time to read */
329 case 'i': /* case-insensitive match */
330 case 'm': /* dump out the fortunes */
332 "fortune: can't match fortunes on this system (Sorry)\n");
335 case 'm': /* dump out the fortunes */
339 case 'i': /* case-insensitive match */
342 #endif /* NO_REGEX */
350 if (!form_file_list(argv, argc))
351 exit(1); /* errors printed through form_file_list() */
365 if (BAD_COMP(RE_COMP(pat))) {
367 fprintf(stderr, "%s\n", pat);
369 fprintf(stderr, "bad pattern: %s\n", pat);
373 #endif /* NO_REGEX */
378 * Form the file list from the file specifications.
381 form_file_list(char **files, int file_cnt)
389 Fortunes_only = true;
390 i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
392 Fortunes_only = false;
395 return (add_file(NO_PROB, "fortunes", FORTDIR,
396 &File_list, &File_tail, NULL));
398 for (i = 0; i < file_cnt; i++) {
400 if (!isdigit((unsigned char)files[i][0]))
404 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
405 percent = percent * 10 + *sp - '0';
407 fprintf(stderr, "percentages must be <= 100\n");
411 fprintf(stderr, "percentages must be integers\n");
415 * If the number isn't followed by a '%', then
416 * it was not a percentage, just the first part
417 * of a file name which starts with digits.
423 else if (*++sp == '\0') {
424 if (++i >= file_cnt) {
425 fprintf(stderr, "percentages must precede files\n");
431 if (strcmp(sp, "all") == 0)
433 if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
442 * Add a file to the file list.
445 add_file(int percent, const char *file, const char *dir,
446 FILEDESC **head, FILEDESC **tail, FILEDESC *parent)
451 char *tpath, *offensive;
461 tpath = do_malloc((unsigned int)(strlen(dir) + strlen(file) + 2));
462 strcat(strcat(strcpy(tpath, dir), "/"), file);
466 if ((isdir = is_dir(path)) && parent != NULL) {
469 return (false); /* don't recurse */
472 if (!isdir && parent == NULL && (All_forts || Offend) &&
473 !is_off_name(path)) {
474 offensive = off_name(path);
481 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
482 file = off_name(file);
486 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
488 if ((fd = open(path, O_RDONLY)) < 0) {
490 * This is a sneak. If the user said -a, and if the
491 * file we're given isn't a file, we check to see if
492 * there is a -o version. If there is, we treat it as
493 * if *that* were the file given. We only do this for
494 * individual files -- if we're scanning a directory,
495 * we'll pick up the -o file anyway.
497 if (All_forts && offensive != NULL) {
503 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
504 file = off_name(file);
507 if (dir == NULL && file[0] != '/')
508 return (add_file(percent, file, FORTDIR, head, tail,
517 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
521 fp->percent = percent;
526 if ((isdir && !add_dir(fp)) ||
528 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
532 "fortune:%s not a fortune file or directory\n",
536 do_free(fp->datfile);
537 do_free(fp->posfile);
543 * If the user said -a, we need to make this node a pointer to
544 * both files, if there are two. We don't need to do this if
545 * we are scanning a directory, since the scan will pick up the
548 if (All_forts && parent == NULL && !is_off_name(path))
549 all_forts(fp, offensive);
552 else if (fp->percent == NO_PROB) {
562 #ifdef OK_TO_WRITE_DISK
563 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
564 #endif /* OK_TO_WRITE_DISK */
571 * Return a pointer to an initialized new FILEDESC.
578 fp = (FILEDESC *)do_malloc(sizeof(*fp));
580 fp->pos = POS_UNKNOWN;
583 fp->percent = NO_PROB;
584 fp->read_tbl = false;
597 * Return a pointer to the offensive version of a file of this name.
600 off_name(const char *file)
604 new = copy(file, (unsigned int)(strlen(file) + 2));
606 return (strcat(new, "-o"));
611 * Is the file an offensive-style name?
614 is_off_name(const char *file)
620 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
625 * Modify a FILEDESC element to be the parent of two children if
626 * there are two children to be a parent of.
629 all_forts(FILEDESC *fp, char *offensive)
632 FILEDESC *scene, *obscene;
634 char *datfile, *posfile;
636 if (fp->child != NULL) /* this is a directory, not a file */
638 if (!is_fortfile(offensive, &datfile, &posfile, false))
640 if ((fd = open(offensive, O_RDONLY)) < 0)
642 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
647 fp->num_children = 2;
649 scene->next = obscene;
650 obscene->next = NULL;
651 scene->child = obscene->child = NULL;
652 scene->parent = obscene->parent = fp;
655 scene->percent = obscene->percent = NO_PROB;
659 obscene->path = offensive;
660 if ((sp = rindex(offensive, '/')) == NULL)
661 obscene->name = offensive;
663 obscene->name = ++sp;
664 obscene->datfile = datfile;
665 obscene->posfile = posfile;
666 obscene->read_tbl = false;
667 #ifdef OK_TO_WRITE_DISK
668 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
669 #endif /* OK_TO_WRITE_DISK */
674 * Add the contents of an entire directory.
677 add_dir(FILEDESC *fp)
680 struct dirent *dirent;
686 if ((dir = opendir(fp->path)) == NULL) {
691 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
692 fp->num_children = 0;
693 while ((dirent = readdir(dir)) != NULL) {
694 if ((name = strdup(dirent->d_name)) == NULL)
695 err(1, "strdup failed");
696 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
701 if (fp->num_children == 0) {
703 "fortune: %s: No fortune files in directory.\n", fp->path);
712 * Return true if the file is a directory, false otherwise.
715 is_dir(const char *file)
719 if (stat(file, &sbuf) < 0)
722 return (sbuf.st_mode & S_IFDIR);
727 * Return true if the file is a fortune database file. We try and
728 * exclude files without reading them if possible to avoid
729 * overhead. Files which start with ".", or which have "illegal"
730 * suffixes, as contained in suflist[], are ruled out.
734 is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
739 static const char *suflist[] = {
740 /* list of "illegal" suffixes" */
741 "dat", "pos", "c", "h", "p", "i", "f",
742 "pas", "ftn", "ins.c", "ins,pas",
747 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
750 * Preclude any -o files for offendable people, and any non -o
751 * files for completely offensive people.
753 if (check_for_offend && !All_forts) {
755 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
756 DPRINTF(2, (stderr, "false (offending file)\n"));
761 if ((sp = rindex(file, '/')) == NULL)
766 DPRINTF(2, (stderr, "false (file starts with '.')\n"));
769 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
770 DPRINTF(2, (stderr, "false (check fortunes only)\n"));
773 if ((sp = rindex(sp, '.')) != NULL) {
775 for (i = 0; suflist[i] != NULL; i++)
776 if (strcmp(sp, suflist[i]) == 0) {
777 DPRINTF(2, (stderr, "false (file has suffix \".%s\")\n", sp));
782 datfile = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */
783 strcat(datfile, ".dat");
784 if (access(datfile, R_OK) < 0) {
785 DPRINTF(2, (stderr, "false (no readable \".dat\" file)\n"));
788 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
797 #ifdef OK_TO_WRITE_DISK
799 *posp = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */
800 strcat(*posp, ".pos");
806 #endif /* OK_TO_WRITE_DISK */
807 DPRINTF(2, (stderr, "true\n"));
814 * Return a malloc()'ed copy of the string
817 copy(const char *str, unsigned int len)
821 new = do_malloc(len + 1);
832 * Do a malloc, checking for NULL return.
835 do_malloc(unsigned int size)
839 if ((new = malloc(size)) == NULL) {
840 fprintf(stderr, "fortune: out of memory.\n");
849 * Free malloc'ed space, if any.
860 * Initialize the fortune probabilities.
866 int percent, num_noprob, frac;
870 * Distribute the residual probability (if any) across all
871 * files with unspecified probability (i.e., probability of 0)
877 for (fp = File_tail; fp != NULL; fp = fp->prev)
878 if (fp->percent == NO_PROB) {
883 percent += fp->percent;
884 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
885 percent, num_noprob));
888 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
890 } else if (percent < 100 && num_noprob == 0) {
892 "fortune: no place to put residual probability (%d%% < 100%%)\n",
895 } else if (percent == 100 && num_noprob != 0) {
897 "fortune: no probability left to put in residual files (100%%)\n");
900 percent = 100 - percent;
902 if (num_noprob != 0) {
903 if (num_noprob > 1) {
904 frac = percent / num_noprob;
905 DPRINTF(1, (stderr, ", frac = %d%%", frac));
906 for (fp = File_list; fp != last; fp = fp->next)
907 if (fp->percent == NO_PROB) {
912 last->percent = percent;
913 DPRINTF(1, (stderr, ", residual = %d%%", percent));
917 ", %d%% distributed over remaining fortunes\n",
920 DPRINTF(1, (stderr, "\n"));
930 * Get the fortune data file's seek pointer for the next fortune.
938 if (File_list->next == NULL || File_list->percent == NO_PROB)
941 choice = random() % 100;
942 DPRINTF(1, (stderr, "choice = %d\n", choice));
943 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
944 if (choice < fp->percent)
947 choice -= fp->percent;
949 " skip \"%s\", %d%% (choice = %d)\n",
950 fp->name, fp->percent, choice));
952 DPRINTF(1, (stderr, "using \"%s\", %d%% (choice = %d)\n",
953 fp->name, fp->percent, choice));
955 if (fp->percent != NO_PROB)
958 if (fp->next != NULL) {
960 choice = random() % Noprob_tbl.str_numstr;
961 DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice,
962 Noprob_tbl.str_numstr));
963 while ((unsigned int)choice >= fp->tbl.str_numstr) {
964 choice -= fp->tbl.str_numstr;
967 " skip \"%s\", %ld (choice = %d)\n",
968 fp->name, fp->tbl.str_numstr,
971 DPRINTF(1, (stderr, "using \"%s\", %ld\n", fp->name,
972 fp->tbl.str_numstr));
976 if (fp->child != NULL) {
977 DPRINTF(1, (stderr, "picking child\n"));
984 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), SEEK_SET);
985 read(fp->datfd, Seekpts, sizeof Seekpts);
986 Seekpts[0] = ntohl(Seekpts[0]);
987 Seekpts[1] = ntohl(Seekpts[1]);
992 * Pick a child from a chosen parent.
995 pick_child(FILEDESC *parent)
1001 choice = random() % parent->num_children;
1002 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1003 choice, parent->num_children));
1004 for (fp = parent->child; choice--; fp = fp->next)
1006 DPRINTF(1, (stderr, " using %s\n", fp->name));
1011 choice = random() % parent->tbl.str_numstr;
1012 DPRINTF(1, (stderr, " choice = %d (of %ld)\n",
1013 choice, parent->tbl.str_numstr));
1014 for (fp = parent->child; (unsigned int)choice >= fp->tbl.str_numstr;
1016 choice -= fp->tbl.str_numstr;
1017 DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n",
1018 fp->name, fp->tbl.str_numstr, choice));
1020 DPRINTF(1, (stderr, " using %s, %ld\n", fp->name,
1021 fp->tbl.str_numstr));
1028 * Sum up all the noprob probabilities, starting with fp.
1031 sum_noprobs(FILEDESC *fp)
1033 static bool did_noprobs = false;
1037 zero_tbl(&Noprob_tbl);
1038 while (fp != NULL) {
1040 sum_tbl(&Noprob_tbl, &fp->tbl);
1049 return (i >= j ? i : j);
1054 * Assocatiate a FILE * with the given FILEDESC.
1057 open_fp(FILEDESC *fp)
1059 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1067 * Open up the dat file if we need to.
1070 open_dat(FILEDESC *fp)
1072 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY)) < 0) {
1073 perror(fp->datfile);
1080 * Get the position from the pos file, if there is one. If not,
1081 * return a random number.
1084 get_pos(FILEDESC *fp)
1086 #ifdef OK_TO_WRITE_DISK
1088 #endif /* OK_TO_WRITE_DISK */
1090 assert(fp->read_tbl);
1091 if (fp->pos == POS_UNKNOWN) {
1092 #ifdef OK_TO_WRITE_DISK
1093 if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
1094 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1095 fp->pos = random() % fp->tbl.str_numstr;
1096 else if (fp->pos >= fp->tbl.str_numstr)
1097 fp->pos %= fp->tbl.str_numstr;
1101 fp->pos = random() % fp->tbl.str_numstr;
1102 #endif /* OK_TO_WRITE_DISK */
1104 if ((unsigned int)++(fp->pos) >= fp->tbl.str_numstr)
1105 fp->pos -= fp->tbl.str_numstr;
1106 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, fp->pos));
1111 * Get the tbl data file the datfile.
1114 get_tbl(FILEDESC *fp)
1121 if (fp->child == NULL) {
1122 if ((fd = open(fp->datfile, O_RDONLY)) < 0) {
1123 perror(fp->datfile);
1126 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1128 "fortune: %s corrupted\n", fp->path);
1131 /* fp->tbl.str_version = ntohl(fp->tbl.str_version); */
1132 fp->tbl.str_numstr = ntohl(fp->tbl.str_numstr);
1133 fp->tbl.str_longlen = ntohl(fp->tbl.str_longlen);
1134 fp->tbl.str_shortlen = ntohl(fp->tbl.str_shortlen);
1135 fp->tbl.str_flags = ntohl(fp->tbl.str_flags);
1140 for (child = fp->child; child != NULL; child = child->next) {
1142 sum_tbl(&fp->tbl, &child->tbl);
1145 fp->read_tbl = true;
1150 * Zero out the fields we care about in a tbl structure.
1153 zero_tbl(STRFILE *tp)
1156 tp->str_longlen = 0;
1157 tp->str_shortlen = ~((unsigned long)0);
1162 * Merge the tbl data of t2 into t1.
1165 sum_tbl(STRFILE *t1, STRFILE *t2)
1167 t1->str_numstr += t2->str_numstr;
1168 if (t1->str_longlen < t2->str_longlen)
1169 t1->str_longlen = t2->str_longlen;
1170 if (t1->str_shortlen > t2->str_shortlen)
1171 t1->str_shortlen = t2->str_shortlen;
1174 #define STR(str) ((str) == NULL ? "NULL" : (str))
1178 * Print out the file list
1181 print_file_list(void)
1183 print_list(File_list, 0);
1188 * Print out the actual list, recursively.
1191 print_list(FILEDESC *list, int lev)
1193 while (list != NULL) {
1194 fprintf(stderr, "%*s", lev * 4, "");
1195 if (list->percent == NO_PROB)
1196 fprintf(stderr, "___%%");
1198 fprintf(stderr, "%3d%%", list->percent);
1199 fprintf(stderr, " %s", STR(list->name));
1200 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1201 STR(list->datfile), STR(list->posfile)));
1202 fprintf(stderr, "\n");
1203 if (list->child != NULL)
1204 print_list(list->child, lev + 1);
1212 * Convert the pattern to an ignore-case equivalent.
1215 conv_pat(char *orig)
1221 cnt = 1; /* allow for '\0' */
1222 for (sp = orig; *sp != '\0'; sp++)
1223 if (isalpha((unsigned char)*sp))
1227 if ((new = malloc(cnt)) == NULL) {
1228 fprintf(stderr, "pattern too long for ignoring case\n");
1232 for (sp = new; *orig != '\0'; orig++) {
1233 if (islower((unsigned char)*orig)) {
1236 *sp++ = toupper((unsigned char)*orig);
1239 else if (isupper((unsigned char)*orig)) {
1242 *sp++ = tolower((unsigned char)*orig);
1255 * Find all the fortunes which match the pattern we've been given.
1260 Fort_len = maxlen_in_list(File_list);
1261 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1262 /* extra length, "%\n" is appended */
1263 Fortbuf = do_malloc((unsigned int)Fort_len + 10);
1266 matches_in_list(File_list);
1273 * Return the maximum fortune len in the file list.
1276 maxlen_in_list(FILEDESC *list)
1282 for (fp = list; fp != NULL; fp = fp->next) {
1283 if (fp->child != NULL) {
1284 if ((len = maxlen_in_list(fp->child)) > maxlen)
1289 if (fp->tbl.str_longlen > (unsigned int)maxlen)
1290 maxlen = fp->tbl.str_longlen;
1299 * Print out the matches from the files in the list.
1302 matches_in_list(FILEDESC *list)
1309 for (fp = list; fp != NULL; fp = fp->next) {
1310 if (fp->child != NULL) {
1311 matches_in_list(fp->child);
1314 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1318 while (fgets(sp, Fort_len, fp->inf) != NULL)
1319 if (fp->tbl.str_flags & STR_COMMENTS
1320 && sp[0] == fp->tbl.str_delim
1321 && sp[1] == fp->tbl.str_delim)
1323 else if (!STR_ENDSTRING(sp, fp->tbl))
1327 if (fp->tbl.str_flags & STR_ROTATED)
1328 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1331 *p = 'A' + (ch - 'A' + 13) % 26;
1332 else if (islower(ch))
1333 *p = 'a' + (ch - 'a' + 13) % 26;
1336 if (RE_EXEC(Fortbuf)) {
1337 printf("%c%c", fp->tbl.str_delim,
1340 printf(" (%s)", fp->name);
1345 fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1351 #endif /* NO_REGEX */
1356 fprintf(stderr, "fortune [-a");
1358 fprintf(stderr, "D");
1360 fprintf(stderr, "f");
1362 fprintf(stderr, "i");
1364 fprintf(stderr, "losw]");
1366 fprintf(stderr, " [-m pattern]");
1368 fprintf(stderr, "[[#%%] file/directory/all]\n");