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
36 * @(#) Copyright (c) 1986, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)fortune.c 8.1 (Berkeley) 5/31/93
38 * $FreeBSD: src/games/fortune/fortune/fortune.c,v 1.18.2.1 2001/07/02 00:35:27 dd Exp $
40 /* $FreeBSD: src/games/fortune/fortune/fortune.c,v 1.18.2.1 2001/07/02 00:35:27 dd Exp $ */
41 /* $DragonFly: src/games/fortune/fortune/fortune.c,v 1.6 2006/08/08 16:58:59 pavalos Exp $ */
43 # include <sys/param.h>
44 # include <sys/stat.h>
59 # include "pathnames.h"
61 # define MINW 6 /* minimum wait if desired */
62 # define CPERS 20 /* # of chars for each sec */
63 # define SLEN 160 /* # of chars in short fortune */
65 # define POS_UNKNOWN ((long) -1) /* pos for file unknown */
66 # define NO_PROB (-1) /* no prob specified for file */
69 # define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
83 char *datfile, *posfile;
88 struct fd *child, *parent;
89 struct fd *next, *prev;
92 bool Found_one; /* did we find a match? */
93 bool Find_files = false; /* just find a list of proper fortune files */
94 bool Fortunes_only = false; /* check only "fortunes" files */
95 bool Wait = false; /* wait desired after fortune */
96 bool Short_only = false; /* short fortune desired */
97 bool Long_only = false; /* long fortune desired */
98 bool Offend = false; /* offensive fortunes only */
99 bool All_forts = false; /* any fortune allowed */
100 bool Equal_probs = false; /* scatter un-allocted prob equally */
102 bool Match = false; /* dump fortunes matching a pattern */
105 bool Debug = false; /* print debug messages */
108 char *Fortbuf = NULL; /* fortune buffer for -m */
112 long Seekpts[2]; /* seek pointers to fortunes */
114 FILEDESC *File_list = NULL, /* Head of file list */
115 *File_tail = NULL; /* Tail of file list */
116 FILEDESC *Fortfile; /* Fortune file to use */
118 STRFILE Noprob_tbl; /* sum of data for all no prob files */
120 bool add_dir (FILEDESC *);
122 const char *, const char *, FILEDESC **, FILEDESC **, FILEDESC *);
123 void all_forts (FILEDESC *, char *);
124 char *copy (const char *, u_int);
125 void display (FILEDESC *);
126 void do_free (void *);
127 void *do_malloc (u_int);
128 bool form_file_list (char **, int);
130 void get_fort (void);
131 void get_pos (FILEDESC *);
132 void get_tbl (FILEDESC *);
133 void getargs (int, char *[]);
134 void init_prob (void);
135 bool is_dir (const char *);
136 bool is_fortfile (const char *, char **, char **, int);
137 bool is_off_name (const char *);
141 char *off_name (const char *);
142 void open_dat (FILEDESC *);
143 void open_fp (FILEDESC *);
145 pick_child (FILEDESC *);
146 void print_file_list (void);
147 void print_list (FILEDESC *, int);
148 void sum_noprobs (FILEDESC *);
149 void sum_tbl (STRFILE *, STRFILE *);
151 void zero_tbl (STRFILE *);
154 char *conv_pat (char *);
155 int find_matches (void);
156 void matches_in_list (FILEDESC *);
157 int maxlen_in_list (FILEDESC *);
162 # define RE_COMP(p) (Re_pat = regcmp(p, NULL))
163 # define BAD_COMP(f) ((f) == NULL)
164 # define RE_EXEC(p) regex(Re_pat, (p))
168 char *regcmp(), *regex();
170 # define RE_COMP(p) (p = re_comp(p))
171 # define BAD_COMP(f) ((f) != NULL)
172 # define RE_EXEC(p) re_exec(p)
178 main(int ac, char **av)
180 #ifdef OK_TO_WRITE_DISK
182 #endif /* OK_TO_WRITE_DISK */
184 (void) setlocale(LC_ALL, "");
190 exit(find_matches() != 0);
197 } while ((Short_only && fortlen() > SLEN) ||
198 (Long_only && fortlen() <= SLEN));
202 #ifdef OK_TO_WRITE_DISK
203 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
204 perror(Fortfile->posfile);
209 * if we can, we exclusive lock, but since it isn't very
210 * important, we just punt if we don't have easy locking
213 (void) flock(fd, LOCK_EX);
215 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
216 if (!Fortfile->was_pos_file)
217 (void) chmod(Fortfile->path, 0666);
219 (void) flock(fd, LOCK_UN);
221 #endif /* OK_TO_WRITE_DISK */
225 sleep((unsigned int) max(Fort_len / CPERS, MINW));
232 display(FILEDESC *fp)
239 (void) fseek(fp->inf, Seekpts[0], 0);
240 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
241 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
242 if (fp->tbl.str_flags & STR_ROTATED)
243 for (p = line; (ch = *p) != '\0'; ++p) {
246 *p = 'A' + (ch - 'A' + 13) % 26;
247 else if (islower(ch))
248 *p = 'a' + (ch - 'a' + 13) % 26;
251 if (fp->tbl.str_flags & STR_COMMENTS
252 && line[0] == fp->tbl.str_delim
253 && line[1] == fp->tbl.str_delim)
257 (void) fflush(stdout);
262 * Return the length of the fortune.
270 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
271 nchar = (Seekpts[1] - Seekpts[0] <= SLEN);
274 (void) fseek(Fortfile->inf, Seekpts[0], 0);
276 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
277 !STR_ENDSTRING(line, Fortfile->tbl))
278 nchar += strlen(line);
285 * This routine evaluates the arguments on the command line
288 getargs(int argc, char **argv)
293 # endif /* NO_REGEX */
299 # endif /* NO_REGEX */
302 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
304 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
307 case 'a': /* any fortune */
316 Equal_probs++; /* scatter un-allocted prob equally */
318 case 'f': /* find fortune files */
321 case 'l': /* long ones only */
325 case 'o': /* offensive ones only */
328 case 's': /* short ones only */
332 case 'w': /* give time to read */
336 case 'i': /* case-insensitive match */
337 case 'm': /* dump out the fortunes */
338 (void) fprintf(stderr,
339 "fortune: can't match fortunes on this system (Sorry)\n");
341 # else /* NO_REGEX */
342 case 'm': /* dump out the fortunes */
346 case 'i': /* case-insensitive match */
349 # endif /* NO_REGEX */
357 if (!form_file_list(argv, argc))
358 exit(1); /* errors printed through form_file_list() */
372 if (BAD_COMP(RE_COMP(pat))) {
374 fprintf(stderr, "%s\n", pat);
376 fprintf(stderr, "bad pattern: %s\n", pat);
380 # endif /* NO_REGEX */
385 * Form the file list from the file specifications.
388 form_file_list(char **files, int file_cnt)
396 Fortunes_only = true;
397 i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
399 Fortunes_only = false;
402 return add_file(NO_PROB, "fortunes", FORTDIR,
403 &File_list, &File_tail, NULL);
405 for (i = 0; i < file_cnt; i++) {
407 if (!isdigit((unsigned char)files[i][0]))
411 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
412 percent = percent * 10 + *sp - '0';
414 fprintf(stderr, "percentages must be <= 100\n");
418 fprintf(stderr, "percentages must be integers\n");
422 * If the number isn't followed by a '%', then
423 * it was not a percentage, just the first part
424 * of a file name which starts with digits.
430 else if (*++sp == '\0') {
431 if (++i >= file_cnt) {
432 fprintf(stderr, "percentages must precede files\n");
438 if (strcmp(sp, "all") == 0)
440 if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
448 * Add a file to the file list.
451 add_file(int percent, const char *file, const char *dir, FILEDESC **head, FILEDESC **tail, FILEDESC *parent)
456 char *tpath, *offensive;
466 tpath = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2));
467 (void) strcat(strcat(strcpy(tpath, dir), "/"), file);
471 if ((isdir = is_dir(path)) && parent != NULL) {
474 return false; /* don't recurse */
477 if (!isdir && parent == NULL && (All_forts || Offend) &&
478 !is_off_name(path)) {
479 offensive = off_name(path);
486 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
487 file = off_name(file);
491 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
493 if ((fd = open(path, 0)) < 0) {
495 * This is a sneak. If the user said -a, and if the
496 * file we're given isn't a file, we check to see if
497 * there is a -o version. If there is, we treat it as
498 * if *that* were the file given. We only do this for
499 * individual files -- if we're scanning a directory,
500 * we'll pick up the -o file anyway.
502 if (All_forts && offensive != NULL) {
508 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
509 file = off_name(file);
512 if (dir == NULL && file[0] != '/')
513 return add_file(percent, file, FORTDIR, head, tail,
522 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
526 fp->percent = percent;
531 if ((isdir && !add_dir(fp)) ||
533 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
537 "fortune:%s not a fortune file or directory\n",
541 do_free(fp->datfile);
542 do_free(fp->posfile);
548 * If the user said -a, we need to make this node a pointer to
549 * both files, if there are two. We don't need to do this if
550 * we are scanning a directory, since the scan will pick up the
553 if (All_forts && parent == NULL && !is_off_name(path))
554 all_forts(fp, offensive);
557 else if (fp->percent == NO_PROB) {
567 #ifdef OK_TO_WRITE_DISK
568 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
569 #endif /* OK_TO_WRITE_DISK */
576 * Return a pointer to an initialized new FILEDESC.
583 fp = (FILEDESC *) do_malloc(sizeof *fp);
585 fp->pos = POS_UNKNOWN;
588 fp->percent = NO_PROB;
589 fp->read_tbl = false;
601 * Return a pointer to the offensive version of a file of this name.
604 off_name(const char *file)
608 new = copy(file, (unsigned int) (strlen(file) + 2));
609 return strcat(new, "-o");
614 * Is the file an offensive-style name?
617 is_off_name(const char *file)
622 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
627 * Modify a FILEDESC element to be the parent of two children if
628 * there are two children to be a parent of.
631 all_forts(FILEDESC *fp, char *offensive)
634 FILEDESC *scene, *obscene;
636 auto char *datfile, *posfile;
638 if (fp->child != NULL) /* this is a directory, not a file */
640 if (!is_fortfile(offensive, &datfile, &posfile, false))
642 if ((fd = open(offensive, 0)) < 0)
644 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
649 fp->num_children = 2;
651 scene->next = obscene;
652 obscene->next = NULL;
653 scene->child = obscene->child = NULL;
654 scene->parent = obscene->parent = fp;
657 scene->percent = obscene->percent = NO_PROB;
661 obscene->path = offensive;
662 if ((sp = rindex(offensive, '/')) == NULL)
663 obscene->name = offensive;
665 obscene->name = ++sp;
666 obscene->datfile = datfile;
667 obscene->posfile = posfile;
668 obscene->read_tbl = false;
669 #ifdef OK_TO_WRITE_DISK
670 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
671 #endif /* OK_TO_WRITE_DISK */
676 * Add the contents of an entire directory.
679 add_dir(FILEDESC *fp)
682 struct dirent *dirent;
683 auto FILEDESC *tailp;
686 (void) close(fp->fd);
688 if ((dir = opendir(fp->path)) == NULL) {
693 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
694 fp->num_children = 0;
695 while ((dirent = readdir(dir)) != NULL) {
696 if ((name = strdup(dirent->d_name)) == NULL)
697 err(1, "strdup failed");
698 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
703 if (fp->num_children == 0) {
704 (void) fprintf(stderr,
705 "fortune: %s: No fortune files in directory.\n", fp->path);
713 * Return true if the file is a directory, false otherwise.
716 is_dir(const char *file)
718 auto struct stat sbuf;
720 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[] = { /* list of "illegal" suffixes" */
740 "dat", "pos", "c", "h", "p", "i", "f",
741 "pas", "ftn", "ins.c", "ins,pas",
746 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
749 * Preclude any -o files for offendable people, and any non -o
750 * files for completely offensive people.
752 if (check_for_offend && !All_forts) {
754 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
755 DPRINTF(2, (stderr, "false (offending file)\n"));
760 if ((sp = rindex(file, '/')) == NULL)
765 DPRINTF(2, (stderr, "false (file starts with '.')\n"));
768 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
769 DPRINTF(2, (stderr, "false (check fortunes only)\n"));
772 if ((sp = rindex(sp, '.')) != NULL) {
774 for (i = 0; suflist[i] != NULL; i++)
775 if (strcmp(sp, suflist[i]) == 0) {
776 DPRINTF(2, (stderr, "false (file has suffix \".%s\")\n", sp));
781 datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
782 strcat(datfile, ".dat");
783 if (access(datfile, R_OK) < 0) {
784 DPRINTF(2, (stderr, "false (no readable \".dat\" file)\n"));
787 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
797 #ifdef OK_TO_WRITE_DISK
798 *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
799 (void) strcat(*posp, ".pos");
802 #endif /* OK_TO_WRITE_DISK */
804 DPRINTF(2, (stderr, "true\n"));
810 * Return a malloc()'ed copy of the string
813 copy(const char *str, unsigned int len)
817 new = do_malloc(len + 1);
827 * Do a malloc, checking for NULL return.
830 do_malloc(unsigned int size)
834 if ((new = malloc(size)) == NULL) {
835 (void) fprintf(stderr, "fortune: out of memory.\n");
843 * Free malloc'ed space, if any.
854 * Initialize the fortune probabilities.
859 FILEDESC *fp, *last = NULL;
860 int percent, num_noprob, frac;
863 * Distribute the residual probability (if any) across all
864 * files with unspecified probability (i.e., probability of 0)
870 for (fp = File_tail; fp != NULL; fp = fp->prev)
871 if (fp->percent == NO_PROB) {
877 percent += fp->percent;
878 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
879 percent, num_noprob));
881 (void) fprintf(stderr,
882 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
885 else if (percent < 100 && num_noprob == 0) {
886 (void) fprintf(stderr,
887 "fortune: no place to put residual probability (%d%% < 100%%)\n",
891 else if (percent == 100 && num_noprob != 0) {
892 (void) fprintf(stderr,
893 "fortune: no probability left to put in residual files (100%%)\n");
896 percent = 100 - percent;
898 if (num_noprob != 0) {
899 if (num_noprob > 1) {
900 frac = percent / num_noprob;
901 DPRINTF(1, (stderr, ", frac = %d%%", frac));
902 for (fp = File_list; fp != last; fp = fp->next)
903 if (fp->percent == NO_PROB) {
908 last->percent = percent;
909 DPRINTF(1, (stderr, ", residual = %d%%", percent));
913 ", %d%% distributed over remaining fortunes\n",
916 DPRINTF(1, (stderr, "\n"));
926 * Get the fortune data file's seek pointer for the next fortune.
934 if (File_list->next == NULL || File_list->percent == NO_PROB)
937 choice = random() % 100;
938 DPRINTF(1, (stderr, "choice = %d\n", choice));
939 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
940 if (choice < fp->percent)
943 choice -= fp->percent;
945 " skip \"%s\", %d%% (choice = %d)\n",
946 fp->name, fp->percent, choice));
949 "using \"%s\", %d%% (choice = %d)\n",
950 fp->name, fp->percent, choice));
952 if (fp->percent != NO_PROB)
955 if (fp->next != NULL) {
957 choice = random() % Noprob_tbl.str_numstr;
958 DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice,
959 Noprob_tbl.str_numstr));
960 while ((unsigned int)choice >= fp->tbl.str_numstr) {
961 choice -= fp->tbl.str_numstr;
964 " skip \"%s\", %ld (choice = %d)\n",
965 fp->name, fp->tbl.str_numstr,
968 DPRINTF(1, (stderr, "using \"%s\", %ld\n", fp->name,
969 fp->tbl.str_numstr));
973 if (fp->child != NULL) {
974 DPRINTF(1, (stderr, "picking child\n"));
980 (void) lseek(fp->datfd,
981 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0);
982 read(fp->datfd, Seekpts, sizeof Seekpts);
983 Seekpts[0] = ntohl(Seekpts[0]);
984 Seekpts[1] = ntohl(Seekpts[1]);
989 * Pick a child from a chosen parent.
992 pick_child(FILEDESC *parent)
998 choice = random() % parent->num_children;
999 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1000 choice, parent->num_children));
1001 for (fp = parent->child; choice--; fp = fp->next)
1003 DPRINTF(1, (stderr, " using %s\n", fp->name));
1008 choice = random() % parent->tbl.str_numstr;
1009 DPRINTF(1, (stderr, " choice = %d (of %ld)\n",
1010 choice, parent->tbl.str_numstr));
1011 for (fp = parent->child; (unsigned int)choice >= fp->tbl.str_numstr;
1013 choice -= fp->tbl.str_numstr;
1014 DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n",
1015 fp->name, fp->tbl.str_numstr, choice));
1017 DPRINTF(1, (stderr, " using %s, %ld\n", fp->name,
1018 fp->tbl.str_numstr));
1025 * Sum up all the noprob probabilities, starting with fp.
1028 sum_noprobs(FILEDESC *fp)
1030 static bool did_noprobs = false;
1034 zero_tbl(&Noprob_tbl);
1035 while (fp != NULL) {
1037 sum_tbl(&Noprob_tbl, &fp->tbl);
1046 return (i >= j ? i : j);
1051 * Assocatiate a FILE * with the given FILEDESC.
1054 open_fp(FILEDESC *fp)
1056 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1064 * Open up the dat file if we need to.
1067 open_dat(FILEDESC *fp)
1069 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, 0)) < 0) {
1070 perror(fp->datfile);
1077 * Get the position from the pos file, if there is one. If not,
1078 * return a random number.
1081 get_pos(FILEDESC *fp)
1083 #ifdef OK_TO_WRITE_DISK
1085 #endif /* OK_TO_WRITE_DISK */
1087 assert(fp->read_tbl);
1088 if (fp->pos == POS_UNKNOWN) {
1089 #ifdef OK_TO_WRITE_DISK
1090 if ((fd = open(fp->posfile, 0)) < 0 ||
1091 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1092 fp->pos = random() % fp->tbl.str_numstr;
1093 else if (fp->pos >= fp->tbl.str_numstr)
1094 fp->pos %= fp->tbl.str_numstr;
1098 fp->pos = random() % fp->tbl.str_numstr;
1099 #endif /* OK_TO_WRITE_DISK */
1101 if ((unsigned int)++(fp->pos) >= fp->tbl.str_numstr)
1102 fp->pos -= fp->tbl.str_numstr;
1103 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, fp->pos));
1108 * Get the tbl data file the datfile.
1111 get_tbl(FILEDESC *fp)
1118 if (fp->child == NULL) {
1119 if ((fd = open(fp->datfile, 0)) < 0) {
1120 perror(fp->datfile);
1123 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1124 (void)fprintf(stderr,
1125 "fortune: %s corrupted\n", fp->path);
1128 /* fp->tbl.str_version = ntohl(fp->tbl.str_version); */
1129 fp->tbl.str_numstr = ntohl(fp->tbl.str_numstr);
1130 fp->tbl.str_longlen = ntohl(fp->tbl.str_longlen);
1131 fp->tbl.str_shortlen = ntohl(fp->tbl.str_shortlen);
1132 fp->tbl.str_flags = ntohl(fp->tbl.str_flags);
1137 for (child = fp->child; child != NULL; child = child->next) {
1139 sum_tbl(&fp->tbl, &child->tbl);
1142 fp->read_tbl = true;
1147 * Zero out the fields we care about in a tbl structure.
1150 zero_tbl(STRFILE *tp)
1153 tp->str_longlen = 0;
1154 tp->str_shortlen = ~((unsigned long)0);
1159 * Merge the tbl data of t2 into t1.
1162 sum_tbl(STRFILE *t1, STRFILE *t2)
1164 t1->str_numstr += t2->str_numstr;
1165 if (t1->str_longlen < t2->str_longlen)
1166 t1->str_longlen = t2->str_longlen;
1167 if (t1->str_shortlen > t2->str_shortlen)
1168 t1->str_shortlen = t2->str_shortlen;
1171 #define STR(str) ((str) == NULL ? "NULL" : (str))
1175 * Print out the file list
1178 print_file_list(void)
1180 print_list(File_list, 0);
1185 * Print out the actual list, recursively.
1188 print_list(FILEDESC *list, int lev)
1190 while (list != NULL) {
1191 fprintf(stderr, "%*s", lev * 4, "");
1192 if (list->percent == NO_PROB)
1193 fprintf(stderr, "___%%");
1195 fprintf(stderr, "%3d%%", list->percent);
1196 fprintf(stderr, " %s", STR(list->name));
1197 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1198 STR(list->datfile), STR(list->posfile)));
1199 fprintf(stderr, "\n");
1200 if (list->child != NULL)
1201 print_list(list->child, lev + 1);
1209 * Convert the pattern to an ignore-case equivalent.
1212 conv_pat(char *orig)
1218 cnt = 1; /* allow for '\0' */
1219 for (sp = orig; *sp != '\0'; sp++)
1220 if (isalpha((unsigned char)*sp))
1224 if ((new = malloc(cnt)) == NULL) {
1225 fprintf(stderr, "pattern too long for ignoring case\n");
1229 for (sp = new; *orig != '\0'; orig++) {
1230 if (islower((unsigned char)*orig)) {
1233 *sp++ = toupper((unsigned char)*orig);
1236 else if (isupper((unsigned char)*orig)) {
1239 *sp++ = tolower((unsigned char)*orig);
1251 * Find all the fortunes which match the pattern we've been given.
1256 Fort_len = maxlen_in_list(File_list);
1257 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1258 /* extra length, "%\n" is appended */
1259 Fortbuf = do_malloc((unsigned int) Fort_len + 10);
1262 matches_in_list(File_list);
1269 * Return the maximum fortune len in the file list.
1272 maxlen_in_list(FILEDESC *list)
1278 for (fp = list; fp != NULL; fp = fp->next) {
1279 if (fp->child != NULL) {
1280 if ((len = maxlen_in_list(fp->child)) > maxlen)
1285 if (fp->tbl.str_longlen > (unsigned int)maxlen)
1286 maxlen = fp->tbl.str_longlen;
1294 * Print out the matches from the files in the list.
1297 matches_in_list(FILEDESC *list)
1304 for (fp = list; fp != NULL; fp = fp->next) {
1305 if (fp->child != NULL) {
1306 matches_in_list(fp->child);
1309 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1313 while (fgets(sp, Fort_len, fp->inf) != NULL)
1314 if (fp->tbl.str_flags & STR_COMMENTS
1315 && sp[0] == fp->tbl.str_delim
1316 && sp[1] == fp->tbl.str_delim)
1318 else if (!STR_ENDSTRING(sp, fp->tbl))
1322 if (fp->tbl.str_flags & STR_ROTATED)
1323 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1326 *p = 'A' + (ch - 'A' + 13) % 26;
1327 else if (islower(ch))
1328 *p = 'a' + (ch - 'a' + 13) % 26;
1331 if (RE_EXEC(Fortbuf)) {
1332 printf("%c%c", fp->tbl.str_delim,
1335 printf(" (%s)", fp->name);
1340 (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1346 # endif /* NO_REGEX */
1351 (void) fprintf(stderr, "fortune [-a");
1353 (void) fprintf(stderr, "D");
1355 (void) fprintf(stderr, "f");
1357 (void) fprintf(stderr, "i");
1358 #endif /* NO_REGEX */
1359 (void) fprintf(stderr, "losw]");
1361 (void) fprintf(stderr, " [-m pattern]");
1362 #endif /* NO_REGEX */
1363 (void) fprintf(stderr, "[[#%%] file/directory/all]\n");