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>
56 #include "pathnames.h"
58 #define MINW 6 /* minimum wait if desired */
59 #define CPERS 20 /* # of chars for each sec */
60 #define SLEN 160 /* # of chars in short fortune */
62 #define POS_UNKNOWN ((long)-1) /* pos for file unknown */
63 #define NO_PROB (-1) /* no prob specified for file */
66 #define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
80 char *datfile, *posfile;
85 struct fd *child, *parent;
86 struct fd *next, *prev;
89 static bool Found_one; /* did we find a match? */
90 static bool Find_files = false; /* just find a list of proper fortune files */
91 static bool Fortunes_only = false; /* check only "fortunes" files */
92 static bool Wait = false; /* wait desired after fortune */
93 static bool Short_only = false; /* short fortune desired */
94 static bool Long_only = false; /* long fortune desired */
95 static bool Offend = false; /* offensive fortunes only */
96 static bool All_forts = false; /* any fortune allowed */
97 static bool Equal_probs = false; /* scatter un-allocted prob equally */
98 static bool Match = false; /* dump fortunes matching a pattern */
99 static bool WriteToDisk = false; /* use files on disk to save state */
101 static int Debug = 0; /* print debug messages */
104 static char *Fortbuf = NULL; /* fortune buffer for -m */
106 static int Fort_len = 0;
108 static long Seekpts[2]; /* seek pointers to fortunes */
110 static FILEDESC *File_list = NULL; /* Head of file list */
111 static FILEDESC *File_tail = NULL; /* Tail of file list */
112 static FILEDESC *Fortfile; /* Fortune file to use */
114 static STRFILE Noprob_tbl; /* sum of data for all no prob files */
116 static bool add_dir(FILEDESC *);
117 static bool add_file(int, const char *, const char *,
118 FILEDESC **, FILEDESC **, FILEDESC *);
119 static void all_forts(FILEDESC *, char *);
120 static char *copy(const char *, u_int);
121 static void display(FILEDESC *);
122 static void do_free(void *);
123 static void *do_malloc(u_int);
124 static bool form_file_list(char **, int);
125 static int fortlen(void);
126 static void get_fort(void);
127 static void get_pos(FILEDESC *);
128 static void get_tbl(FILEDESC *);
129 static void getargs(int, char *[]);
130 static void init_prob(void);
131 static bool is_dir(const char *);
132 static bool is_fortfile(const char *, char **, char **, int);
133 static bool is_off_name(const char *);
134 static int max(int, int);
135 static FILEDESC *new_fp(void);
136 static char *off_name(const char *);
137 static void open_dat(FILEDESC *);
138 static void open_fp(FILEDESC *);
139 static FILEDESC *pick_child(FILEDESC *);
140 static void print_file_list(void);
141 static void print_list(FILEDESC *, int);
142 static void sum_noprobs(FILEDESC *);
143 static void sum_tbl(STRFILE *, STRFILE *);
144 static void usage(void);
145 static void zero_tbl(STRFILE *);
147 static char *conv_pat(char *);
148 static int find_matches(void);
149 static void matches_in_list(FILEDESC *);
150 static int maxlen_in_list(FILEDESC *);
152 static regex_t Re_pat;
155 main(int argc, char *argv[])
159 if (getenv("FORTUNE_SAVESTATE") != NULL)
162 setlocale(LC_ALL, "");
167 exit(find_matches() != 0);
173 } while ((Short_only && fortlen() > SLEN) ||
174 (Long_only && fortlen() <= SLEN));
179 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
180 perror(Fortfile->posfile);
184 * if we can, we exclusive lock, but since it isn't very
185 * important, we just punt if we don't have easy locking
189 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
190 if (!Fortfile->was_pos_file)
191 chmod(Fortfile->path, 0666);
198 sleep((unsigned int)max(Fort_len / CPERS, MINW));
205 display(FILEDESC *fp)
212 fseek(fp->inf, Seekpts[0], SEEK_SET);
213 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
214 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
215 if (fp->tbl.str_flags & STR_ROTATED)
216 for (p = line; (ch = *p) != '\0'; ++p) {
219 *p = 'A' + (ch - 'A' + 13) % 26;
220 else if (islower(ch))
221 *p = 'a' + (ch - 'a' + 13) % 26;
224 if (fp->tbl.str_flags & STR_COMMENTS
225 && line[0] == fp->tbl.str_delim
226 && line[1] == fp->tbl.str_delim)
235 * Return the length of the fortune.
243 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
244 nchar = (Seekpts[1] - Seekpts[0] <= SLEN);
247 fseek(Fortfile->inf, Seekpts[0], SEEK_SET);
249 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
250 !STR_ENDSTRING(line, Fortfile->tbl))
251 nchar += strlen(line);
259 * This routine evaluates the arguments on the command line
262 getargs(int argc, char *argv[])
272 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
274 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
277 case 'a': /* any fortune */
286 Equal_probs++; /* scatter un-allocted prob equally */
288 case 'f': /* find fortune files */
291 case 'l': /* long ones only */
295 case 'o': /* offensive ones only */
298 case 's': /* short ones only */
302 case 'w': /* give time to read */
305 case 'm': /* dump out the fortunes */
309 case 'i': /* case-insensitive match */
319 if (!form_file_list(argv, argc))
320 exit(1); /* errors printed through form_file_list() */
335 error = regcomp(&Re_pat, pat, REG_BASIC);
337 fprintf(stderr, "regcomp(%s) fails\n", pat);
345 * Form the file list from the file specifications.
348 form_file_list(char **files, int file_cnt)
356 Fortunes_only = true;
357 i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
359 Fortunes_only = false;
362 return (add_file(NO_PROB, "fortunes", FORTDIR,
363 &File_list, &File_tail, NULL));
365 for (i = 0; i < file_cnt; i++) {
367 if (!isdigit((unsigned char)files[i][0]))
371 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
372 percent = percent * 10 + *sp - '0';
374 fprintf(stderr, "percentages must be <= 100\n");
378 fprintf(stderr, "percentages must be integers\n");
382 * If the number isn't followed by a '%', then
383 * it was not a percentage, just the first part
384 * of a file name which starts with digits.
390 else if (*++sp == '\0') {
391 if (++i >= file_cnt) {
392 fprintf(stderr, "percentages must precede files\n");
398 if (strcmp(sp, "all") == 0)
400 if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
409 * Add a file to the file list.
412 add_file(int percent, const char *file, const char *dir,
413 FILEDESC **head, FILEDESC **tail, FILEDESC *parent)
418 char *tpath, *offensive;
428 tpath = do_malloc((unsigned int)(strlen(dir) + strlen(file) + 2));
429 strcat(strcat(strcpy(tpath, dir), "/"), file);
433 if ((isdir = is_dir(path)) && parent != NULL) {
436 return (false); /* don't recurse */
439 if (!isdir && parent == NULL && (All_forts || Offend) &&
440 !is_off_name(path)) {
441 offensive = off_name(path);
448 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
449 file = off_name(file);
453 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
455 if ((fd = open(path, O_RDONLY)) < 0) {
457 * This is a sneak. If the user said -a, and if the
458 * file we're given isn't a file, we check to see if
459 * there is a -o version. If there is, we treat it as
460 * if *that* were the file given. We only do this for
461 * individual files -- if we're scanning a directory,
462 * we'll pick up the -o file anyway.
464 if (All_forts && offensive != NULL) {
470 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
471 file = off_name(file);
474 if (dir == NULL && file[0] != '/')
475 return (add_file(percent, file, FORTDIR, head, tail,
484 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
488 fp->percent = percent;
493 if ((isdir && !add_dir(fp)) ||
495 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
499 "fortune:%s not a fortune file or directory\n",
503 do_free(fp->datfile);
504 do_free(fp->posfile);
510 * If the user said -a, we need to make this node a pointer to
511 * both files, if there are two. We don't need to do this if
512 * we are scanning a directory, since the scan will pick up the
515 if (All_forts && parent == NULL && !is_off_name(path))
516 all_forts(fp, offensive);
519 else if (fp->percent == NO_PROB) {
530 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
537 * Return a pointer to an initialized new FILEDESC.
544 fp = (FILEDESC *)do_malloc(sizeof(*fp));
546 fp->pos = POS_UNKNOWN;
549 fp->percent = NO_PROB;
550 fp->read_tbl = false;
563 * Return a pointer to the offensive version of a file of this name.
566 off_name(const char *file)
570 new = copy(file, (unsigned int)(strlen(file) + 2));
572 return (strcat(new, "-o"));
577 * Is the file an offensive-style name?
580 is_off_name(const char *file)
586 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
591 * Modify a FILEDESC element to be the parent of two children if
592 * there are two children to be a parent of.
595 all_forts(FILEDESC *fp, char *offensive)
598 FILEDESC *scene, *obscene;
600 char *datfile, *posfile;
602 if (fp->child != NULL) /* this is a directory, not a file */
604 if (!is_fortfile(offensive, &datfile, &posfile, false))
606 if ((fd = open(offensive, O_RDONLY)) < 0)
608 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
613 fp->num_children = 2;
615 scene->next = obscene;
616 obscene->next = NULL;
617 scene->child = obscene->child = NULL;
618 scene->parent = obscene->parent = fp;
621 scene->percent = obscene->percent = NO_PROB;
625 obscene->path = offensive;
626 if ((sp = rindex(offensive, '/')) == NULL)
627 obscene->name = offensive;
629 obscene->name = ++sp;
630 obscene->datfile = datfile;
631 obscene->posfile = posfile;
632 obscene->read_tbl = false;
634 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
639 * Add the contents of an entire directory.
642 add_dir(FILEDESC *fp)
645 struct dirent *dirent;
651 if ((dir = opendir(fp->path)) == NULL) {
656 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
657 fp->num_children = 0;
658 while ((dirent = readdir(dir)) != NULL) {
659 if ((name = strdup(dirent->d_name)) == NULL)
660 err(1, "strdup failed");
661 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
666 if (fp->num_children == 0) {
668 "fortune: %s: No fortune files in directory.\n", fp->path);
677 * Return true if the file is a directory, false otherwise.
680 is_dir(const char *file)
684 if (stat(file, &sbuf) < 0)
687 return (sbuf.st_mode & S_IFDIR);
692 * Return true if the file is a fortune database file. We try and
693 * exclude files without reading them if possible to avoid
694 * overhead. Files which start with ".", or which have "illegal"
695 * suffixes, as contained in suflist[], are ruled out.
699 is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
704 static const char *suflist[] = {
705 /* list of "illegal" suffixes" */
706 "dat", "pos", "c", "h", "p", "i", "f",
707 "pas", "ftn", "ins.c", "ins,pas",
712 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
715 * Preclude any -o files for offendable people, and any non -o
716 * files for completely offensive people.
718 if (check_for_offend && !All_forts) {
720 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
721 DPRINTF(2, (stderr, "false (offending file)\n"));
726 if ((sp = rindex(file, '/')) == NULL)
731 DPRINTF(2, (stderr, "false (file starts with '.')\n"));
734 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
735 DPRINTF(2, (stderr, "false (check fortunes only)\n"));
738 if ((sp = rindex(sp, '.')) != NULL) {
740 for (i = 0; suflist[i] != NULL; i++)
741 if (strcmp(sp, suflist[i]) == 0) {
742 DPRINTF(2, (stderr, "false (file has suffix \".%s\")\n", sp));
747 datfile = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */
748 strcat(datfile, ".dat");
749 if (access(datfile, R_OK) < 0) {
750 DPRINTF(2, (stderr, "false (no readable \".dat\" file)\n"));
753 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
764 *posp = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */
765 strcat(*posp, ".pos");
770 DPRINTF(2, (stderr, "true\n"));
777 * Return a malloc()'ed copy of the string
780 copy(const char *str, unsigned int len)
784 new = do_malloc(len + 1);
795 * Do a malloc, checking for NULL return.
798 do_malloc(unsigned int size)
802 if ((new = malloc(size)) == NULL) {
803 fprintf(stderr, "fortune: out of memory.\n");
812 * Free malloc'ed space, if any.
823 * Initialize the fortune probabilities.
829 int percent, num_noprob, frac;
833 * Distribute the residual probability (if any) across all
834 * files with unspecified probability (i.e., probability of 0)
840 for (fp = File_tail; fp != NULL; fp = fp->prev)
841 if (fp->percent == NO_PROB) {
846 percent += fp->percent;
847 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
848 percent, num_noprob));
851 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
853 } else if (percent < 100 && num_noprob == 0) {
855 "fortune: no place to put residual probability (%d%% < 100%%)\n",
858 } else if (percent == 100 && num_noprob != 0) {
860 "fortune: no probability left to put in residual files (100%%)\n");
863 percent = 100 - percent;
865 if (num_noprob != 0) {
866 if (num_noprob > 1) {
867 frac = percent / num_noprob;
868 DPRINTF(1, (stderr, ", frac = %d%%", frac));
869 for (fp = File_list; fp != last; fp = fp->next)
870 if (fp->percent == NO_PROB) {
875 last->percent = percent;
876 DPRINTF(1, (stderr, ", residual = %d%%", percent));
880 ", %d%% distributed over remaining fortunes\n",
883 DPRINTF(1, (stderr, "\n"));
893 * Get the fortune data file's seek pointer for the next fortune.
901 if (File_list->next == NULL || File_list->percent == NO_PROB)
904 choice = random() % 100;
905 DPRINTF(1, (stderr, "choice = %d\n", choice));
906 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
907 if (choice < fp->percent)
910 choice -= fp->percent;
912 " skip \"%s\", %d%% (choice = %d)\n",
913 fp->name, fp->percent, choice));
915 DPRINTF(1, (stderr, "using \"%s\", %d%% (choice = %d)\n",
916 fp->name, fp->percent, choice));
918 if (fp->percent != NO_PROB)
921 if (fp->next != NULL) {
923 choice = random() % Noprob_tbl.str_numstr;
924 DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice,
925 Noprob_tbl.str_numstr));
926 while ((unsigned int)choice >= fp->tbl.str_numstr) {
927 choice -= fp->tbl.str_numstr;
930 " skip \"%s\", %ld (choice = %d)\n",
931 fp->name, fp->tbl.str_numstr,
934 DPRINTF(1, (stderr, "using \"%s\", %ld\n", fp->name,
935 fp->tbl.str_numstr));
939 if (fp->child != NULL) {
940 DPRINTF(1, (stderr, "picking child\n"));
947 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), SEEK_SET);
948 read(fp->datfd, Seekpts, sizeof Seekpts);
949 Seekpts[0] = ntohl(Seekpts[0]);
950 Seekpts[1] = ntohl(Seekpts[1]);
955 * Pick a child from a chosen parent.
958 pick_child(FILEDESC *parent)
964 choice = random() % parent->num_children;
965 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
966 choice, parent->num_children));
967 for (fp = parent->child; choice--; fp = fp->next)
969 DPRINTF(1, (stderr, " using %s\n", fp->name));
974 choice = random() % parent->tbl.str_numstr;
975 DPRINTF(1, (stderr, " choice = %d (of %ld)\n",
976 choice, parent->tbl.str_numstr));
977 for (fp = parent->child; (unsigned int)choice >= fp->tbl.str_numstr;
979 choice -= fp->tbl.str_numstr;
980 DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n",
981 fp->name, fp->tbl.str_numstr, choice));
983 DPRINTF(1, (stderr, " using %s, %ld\n", fp->name,
984 fp->tbl.str_numstr));
991 * Sum up all the noprob probabilities, starting with fp.
994 sum_noprobs(FILEDESC *fp)
996 static bool did_noprobs = false;
1000 zero_tbl(&Noprob_tbl);
1001 while (fp != NULL) {
1003 sum_tbl(&Noprob_tbl, &fp->tbl);
1012 return (i >= j ? i : j);
1017 * Assocatiate a FILE * with the given FILEDESC.
1020 open_fp(FILEDESC *fp)
1022 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1030 * Open up the dat file if we need to.
1033 open_dat(FILEDESC *fp)
1035 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY)) < 0) {
1036 perror(fp->datfile);
1043 * Get the position from the pos file, if there is one. If not,
1044 * return a random number.
1047 get_pos(FILEDESC *fp)
1051 assert(fp->read_tbl);
1052 if (fp->pos == POS_UNKNOWN) {
1054 if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
1055 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1056 fp->pos = random() % fp->tbl.str_numstr;
1057 else if ((unsigned int)fp->pos >= fp->tbl.str_numstr)
1058 fp->pos %= fp->tbl.str_numstr;
1062 fp->pos = random() % fp->tbl.str_numstr;
1065 if ((unsigned int)++(fp->pos) >= fp->tbl.str_numstr)
1066 fp->pos -= fp->tbl.str_numstr;
1067 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, fp->pos));
1072 * Get the tbl data file the datfile.
1075 get_tbl(FILEDESC *fp)
1082 if (fp->child == NULL) {
1083 if ((fd = open(fp->datfile, O_RDONLY)) < 0) {
1084 perror(fp->datfile);
1087 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1089 "fortune: %s corrupted\n", fp->path);
1092 /* fp->tbl.str_version = ntohl(fp->tbl.str_version); */
1093 fp->tbl.str_numstr = ntohl(fp->tbl.str_numstr);
1094 fp->tbl.str_longlen = ntohl(fp->tbl.str_longlen);
1095 fp->tbl.str_shortlen = ntohl(fp->tbl.str_shortlen);
1096 fp->tbl.str_flags = ntohl(fp->tbl.str_flags);
1101 for (child = fp->child; child != NULL; child = child->next) {
1103 sum_tbl(&fp->tbl, &child->tbl);
1106 fp->read_tbl = true;
1111 * Zero out the fields we care about in a tbl structure.
1114 zero_tbl(STRFILE *tp)
1117 tp->str_longlen = 0;
1118 tp->str_shortlen = ~((unsigned long)0);
1123 * Merge the tbl data of t2 into t1.
1126 sum_tbl(STRFILE *t1, STRFILE *t2)
1128 t1->str_numstr += t2->str_numstr;
1129 if (t1->str_longlen < t2->str_longlen)
1130 t1->str_longlen = t2->str_longlen;
1131 if (t1->str_shortlen > t2->str_shortlen)
1132 t1->str_shortlen = t2->str_shortlen;
1135 #define STR(str) ((str) == NULL ? "NULL" : (str))
1139 * Print out the file list
1142 print_file_list(void)
1144 print_list(File_list, 0);
1149 * Print out the actual list, recursively.
1152 print_list(FILEDESC *list, int lev)
1154 while (list != NULL) {
1155 fprintf(stderr, "%*s", lev * 4, "");
1156 if (list->percent == NO_PROB)
1157 fprintf(stderr, "___%%");
1159 fprintf(stderr, "%3d%%", list->percent);
1160 fprintf(stderr, " %s", STR(list->name));
1161 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1162 STR(list->datfile), STR(list->posfile)));
1163 fprintf(stderr, "\n");
1164 if (list->child != NULL)
1165 print_list(list->child, lev + 1);
1172 * Convert the pattern to an ignore-case equivalent.
1175 conv_pat(char *orig)
1181 cnt = 1; /* allow for '\0' */
1182 for (sp = orig; *sp != '\0'; sp++)
1183 if (isalpha((unsigned char)*sp))
1187 if ((new = malloc(cnt)) == NULL) {
1188 fprintf(stderr, "pattern too long for ignoring case\n");
1192 for (sp = new; *orig != '\0'; orig++) {
1193 if (islower((unsigned char)*orig)) {
1196 *sp++ = toupper((unsigned char)*orig);
1199 else if (isupper((unsigned char)*orig)) {
1202 *sp++ = tolower((unsigned char)*orig);
1215 * Find all the fortunes which match the pattern we've been given.
1220 Fort_len = maxlen_in_list(File_list);
1221 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1222 /* extra length, "%\n" is appended */
1223 Fortbuf = do_malloc((unsigned int)Fort_len + 10);
1226 matches_in_list(File_list);
1233 * Return the maximum fortune len in the file list.
1236 maxlen_in_list(FILEDESC *list)
1242 for (fp = list; fp != NULL; fp = fp->next) {
1243 if (fp->child != NULL) {
1244 if ((len = maxlen_in_list(fp->child)) > maxlen)
1249 if (fp->tbl.str_longlen > (unsigned int)maxlen)
1250 maxlen = fp->tbl.str_longlen;
1259 * Print out the matches from the files in the list.
1262 matches_in_list(FILEDESC *list)
1269 for (fp = list; fp != NULL; fp = fp->next) {
1270 if (fp->child != NULL) {
1271 matches_in_list(fp->child);
1274 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1278 while (fgets(sp, Fort_len, fp->inf) != NULL)
1279 if (fp->tbl.str_flags & STR_COMMENTS
1280 && sp[0] == fp->tbl.str_delim
1281 && sp[1] == fp->tbl.str_delim)
1283 else if (!STR_ENDSTRING(sp, fp->tbl))
1287 if (fp->tbl.str_flags & STR_ROTATED)
1288 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1291 *p = 'A' + (ch - 'A' + 13) % 26;
1292 else if (islower(ch))
1293 *p = 'a' + (ch - 'a' + 13) % 26;
1296 if (regexec(&Re_pat, Fortbuf, 0, NULL, 0) != REG_NOMATCH) {
1297 printf("%c%c", fp->tbl.str_delim,
1300 printf(" (%s)", fp->name);
1305 fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1315 fprintf(stderr, "fortune [-a");
1317 fprintf(stderr, "D");
1319 fprintf(stderr, "f");
1320 fprintf(stderr, "i");
1321 fprintf(stderr, "losw]");
1322 fprintf(stderr, " [-m pattern]");
1323 fprintf(stderr, "[[#%%] file/directory/all]\n");