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.3 2003/11/12 14:53:53 eirikn Exp $ */
43 # include <sys/param.h>
44 # include <sys/stat.h>
57 # include "pathnames.h"
63 # define MINW 6 /* minimum wait if desired */
64 # define CPERS 20 /* # of chars for each sec */
65 # define SLEN 160 /* # of chars in short fortune */
67 # define POS_UNKNOWN ((long) -1) /* pos for file unknown */
68 # define NO_PROB (-1) /* no prob specified for file */
71 # define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
85 char *datfile, *posfile;
90 struct fd *child, *parent;
91 struct fd *next, *prev;
94 bool Found_one; /* did we find a match? */
95 bool Find_files = FALSE; /* just find a list of proper fortune files */
96 bool Fortunes_only = FALSE; /* check only "fortunes" files */
97 bool Wait = FALSE; /* wait desired after fortune */
98 bool Short_only = FALSE; /* short fortune desired */
99 bool Long_only = FALSE; /* long fortune desired */
100 bool Offend = FALSE; /* offensive fortunes only */
101 bool All_forts = FALSE; /* any fortune allowed */
102 bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
104 bool Match = FALSE; /* dump fortunes matching a pattern */
107 bool Debug = FALSE; /* print debug messages */
110 char *Fortbuf = NULL; /* fortune buffer for -m */
114 long Seekpts[2]; /* seek pointers to fortunes */
116 FILEDESC *File_list = NULL, /* Head of file list */
117 *File_tail = NULL; /* Tail of file list */
118 FILEDESC *Fortfile; /* Fortune file to use */
120 STRFILE Noprob_tbl; /* sum of data for all no prob files */
122 int add_dir (FILEDESC *);
124 char *, char *, FILEDESC **, FILEDESC **, FILEDESC *);
125 void all_forts (FILEDESC *, char *);
126 char *copy (char *, u_int);
127 void display (FILEDESC *);
128 void do_free (void *);
129 void *do_malloc (u_int);
130 int form_file_list (char **, int);
132 void get_fort (void);
133 void get_pos (FILEDESC *);
134 void get_tbl (FILEDESC *);
135 void getargs (int, char *[]);
136 void init_prob (void);
138 int is_fortfile (char *, char **, char **, int);
139 int is_off_name (char *);
143 char *off_name (char *);
144 void open_dat (FILEDESC *);
145 void open_fp (FILEDESC *);
147 pick_child (FILEDESC *);
148 void print_file_list (void);
149 void print_list (FILEDESC *, int);
150 void sum_noprobs (FILEDESC *);
151 void sum_tbl (STRFILE *, STRFILE *);
153 void zero_tbl (STRFILE *);
156 char *conv_pat (char *);
157 int find_matches (void);
158 void matches_in_list (FILEDESC *);
159 int maxlen_in_list (FILEDESC *);
164 # define RE_COMP(p) (Re_pat = regcmp(p, NULL))
165 # define BAD_COMP(f) ((f) == NULL)
166 # define RE_EXEC(p) regex(Re_pat, (p))
170 char *regcmp(), *regex();
172 # define RE_COMP(p) (p = re_comp(p))
173 # define BAD_COMP(f) ((f) != NULL)
174 # define RE_EXEC(p) re_exec(p)
184 #ifdef OK_TO_WRITE_DISK
186 #endif /* OK_TO_WRITE_DISK */
188 (void) setlocale(LC_ALL, "");
194 exit(find_matches() != 0);
201 } while ((Short_only && fortlen() > SLEN) ||
202 (Long_only && fortlen() <= SLEN));
206 #ifdef OK_TO_WRITE_DISK
207 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
208 perror(Fortfile->posfile);
213 * if we can, we exclusive lock, but since it isn't very
214 * important, we just punt if we don't have easy locking
217 (void) flock(fd, LOCK_EX);
219 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
220 if (!Fortfile->was_pos_file)
221 (void) chmod(Fortfile->path, 0666);
223 (void) flock(fd, LOCK_UN);
225 #endif /* OK_TO_WRITE_DISK */
229 sleep((unsigned int) max(Fort_len / CPERS, MINW));
244 (void) fseek(fp->inf, Seekpts[0], 0);
245 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
246 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
247 if (fp->tbl.str_flags & STR_ROTATED)
248 for (p = line; (ch = *p) != '\0'; ++p) {
251 *p = 'A' + (ch - 'A' + 13) % 26;
252 else if (islower(ch))
253 *p = 'a' + (ch - 'a' + 13) % 26;
256 if (fp->tbl.str_flags & STR_COMMENTS
257 && line[0] == fp->tbl.str_delim
258 && line[1] == fp->tbl.str_delim)
262 (void) fflush(stdout);
267 * Return the length of the fortune.
275 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
276 nchar = (Seekpts[1] - Seekpts[0] <= SLEN);
279 (void) fseek(Fortfile->inf, Seekpts[0], 0);
281 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
282 !STR_ENDSTRING(line, Fortfile->tbl))
283 nchar += strlen(line);
290 * This routine evaluates the arguments on the command line
300 # endif /* NO_REGEX */
308 # endif /* NO_REGEX */
311 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
313 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
316 case 'a': /* any fortune */
325 Equal_probs++; /* scatter un-allocted prob equally */
327 case 'f': /* find fortune files */
330 case 'l': /* long ones only */
334 case 'o': /* offensive ones only */
337 case 's': /* short ones only */
341 case 'w': /* give time to read */
345 case 'i': /* case-insensitive match */
346 case 'm': /* dump out the fortunes */
347 (void) fprintf(stderr,
348 "fortune: can't match fortunes on this system (Sorry)\n");
350 # else /* NO_REGEX */
351 case 'm': /* dump out the fortunes */
355 case 'i': /* case-insensitive match */
358 # endif /* NO_REGEX */
366 if (!form_file_list(argv, argc))
367 exit(1); /* errors printed through form_file_list() */
381 if (BAD_COMP(RE_COMP(pat))) {
383 fprintf(stderr, "%s\n", pat);
385 fprintf(stderr, "bad pattern: %s\n", pat);
389 # endif /* NO_REGEX */
394 * Form the file list from the file specifications.
397 form_file_list(files, file_cnt)
406 Fortunes_only = TRUE;
407 i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
409 Fortunes_only = FALSE;
412 return add_file(NO_PROB, "fortunes", FORTDIR,
413 &File_list, &File_tail, NULL);
415 for (i = 0; i < file_cnt; i++) {
417 if (!isdigit((unsigned char)files[i][0]))
421 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
422 percent = percent * 10 + *sp - '0';
424 fprintf(stderr, "percentages must be <= 100\n");
428 fprintf(stderr, "percentages must be integers\n");
432 * If the number isn't followed by a '%', then
433 * it was not a percentage, just the first part
434 * of a file name which starts with digits.
440 else if (*++sp == '\0') {
441 if (++i >= file_cnt) {
442 fprintf(stderr, "percentages must precede files\n");
448 if (strcmp(sp, "all") == 0)
450 if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
458 * Add a file to the file list.
461 add_file(percent, file, dir, head, tail, parent)
465 FILEDESC **head, **tail;
470 char *path, *offensive;
479 path = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2));
480 (void) strcat(strcat(strcpy(path, dir), "/"), file);
483 if ((isdir = is_dir(path)) && parent != NULL) {
486 return FALSE; /* don't recurse */
489 if (!isdir && parent == NULL && (All_forts || Offend) &&
490 !is_off_name(path)) {
491 offensive = off_name(path);
498 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
499 file = off_name(file);
503 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
505 if ((fd = open(path, 0)) < 0) {
507 * This is a sneak. If the user said -a, and if the
508 * file we're given isn't a file, we check to see if
509 * there is a -o version. If there is, we treat it as
510 * if *that* were the file given. We only do this for
511 * individual files -- if we're scanning a directory,
512 * we'll pick up the -o file anyway.
514 if (All_forts && offensive != NULL) {
520 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
521 file = off_name(file);
524 if (dir == NULL && file[0] != '/')
525 return add_file(percent, file, FORTDIR, head, tail,
534 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
538 fp->percent = percent;
543 if ((isdir && !add_dir(fp)) ||
545 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
549 "fortune:%s not a fortune file or directory\n",
553 do_free(fp->datfile);
554 do_free(fp->posfile);
560 * If the user said -a, we need to make this node a pointer to
561 * both files, if there are two. We don't need to do this if
562 * we are scanning a directory, since the scan will pick up the
565 if (All_forts && parent == NULL && !is_off_name(path))
566 all_forts(fp, offensive);
569 else if (fp->percent == NO_PROB) {
579 #ifdef OK_TO_WRITE_DISK
580 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
581 #endif /* OK_TO_WRITE_DISK */
588 * Return a pointer to an initialized new FILEDESC.
595 fp = (FILEDESC *) do_malloc(sizeof *fp);
597 fp->pos = POS_UNKNOWN;
600 fp->percent = NO_PROB;
601 fp->read_tbl = FALSE;
613 * Return a pointer to the offensive version of a file of this name.
621 new = copy(file, (unsigned int) (strlen(file) + 2));
622 return strcat(new, "-o");
627 * Is the file an offensive-style name?
636 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
641 * Modify a FILEDESC element to be the parent of two children if
642 * there are two children to be a parent of.
645 all_forts(fp, offensive)
650 FILEDESC *scene, *obscene;
652 auto char *datfile, *posfile;
654 if (fp->child != NULL) /* this is a directory, not a file */
656 if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
658 if ((fd = open(offensive, 0)) < 0)
660 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
665 fp->num_children = 2;
667 scene->next = obscene;
668 obscene->next = NULL;
669 scene->child = obscene->child = NULL;
670 scene->parent = obscene->parent = fp;
673 scene->percent = obscene->percent = NO_PROB;
677 obscene->path = offensive;
678 if ((sp = rindex(offensive, '/')) == NULL)
679 obscene->name = offensive;
681 obscene->name = ++sp;
682 obscene->datfile = datfile;
683 obscene->posfile = posfile;
684 obscene->read_tbl = FALSE;
685 #ifdef OK_TO_WRITE_DISK
686 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
687 #endif /* OK_TO_WRITE_DISK */
692 * Add the contents of an entire directory.
699 struct dirent *dirent;
700 auto FILEDESC *tailp;
703 (void) close(fp->fd);
705 if ((dir = opendir(fp->path)) == NULL) {
710 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
711 fp->num_children = 0;
712 while ((dirent = readdir(dir)) != NULL) {
713 if (dirent->d_namlen == 0)
715 name = copy(dirent->d_name, dirent->d_namlen);
716 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
721 if (fp->num_children == 0) {
722 (void) fprintf(stderr,
723 "fortune: %s: No fortune files in directory.\n", fp->path);
731 * Return TRUE if the file is a directory, FALSE otherwise.
737 auto struct stat sbuf;
739 if (stat(file, &sbuf) < 0)
741 return (sbuf.st_mode & S_IFDIR);
746 * Return TRUE if the file is a fortune database file. We try and
747 * exclude files without reading them if possible to avoid
748 * overhead. Files which start with ".", or which have "illegal"
749 * suffixes, as contained in suflist[], are ruled out.
753 is_fortfile(file, datp, posp, check_for_offend)
754 char *file, **datp, **posp;
755 int check_for_offend;
760 static char *suflist[] = { /* list of "illegal" suffixes" */
761 "dat", "pos", "c", "h", "p", "i", "f",
762 "pas", "ftn", "ins.c", "ins,pas",
767 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
770 * Preclude any -o files for offendable people, and any non -o
771 * files for completely offensive people.
773 if (check_for_offend && !All_forts) {
775 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
776 DPRINTF(2, (stderr, "FALSE (offending file)\n"));
781 if ((sp = rindex(file, '/')) == NULL)
786 DPRINTF(2, (stderr, "FALSE (file starts with '.')\n"));
789 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
790 DPRINTF(2, (stderr, "FALSE (check fortunes only)\n"));
793 if ((sp = rindex(sp, '.')) != NULL) {
795 for (i = 0; suflist[i] != NULL; i++)
796 if (strcmp(sp, suflist[i]) == 0) {
797 DPRINTF(2, (stderr, "FALSE (file has suffix \".%s\")\n", sp));
802 datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
803 strcat(datfile, ".dat");
804 if (access(datfile, R_OK) < 0) {
805 DPRINTF(2, (stderr, "FALSE (no readable \".dat\" file)\n"));
808 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
818 #ifdef OK_TO_WRITE_DISK
819 *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
820 (void) strcat(*posp, ".pos");
823 #endif /* OK_TO_WRITE_DISK */
825 DPRINTF(2, (stderr, "TRUE\n"));
831 * Return a malloc()'ed copy of the string
840 new = do_malloc(len + 1);
850 * Do a malloc, checking for NULL return.
858 if ((new = malloc(size)) == NULL) {
859 (void) fprintf(stderr, "fortune: out of memory.\n");
867 * Free malloc'ed space, if any.
879 * Initialize the fortune probabilities.
884 FILEDESC *fp, *last = NULL;
885 int percent, num_noprob, frac;
888 * Distribute the residual probability (if any) across all
889 * files with unspecified probability (i.e., probability of 0)
895 for (fp = File_tail; fp != NULL; fp = fp->prev)
896 if (fp->percent == NO_PROB) {
902 percent += fp->percent;
903 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
904 percent, num_noprob));
906 (void) fprintf(stderr,
907 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
910 else if (percent < 100 && num_noprob == 0) {
911 (void) fprintf(stderr,
912 "fortune: no place to put residual probability (%d%% < 100%%)\n",
916 else if (percent == 100 && num_noprob != 0) {
917 (void) fprintf(stderr,
918 "fortune: no probability left to put in residual files (100%%)\n");
921 percent = 100 - percent;
923 if (num_noprob != 0) {
924 if (num_noprob > 1) {
925 frac = percent / num_noprob;
926 DPRINTF(1, (stderr, ", frac = %d%%", frac));
927 for (fp = File_list; fp != last; fp = fp->next)
928 if (fp->percent == NO_PROB) {
933 last->percent = percent;
934 DPRINTF(1, (stderr, ", residual = %d%%", percent));
938 ", %d%% distributed over remaining fortunes\n",
941 DPRINTF(1, (stderr, "\n"));
951 * Get the fortune data file's seek pointer for the next fortune.
959 if (File_list->next == NULL || File_list->percent == NO_PROB)
962 choice = random() % 100;
963 DPRINTF(1, (stderr, "choice = %d\n", choice));
964 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
965 if (choice < fp->percent)
968 choice -= fp->percent;
970 " skip \"%s\", %d%% (choice = %d)\n",
971 fp->name, fp->percent, choice));
974 "using \"%s\", %d%% (choice = %d)\n",
975 fp->name, fp->percent, choice));
977 if (fp->percent != NO_PROB)
980 if (fp->next != NULL) {
982 choice = random() % Noprob_tbl.str_numstr;
983 DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice,
984 Noprob_tbl.str_numstr));
985 while (choice >= fp->tbl.str_numstr) {
986 choice -= fp->tbl.str_numstr;
989 " skip \"%s\", %ld (choice = %d)\n",
990 fp->name, fp->tbl.str_numstr,
993 DPRINTF(1, (stderr, "using \"%s\", %ld\n", fp->name,
994 fp->tbl.str_numstr));
998 if (fp->child != NULL) {
999 DPRINTF(1, (stderr, "picking child\n"));
1000 fp = pick_child(fp);
1005 (void) lseek(fp->datfd,
1006 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0);
1007 read(fp->datfd, Seekpts, sizeof Seekpts);
1008 Seekpts[0] = ntohl(Seekpts[0]);
1009 Seekpts[1] = ntohl(Seekpts[1]);
1014 * Pick a child from a chosen parent.
1024 choice = random() % parent->num_children;
1025 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1026 choice, parent->num_children));
1027 for (fp = parent->child; choice--; fp = fp->next)
1029 DPRINTF(1, (stderr, " using %s\n", fp->name));
1034 choice = random() % parent->tbl.str_numstr;
1035 DPRINTF(1, (stderr, " choice = %d (of %ld)\n",
1036 choice, parent->tbl.str_numstr));
1037 for (fp = parent->child; choice >= fp->tbl.str_numstr;
1039 choice -= fp->tbl.str_numstr;
1040 DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n",
1041 fp->name, fp->tbl.str_numstr, choice));
1043 DPRINTF(1, (stderr, " using %s, %ld\n", fp->name,
1044 fp->tbl.str_numstr));
1051 * Sum up all the noprob probabilities, starting with fp.
1057 static bool did_noprobs = FALSE;
1061 zero_tbl(&Noprob_tbl);
1062 while (fp != NULL) {
1064 sum_tbl(&Noprob_tbl, &fp->tbl);
1074 return (i >= j ? i : j);
1079 * Assocatiate a FILE * with the given FILEDESC.
1085 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1093 * Open up the dat file if we need to.
1099 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, 0)) < 0) {
1100 perror(fp->datfile);
1107 * Get the position from the pos file, if there is one. If not,
1108 * return a random number.
1114 #ifdef OK_TO_WRITE_DISK
1116 #endif /* OK_TO_WRITE_DISK */
1118 assert(fp->read_tbl);
1119 if (fp->pos == POS_UNKNOWN) {
1120 #ifdef OK_TO_WRITE_DISK
1121 if ((fd = open(fp->posfile, 0)) < 0 ||
1122 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1123 fp->pos = random() % fp->tbl.str_numstr;
1124 else if (fp->pos >= fp->tbl.str_numstr)
1125 fp->pos %= fp->tbl.str_numstr;
1129 fp->pos = random() % fp->tbl.str_numstr;
1130 #endif /* OK_TO_WRITE_DISK */
1132 if (++(fp->pos) >= fp->tbl.str_numstr)
1133 fp->pos -= fp->tbl.str_numstr;
1134 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, fp->pos));
1139 * Get the tbl data file the datfile.
1150 if (fp->child == NULL) {
1151 if ((fd = open(fp->datfile, 0)) < 0) {
1152 perror(fp->datfile);
1155 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1156 (void)fprintf(stderr,
1157 "fortune: %s corrupted\n", fp->path);
1160 /* fp->tbl.str_version = ntohl(fp->tbl.str_version); */
1161 fp->tbl.str_numstr = ntohl(fp->tbl.str_numstr);
1162 fp->tbl.str_longlen = ntohl(fp->tbl.str_longlen);
1163 fp->tbl.str_shortlen = ntohl(fp->tbl.str_shortlen);
1164 fp->tbl.str_flags = ntohl(fp->tbl.str_flags);
1169 for (child = fp->child; child != NULL; child = child->next) {
1171 sum_tbl(&fp->tbl, &child->tbl);
1174 fp->read_tbl = TRUE;
1179 * Zero out the fields we care about in a tbl structure.
1186 tp->str_longlen = 0;
1187 tp->str_shortlen = ~((unsigned long)0);
1192 * Merge the tbl data of t2 into t1.
1198 t1->str_numstr += t2->str_numstr;
1199 if (t1->str_longlen < t2->str_longlen)
1200 t1->str_longlen = t2->str_longlen;
1201 if (t1->str_shortlen > t2->str_shortlen)
1202 t1->str_shortlen = t2->str_shortlen;
1205 #define STR(str) ((str) == NULL ? "NULL" : (str))
1209 * Print out the file list
1214 print_list(File_list, 0);
1219 * Print out the actual list, recursively.
1222 print_list(list, lev)
1226 while (list != NULL) {
1227 fprintf(stderr, "%*s", lev * 4, "");
1228 if (list->percent == NO_PROB)
1229 fprintf(stderr, "___%%");
1231 fprintf(stderr, "%3d%%", list->percent);
1232 fprintf(stderr, " %s", STR(list->name));
1233 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1234 STR(list->datfile), STR(list->posfile)));
1235 fprintf(stderr, "\n");
1236 if (list->child != NULL)
1237 print_list(list->child, lev + 1);
1245 * Convert the pattern to an ignore-case equivalent.
1255 cnt = 1; /* allow for '\0' */
1256 for (sp = orig; *sp != '\0'; sp++)
1257 if (isalpha((unsigned char)*sp))
1261 if ((new = malloc(cnt)) == NULL) {
1262 fprintf(stderr, "pattern too long for ignoring case\n");
1266 for (sp = new; *orig != '\0'; orig++) {
1267 if (islower((unsigned char)*orig)) {
1270 *sp++ = toupper((unsigned char)*orig);
1273 else if (isupper((unsigned char)*orig)) {
1276 *sp++ = tolower((unsigned char)*orig);
1288 * Find all the fortunes which match the pattern we've been given.
1293 Fort_len = maxlen_in_list(File_list);
1294 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1295 /* extra length, "%\n" is appended */
1296 Fortbuf = do_malloc((unsigned int) Fort_len + 10);
1299 matches_in_list(File_list);
1306 * Return the maximum fortune len in the file list.
1309 maxlen_in_list(list)
1316 for (fp = list; fp != NULL; fp = fp->next) {
1317 if (fp->child != NULL) {
1318 if ((len = maxlen_in_list(fp->child)) > maxlen)
1323 if (fp->tbl.str_longlen > maxlen)
1324 maxlen = fp->tbl.str_longlen;
1332 * Print out the matches from the files in the list.
1335 matches_in_list(list)
1343 for (fp = list; fp != NULL; fp = fp->next) {
1344 if (fp->child != NULL) {
1345 matches_in_list(fp->child);
1348 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1352 while (fgets(sp, Fort_len, fp->inf) != NULL)
1353 if (fp->tbl.str_flags & STR_COMMENTS
1354 && sp[0] == fp->tbl.str_delim
1355 && sp[1] == fp->tbl.str_delim)
1357 else if (!STR_ENDSTRING(sp, fp->tbl))
1361 if (fp->tbl.str_flags & STR_ROTATED)
1362 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1365 *p = 'A' + (ch - 'A' + 13) % 26;
1366 else if (islower(ch))
1367 *p = 'a' + (ch - 'a' + 13) % 26;
1370 if (RE_EXEC(Fortbuf)) {
1371 printf("%c%c", fp->tbl.str_delim,
1374 printf(" (%s)", fp->name);
1379 (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1385 # endif /* NO_REGEX */
1390 (void) fprintf(stderr, "fortune [-a");
1392 (void) fprintf(stderr, "D");
1394 (void) fprintf(stderr, "f");
1396 (void) fprintf(stderr, "i");
1397 #endif /* NO_REGEX */
1398 (void) fprintf(stderr, "losw]");
1400 (void) fprintf(stderr, " [-m pattern]");
1401 #endif /* NO_REGEX */
1402 (void) fprintf(stderr, "[[#%%] file/directory/all]\n");