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.5 2005/09/01 22:45:35 liamfoy Exp $ */
43 # include <sys/param.h>
44 # include <sys/stat.h>
58 # include "pathnames.h"
64 # define MINW 6 /* minimum wait if desired */
65 # define CPERS 20 /* # of chars for each sec */
66 # define SLEN 160 /* # of chars in short fortune */
68 # define POS_UNKNOWN ((long) -1) /* pos for file unknown */
69 # define NO_PROB (-1) /* no prob specified for file */
72 # define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
86 char *datfile, *posfile;
91 struct fd *child, *parent;
92 struct fd *next, *prev;
95 bool Found_one; /* did we find a match? */
96 bool Find_files = FALSE; /* just find a list of proper fortune files */
97 bool Fortunes_only = FALSE; /* check only "fortunes" files */
98 bool Wait = FALSE; /* wait desired after fortune */
99 bool Short_only = FALSE; /* short fortune desired */
100 bool Long_only = FALSE; /* long fortune desired */
101 bool Offend = FALSE; /* offensive fortunes only */
102 bool All_forts = FALSE; /* any fortune allowed */
103 bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
105 bool Match = FALSE; /* dump fortunes matching a pattern */
108 bool Debug = FALSE; /* print debug messages */
111 char *Fortbuf = NULL; /* fortune buffer for -m */
115 long Seekpts[2]; /* seek pointers to fortunes */
117 FILEDESC *File_list = NULL, /* Head of file list */
118 *File_tail = NULL; /* Tail of file list */
119 FILEDESC *Fortfile; /* Fortune file to use */
121 STRFILE Noprob_tbl; /* sum of data for all no prob files */
123 int add_dir (FILEDESC *);
125 char *, char *, FILEDESC **, FILEDESC **, FILEDESC *);
126 void all_forts (FILEDESC *, char *);
127 char *copy (char *, u_int);
128 void display (FILEDESC *);
129 void do_free (void *);
130 void *do_malloc (u_int);
131 int form_file_list (char **, int);
133 void get_fort (void);
134 void get_pos (FILEDESC *);
135 void get_tbl (FILEDESC *);
136 void getargs (int, char *[]);
137 void init_prob (void);
139 int is_fortfile (char *, char **, char **, int);
140 int is_off_name (char *);
144 char *off_name (char *);
145 void open_dat (FILEDESC *);
146 void open_fp (FILEDESC *);
148 pick_child (FILEDESC *);
149 void print_file_list (void);
150 void print_list (FILEDESC *, int);
151 void sum_noprobs (FILEDESC *);
152 void sum_tbl (STRFILE *, STRFILE *);
154 void zero_tbl (STRFILE *);
157 char *conv_pat (char *);
158 int find_matches (void);
159 void matches_in_list (FILEDESC *);
160 int maxlen_in_list (FILEDESC *);
165 # define RE_COMP(p) (Re_pat = regcmp(p, NULL))
166 # define BAD_COMP(f) ((f) == NULL)
167 # define RE_EXEC(p) regex(Re_pat, (p))
171 char *regcmp(), *regex();
173 # define RE_COMP(p) (p = re_comp(p))
174 # define BAD_COMP(f) ((f) != NULL)
175 # define RE_EXEC(p) re_exec(p)
181 main(int ac, char **av)
183 #ifdef OK_TO_WRITE_DISK
185 #endif /* OK_TO_WRITE_DISK */
187 (void) setlocale(LC_ALL, "");
193 exit(find_matches() != 0);
200 } while ((Short_only && fortlen() > SLEN) ||
201 (Long_only && fortlen() <= SLEN));
205 #ifdef OK_TO_WRITE_DISK
206 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
207 perror(Fortfile->posfile);
212 * if we can, we exclusive lock, but since it isn't very
213 * important, we just punt if we don't have easy locking
216 (void) flock(fd, LOCK_EX);
218 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
219 if (!Fortfile->was_pos_file)
220 (void) chmod(Fortfile->path, 0666);
222 (void) flock(fd, LOCK_UN);
224 #endif /* OK_TO_WRITE_DISK */
228 sleep((unsigned int) max(Fort_len / CPERS, MINW));
235 display(FILEDESC *fp)
242 (void) fseek(fp->inf, Seekpts[0], 0);
243 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
244 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
245 if (fp->tbl.str_flags & STR_ROTATED)
246 for (p = line; (ch = *p) != '\0'; ++p) {
249 *p = 'A' + (ch - 'A' + 13) % 26;
250 else if (islower(ch))
251 *p = 'a' + (ch - 'a' + 13) % 26;
254 if (fp->tbl.str_flags & STR_COMMENTS
255 && line[0] == fp->tbl.str_delim
256 && line[1] == fp->tbl.str_delim)
260 (void) fflush(stdout);
265 * Return the length of the fortune.
273 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
274 nchar = (Seekpts[1] - Seekpts[0] <= SLEN);
277 (void) fseek(Fortfile->inf, Seekpts[0], 0);
279 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
280 !STR_ENDSTRING(line, Fortfile->tbl))
281 nchar += strlen(line);
288 * This routine evaluates the arguments on the command line
291 getargs(int argc, char **argv)
296 # endif /* NO_REGEX */
304 # endif /* NO_REGEX */
307 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
309 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
312 case 'a': /* any fortune */
321 Equal_probs++; /* scatter un-allocted prob equally */
323 case 'f': /* find fortune files */
326 case 'l': /* long ones only */
330 case 'o': /* offensive ones only */
333 case 's': /* short ones only */
337 case 'w': /* give time to read */
341 case 'i': /* case-insensitive match */
342 case 'm': /* dump out the fortunes */
343 (void) fprintf(stderr,
344 "fortune: can't match fortunes on this system (Sorry)\n");
346 # else /* NO_REGEX */
347 case 'm': /* dump out the fortunes */
351 case 'i': /* case-insensitive match */
354 # endif /* NO_REGEX */
362 if (!form_file_list(argv, argc))
363 exit(1); /* errors printed through form_file_list() */
377 if (BAD_COMP(RE_COMP(pat))) {
379 fprintf(stderr, "%s\n", pat);
381 fprintf(stderr, "bad pattern: %s\n", pat);
385 # endif /* NO_REGEX */
390 * Form the file list from the file specifications.
393 form_file_list(char **files, int file_cnt)
400 Fortunes_only = TRUE;
401 i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
403 Fortunes_only = FALSE;
406 return add_file(NO_PROB, "fortunes", FORTDIR,
407 &File_list, &File_tail, NULL);
409 for (i = 0; i < file_cnt; i++) {
411 if (!isdigit((unsigned char)files[i][0]))
415 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
416 percent = percent * 10 + *sp - '0';
418 fprintf(stderr, "percentages must be <= 100\n");
422 fprintf(stderr, "percentages must be integers\n");
426 * If the number isn't followed by a '%', then
427 * it was not a percentage, just the first part
428 * of a file name which starts with digits.
434 else if (*++sp == '\0') {
435 if (++i >= file_cnt) {
436 fprintf(stderr, "percentages must precede files\n");
442 if (strcmp(sp, "all") == 0)
444 if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
452 * Add a file to the file list.
455 add_file(int percent, char *file, char *dir, FILEDESC **head, FILEDESC **tail, FILEDESC *parent)
459 char *path, *offensive;
468 path = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2));
469 (void) strcat(strcat(strcpy(path, dir), "/"), file);
472 if ((isdir = is_dir(path)) && parent != NULL) {
475 return FALSE; /* don't recurse */
478 if (!isdir && parent == NULL && (All_forts || Offend) &&
479 !is_off_name(path)) {
480 offensive = off_name(path);
487 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
488 file = off_name(file);
492 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
494 if ((fd = open(path, 0)) < 0) {
496 * This is a sneak. If the user said -a, and if the
497 * file we're given isn't a file, we check to see if
498 * there is a -o version. If there is, we treat it as
499 * if *that* were the file given. We only do this for
500 * individual files -- if we're scanning a directory,
501 * we'll pick up the -o file anyway.
503 if (All_forts && offensive != NULL) {
509 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
510 file = off_name(file);
513 if (dir == NULL && file[0] != '/')
514 return add_file(percent, file, FORTDIR, head, tail,
523 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
527 fp->percent = percent;
532 if ((isdir && !add_dir(fp)) ||
534 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
538 "fortune:%s not a fortune file or directory\n",
542 do_free(fp->datfile);
543 do_free(fp->posfile);
549 * If the user said -a, we need to make this node a pointer to
550 * both files, if there are two. We don't need to do this if
551 * we are scanning a directory, since the scan will pick up the
554 if (All_forts && parent == NULL && !is_off_name(path))
555 all_forts(fp, offensive);
558 else if (fp->percent == NO_PROB) {
568 #ifdef OK_TO_WRITE_DISK
569 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
570 #endif /* OK_TO_WRITE_DISK */
577 * Return a pointer to an initialized new FILEDESC.
584 fp = (FILEDESC *) do_malloc(sizeof *fp);
586 fp->pos = POS_UNKNOWN;
589 fp->percent = NO_PROB;
590 fp->read_tbl = FALSE;
602 * Return a pointer to the offensive version of a file of this name.
609 new = copy(file, (unsigned int) (strlen(file) + 2));
610 return strcat(new, "-o");
615 * Is the file an offensive-style name?
618 is_off_name(char *file)
623 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
628 * Modify a FILEDESC element to be the parent of two children if
629 * there are two children to be a parent of.
632 all_forts(FILEDESC *fp, char *offensive)
635 FILEDESC *scene, *obscene;
637 auto char *datfile, *posfile;
639 if (fp->child != NULL) /* this is a directory, not a file */
641 if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
643 if ((fd = open(offensive, 0)) < 0)
645 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
650 fp->num_children = 2;
652 scene->next = obscene;
653 obscene->next = NULL;
654 scene->child = obscene->child = NULL;
655 scene->parent = obscene->parent = fp;
658 scene->percent = obscene->percent = NO_PROB;
662 obscene->path = offensive;
663 if ((sp = rindex(offensive, '/')) == NULL)
664 obscene->name = offensive;
666 obscene->name = ++sp;
667 obscene->datfile = datfile;
668 obscene->posfile = posfile;
669 obscene->read_tbl = FALSE;
670 #ifdef OK_TO_WRITE_DISK
671 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
672 #endif /* OK_TO_WRITE_DISK */
677 * Add the contents of an entire directory.
680 add_dir(FILEDESC *fp)
683 struct dirent *dirent;
684 auto FILEDESC *tailp;
687 (void) close(fp->fd);
689 if ((dir = opendir(fp->path)) == NULL) {
694 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
695 fp->num_children = 0;
696 while ((dirent = readdir(dir)) != NULL) {
697 if ((name = strdup(dirent->d_name)) == NULL)
698 err(1, "strdup failed");
699 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
704 if (fp->num_children == 0) {
705 (void) fprintf(stderr,
706 "fortune: %s: No fortune files in directory.\n", fp->path);
714 * Return TRUE if the file is a directory, FALSE otherwise.
719 auto struct stat sbuf;
721 if (stat(file, &sbuf) < 0)
723 return (sbuf.st_mode & S_IFDIR);
728 * Return TRUE if the file is a fortune database file. We try and
729 * exclude files without reading them if possible to avoid
730 * overhead. Files which start with ".", or which have "illegal"
731 * suffixes, as contained in suflist[], are ruled out.
735 is_fortfile(char *file, char **datp, char **posp, int check_for_offend)
740 static char *suflist[] = { /* 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));
798 #ifdef OK_TO_WRITE_DISK
799 *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
800 (void) strcat(*posp, ".pos");
803 #endif /* OK_TO_WRITE_DISK */
805 DPRINTF(2, (stderr, "TRUE\n"));
811 * Return a malloc()'ed copy of the string
814 copy(char *str, unsigned int len)
818 new = do_malloc(len + 1);
828 * Do a malloc, checking for NULL return.
831 do_malloc(unsigned int size)
835 if ((new = malloc(size)) == NULL) {
836 (void) fprintf(stderr, "fortune: out of memory.\n");
844 * Free malloc'ed space, if any.
855 * Initialize the fortune probabilities.
860 FILEDESC *fp, *last = NULL;
861 int percent, num_noprob, frac;
864 * Distribute the residual probability (if any) across all
865 * files with unspecified probability (i.e., probability of 0)
871 for (fp = File_tail; fp != NULL; fp = fp->prev)
872 if (fp->percent == NO_PROB) {
878 percent += fp->percent;
879 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
880 percent, num_noprob));
882 (void) fprintf(stderr,
883 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
886 else if (percent < 100 && num_noprob == 0) {
887 (void) fprintf(stderr,
888 "fortune: no place to put residual probability (%d%% < 100%%)\n",
892 else if (percent == 100 && num_noprob != 0) {
893 (void) fprintf(stderr,
894 "fortune: no probability left to put in residual files (100%%)\n");
897 percent = 100 - percent;
899 if (num_noprob != 0) {
900 if (num_noprob > 1) {
901 frac = percent / num_noprob;
902 DPRINTF(1, (stderr, ", frac = %d%%", frac));
903 for (fp = File_list; fp != last; fp = fp->next)
904 if (fp->percent == NO_PROB) {
909 last->percent = percent;
910 DPRINTF(1, (stderr, ", residual = %d%%", percent));
914 ", %d%% distributed over remaining fortunes\n",
917 DPRINTF(1, (stderr, "\n"));
927 * Get the fortune data file's seek pointer for the next fortune.
935 if (File_list->next == NULL || File_list->percent == NO_PROB)
938 choice = random() % 100;
939 DPRINTF(1, (stderr, "choice = %d\n", choice));
940 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
941 if (choice < fp->percent)
944 choice -= fp->percent;
946 " skip \"%s\", %d%% (choice = %d)\n",
947 fp->name, fp->percent, choice));
950 "using \"%s\", %d%% (choice = %d)\n",
951 fp->name, fp->percent, choice));
953 if (fp->percent != NO_PROB)
956 if (fp->next != NULL) {
958 choice = random() % Noprob_tbl.str_numstr;
959 DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice,
960 Noprob_tbl.str_numstr));
961 while (choice >= fp->tbl.str_numstr) {
962 choice -= fp->tbl.str_numstr;
965 " skip \"%s\", %ld (choice = %d)\n",
966 fp->name, fp->tbl.str_numstr,
969 DPRINTF(1, (stderr, "using \"%s\", %ld\n", fp->name,
970 fp->tbl.str_numstr));
974 if (fp->child != NULL) {
975 DPRINTF(1, (stderr, "picking child\n"));
981 (void) lseek(fp->datfd,
982 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0);
983 read(fp->datfd, Seekpts, sizeof Seekpts);
984 Seekpts[0] = ntohl(Seekpts[0]);
985 Seekpts[1] = ntohl(Seekpts[1]);
990 * Pick a child from a chosen parent.
993 pick_child(FILEDESC *parent)
999 choice = random() % parent->num_children;
1000 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1001 choice, parent->num_children));
1002 for (fp = parent->child; choice--; fp = fp->next)
1004 DPRINTF(1, (stderr, " using %s\n", fp->name));
1009 choice = random() % parent->tbl.str_numstr;
1010 DPRINTF(1, (stderr, " choice = %d (of %ld)\n",
1011 choice, parent->tbl.str_numstr));
1012 for (fp = parent->child; choice >= fp->tbl.str_numstr;
1014 choice -= fp->tbl.str_numstr;
1015 DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n",
1016 fp->name, fp->tbl.str_numstr, choice));
1018 DPRINTF(1, (stderr, " using %s, %ld\n", fp->name,
1019 fp->tbl.str_numstr));
1026 * Sum up all the noprob probabilities, starting with fp.
1029 sum_noprobs(FILEDESC *fp)
1031 static bool did_noprobs = FALSE;
1035 zero_tbl(&Noprob_tbl);
1036 while (fp != NULL) {
1038 sum_tbl(&Noprob_tbl, &fp->tbl);
1047 return (i >= j ? i : j);
1052 * Assocatiate a FILE * with the given FILEDESC.
1055 open_fp(FILEDESC *fp)
1057 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1065 * Open up the dat file if we need to.
1068 open_dat(FILEDESC *fp)
1070 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, 0)) < 0) {
1071 perror(fp->datfile);
1078 * Get the position from the pos file, if there is one. If not,
1079 * return a random number.
1082 get_pos(FILEDESC *fp)
1084 #ifdef OK_TO_WRITE_DISK
1086 #endif /* OK_TO_WRITE_DISK */
1088 assert(fp->read_tbl);
1089 if (fp->pos == POS_UNKNOWN) {
1090 #ifdef OK_TO_WRITE_DISK
1091 if ((fd = open(fp->posfile, 0)) < 0 ||
1092 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1093 fp->pos = random() % fp->tbl.str_numstr;
1094 else if (fp->pos >= fp->tbl.str_numstr)
1095 fp->pos %= fp->tbl.str_numstr;
1099 fp->pos = random() % fp->tbl.str_numstr;
1100 #endif /* OK_TO_WRITE_DISK */
1102 if (++(fp->pos) >= fp->tbl.str_numstr)
1103 fp->pos -= fp->tbl.str_numstr;
1104 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, fp->pos));
1109 * Get the tbl data file the datfile.
1112 get_tbl(FILEDESC *fp)
1119 if (fp->child == NULL) {
1120 if ((fd = open(fp->datfile, 0)) < 0) {
1121 perror(fp->datfile);
1124 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1125 (void)fprintf(stderr,
1126 "fortune: %s corrupted\n", fp->path);
1129 /* fp->tbl.str_version = ntohl(fp->tbl.str_version); */
1130 fp->tbl.str_numstr = ntohl(fp->tbl.str_numstr);
1131 fp->tbl.str_longlen = ntohl(fp->tbl.str_longlen);
1132 fp->tbl.str_shortlen = ntohl(fp->tbl.str_shortlen);
1133 fp->tbl.str_flags = ntohl(fp->tbl.str_flags);
1138 for (child = fp->child; child != NULL; child = child->next) {
1140 sum_tbl(&fp->tbl, &child->tbl);
1143 fp->read_tbl = TRUE;
1148 * Zero out the fields we care about in a tbl structure.
1151 zero_tbl(STRFILE *tp)
1154 tp->str_longlen = 0;
1155 tp->str_shortlen = ~((unsigned long)0);
1160 * Merge the tbl data of t2 into t1.
1163 sum_tbl(STRFILE *t1, STRFILE *t2)
1165 t1->str_numstr += t2->str_numstr;
1166 if (t1->str_longlen < t2->str_longlen)
1167 t1->str_longlen = t2->str_longlen;
1168 if (t1->str_shortlen > t2->str_shortlen)
1169 t1->str_shortlen = t2->str_shortlen;
1172 #define STR(str) ((str) == NULL ? "NULL" : (str))
1176 * Print out the file list
1179 print_file_list(void)
1181 print_list(File_list, 0);
1186 * Print out the actual list, recursively.
1189 print_list(FILEDESC *list, int lev)
1191 while (list != NULL) {
1192 fprintf(stderr, "%*s", lev * 4, "");
1193 if (list->percent == NO_PROB)
1194 fprintf(stderr, "___%%");
1196 fprintf(stderr, "%3d%%", list->percent);
1197 fprintf(stderr, " %s", STR(list->name));
1198 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1199 STR(list->datfile), STR(list->posfile)));
1200 fprintf(stderr, "\n");
1201 if (list->child != NULL)
1202 print_list(list->child, lev + 1);
1210 * Convert the pattern to an ignore-case equivalent.
1213 conv_pat(char *orig)
1219 cnt = 1; /* allow for '\0' */
1220 for (sp = orig; *sp != '\0'; sp++)
1221 if (isalpha((unsigned char)*sp))
1225 if ((new = malloc(cnt)) == NULL) {
1226 fprintf(stderr, "pattern too long for ignoring case\n");
1230 for (sp = new; *orig != '\0'; orig++) {
1231 if (islower((unsigned char)*orig)) {
1234 *sp++ = toupper((unsigned char)*orig);
1237 else if (isupper((unsigned char)*orig)) {
1240 *sp++ = tolower((unsigned char)*orig);
1252 * Find all the fortunes which match the pattern we've been given.
1257 Fort_len = maxlen_in_list(File_list);
1258 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1259 /* extra length, "%\n" is appended */
1260 Fortbuf = do_malloc((unsigned int) Fort_len + 10);
1263 matches_in_list(File_list);
1270 * Return the maximum fortune len in the file list.
1273 maxlen_in_list(FILEDESC *list)
1279 for (fp = list; fp != NULL; fp = fp->next) {
1280 if (fp->child != NULL) {
1281 if ((len = maxlen_in_list(fp->child)) > maxlen)
1286 if (fp->tbl.str_longlen > maxlen)
1287 maxlen = fp->tbl.str_longlen;
1295 * Print out the matches from the files in the list.
1298 matches_in_list(FILEDESC *list)
1305 for (fp = list; fp != NULL; fp = fp->next) {
1306 if (fp->child != NULL) {
1307 matches_in_list(fp->child);
1310 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1314 while (fgets(sp, Fort_len, fp->inf) != NULL)
1315 if (fp->tbl.str_flags & STR_COMMENTS
1316 && sp[0] == fp->tbl.str_delim
1317 && sp[1] == fp->tbl.str_delim)
1319 else if (!STR_ENDSTRING(sp, fp->tbl))
1323 if (fp->tbl.str_flags & STR_ROTATED)
1324 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1327 *p = 'A' + (ch - 'A' + 13) % 26;
1328 else if (islower(ch))
1329 *p = 'a' + (ch - 'a' + 13) % 26;
1332 if (RE_EXEC(Fortbuf)) {
1333 printf("%c%c", fp->tbl.str_delim,
1336 printf(" (%s)", fp->name);
1341 (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1347 # endif /* NO_REGEX */
1352 (void) fprintf(stderr, "fortune [-a");
1354 (void) fprintf(stderr, "D");
1356 (void) fprintf(stderr, "f");
1358 (void) fprintf(stderr, "i");
1359 #endif /* NO_REGEX */
1360 (void) fprintf(stderr, "losw]");
1362 (void) fprintf(stderr, " [-m pattern]");
1363 #endif /* NO_REGEX */
1364 (void) fprintf(stderr, "[[#%%] file/directory/all]\n");