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
39 static const char copyright[] =
40 "@(#) Copyright (c) 1986, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 # include <sys/stat.h>
52 # include <sys/endian.h>
66 # include "pathnames.h"
72 # define MINW 6 /* minimum wait if desired */
73 # define CPERS 20 /* # of chars for each sec */
74 # define SLEN 160 /* # of chars in short fortune */
76 # define POS_UNKNOWN ((uint32_t) -1) /* pos for file unknown */
77 # define NO_PROB (-1) /* no prob specified for file */
80 # define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
94 char *datfile, *posfile;
99 struct fd *child, *parent;
100 struct fd *next, *prev;
103 bool Found_one; /* did we find a match? */
104 bool Find_files = FALSE; /* just find a list of proper fortune files */
105 bool Fortunes_only = FALSE; /* check only "fortunes" files */
106 bool Wait = FALSE; /* wait desired after fortune */
107 bool Short_only = FALSE; /* short fortune desired */
108 bool Long_only = FALSE; /* long fortune desired */
109 bool Offend = FALSE; /* offensive fortunes only */
110 bool All_forts = FALSE; /* any fortune allowed */
111 bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
112 bool Match = FALSE; /* dump fortunes matching a pattern */
114 bool Debug = FALSE; /* print debug messages */
117 char *Fortbuf = NULL; /* fortune buffer for -m */
121 off_t Seekpts[2]; /* seek pointers to fortunes */
123 FILEDESC *File_list = NULL, /* Head of file list */
124 *File_tail = NULL; /* Tail of file list */
125 FILEDESC *Fortfile; /* Fortune file to use */
127 STRFILE Noprob_tbl; /* sum of data for all no prob files */
129 int add_dir(FILEDESC *);
130 int add_file __P((int,
131 char *, char *, FILEDESC **, FILEDESC **, FILEDESC *));
132 void all_forts(FILEDESC *, char *);
133 char *copy(char *, u_int);
134 void display(FILEDESC *);
135 void do_free(void *);
136 void *do_malloc(u_int);
137 int form_file_list(char **, int);
140 void get_pos(FILEDESC *);
141 void get_tbl(FILEDESC *);
142 void getargs(int, char *[]);
143 void init_prob(void);
145 int is_fortfile(char *, char **, char **, int);
146 int is_off_name(char *);
150 char *off_name(char *);
151 void open_dat(FILEDESC *);
152 void open_fp(FILEDESC *);
154 pick_child(FILEDESC *);
155 void print_file_list(void);
156 void print_list(FILEDESC *, int);
157 void sum_noprobs(FILEDESC *);
158 void sum_tbl(STRFILE *, STRFILE *);
160 void zero_tbl(STRFILE *);
162 char *conv_pat(char *);
163 int find_matches(void);
164 void matches_in_list(FILEDESC *);
165 int maxlen_in_list(FILEDESC *);
167 static regex_t Re_pat;
174 #ifdef OK_TO_WRITE_DISK
176 #endif /* OK_TO_WRITE_DISK */
178 (void) setlocale(LC_ALL, "");
183 exit(find_matches() != 0);
189 } while ((Short_only && fortlen() > SLEN) ||
190 (Long_only && fortlen() <= SLEN));
194 #ifdef OK_TO_WRITE_DISK
195 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
196 perror(Fortfile->posfile);
201 * if we can, we exclusive lock, but since it isn't very
202 * important, we just punt if we don't have easy locking
205 (void) flock(fd, LOCK_EX);
207 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
208 if (!Fortfile->was_pos_file)
209 (void) chmod(Fortfile->path, 0666);
211 (void) flock(fd, LOCK_UN);
213 #endif /* OK_TO_WRITE_DISK */
217 sleep((unsigned int) max(Fort_len / CPERS, MINW));
232 (void) fseeko(fp->inf, Seekpts[0], 0);
233 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
234 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
235 if (fp->tbl.str_flags & STR_ROTATED)
236 for (p = line; (ch = *p) != '\0'; ++p) {
239 *p = 'A' + (ch - 'A' + 13) % 26;
240 else if (islower(ch))
241 *p = 'a' + (ch - 'a' + 13) % 26;
244 if (fp->tbl.str_flags & STR_COMMENTS
245 && line[0] == fp->tbl.str_delim
246 && line[1] == fp->tbl.str_delim)
250 (void) fflush(stdout);
255 * Return the length of the fortune.
263 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
264 nchar = (int)(Seekpts[1] - Seekpts[0]);
267 (void) fseeko(Fortfile->inf, Seekpts[0], 0);
269 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
270 !STR_ENDSTRING(line, Fortfile->tbl))
271 nchar += strlen(line);
278 * This routine evaluates the arguments on the command line
295 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
297 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
300 case 'a': /* any fortune */
309 Equal_probs++; /* scatter un-allocted prob equally */
311 case 'f': /* find fortune files */
314 case 'l': /* long ones only */
318 case 'o': /* offensive ones only */
321 case 's': /* short ones only */
325 case 'w': /* give time to read */
328 case 'm': /* dump out the fortunes */
332 case 'i': /* case-insensitive match */
342 if (!form_file_list(argv, argc))
343 exit(1); /* errors printed through form_file_list() */
358 error = regcomp(&Re_pat, pat, REG_BASIC);
360 fprintf(stderr, "regcomp(%s) fails\n", pat);
368 * Form the file list from the file specifications.
371 form_file_list(files, file_cnt)
380 Fortunes_only = TRUE;
381 i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
383 Fortunes_only = FALSE;
386 return add_file(NO_PROB, "fortunes", FORTDIR,
387 &File_list, &File_tail, NULL);
389 for (i = 0; i < file_cnt; i++) {
391 if (!isdigit((unsigned char)files[i][0]))
395 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
396 percent = percent * 10 + *sp - '0';
398 fprintf(stderr, "percentages must be <= 100\n");
402 fprintf(stderr, "percentages must be integers\n");
406 * If the number isn't followed by a '%', then
407 * it was not a percentage, just the first part
408 * of a file name which starts with digits.
414 else if (*++sp == '\0') {
415 if (++i >= file_cnt) {
416 fprintf(stderr, "percentages must precede files\n");
422 if (strcmp(sp, "all") == 0)
424 if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
432 * Add a file to the file list.
435 add_file(percent, file, dir, head, tail, parent)
439 FILEDESC **head, **tail;
444 char *path, *offensive;
453 path = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2));
454 (void) strcat(strcat(strcpy(path, dir), "/"), file);
457 if ((isdir = is_dir(path)) && parent != NULL) {
460 return FALSE; /* don't recurse */
463 if (!isdir && parent == NULL && (All_forts || Offend) &&
464 !is_off_name(path)) {
465 offensive = off_name(path);
472 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
473 file = off_name(file);
477 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
479 if ((fd = open(path, 0)) < 0) {
481 * This is a sneak. If the user said -a, and if the
482 * file we're given isn't a file, we check to see if
483 * there is a -o version. If there is, we treat it as
484 * if *that* were the file given. We only do this for
485 * individual files -- if we're scanning a directory,
486 * we'll pick up the -o file anyway.
488 if (All_forts && offensive != NULL) {
494 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
495 file = off_name(file);
498 if (dir == NULL && file[0] != '/')
499 return add_file(percent, file, FORTDIR, head, tail,
508 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
512 fp->percent = percent;
517 if ((isdir && !add_dir(fp)) ||
519 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
523 "fortune:%s not a fortune file or directory\n",
527 do_free(fp->datfile);
528 do_free(fp->posfile);
534 * If the user said -a, we need to make this node a pointer to
535 * both files, if there are two. We don't need to do this if
536 * we are scanning a directory, since the scan will pick up the
539 if (All_forts && parent == NULL && !is_off_name(path))
540 all_forts(fp, offensive);
543 else if (fp->percent == NO_PROB) {
553 #ifdef OK_TO_WRITE_DISK
554 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
555 #endif /* OK_TO_WRITE_DISK */
562 * Return a pointer to an initialized new FILEDESC.
569 fp = (FILEDESC *) do_malloc(sizeof *fp);
571 fp->pos = POS_UNKNOWN;
574 fp->percent = NO_PROB;
575 fp->read_tbl = FALSE;
587 * Return a pointer to the offensive version of a file of this name.
595 new = copy(file, (unsigned int) (strlen(file) + 2));
596 return strcat(new, "-o");
601 * Is the file an offensive-style name?
610 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
615 * Modify a FILEDESC element to be the parent of two children if
616 * there are two children to be a parent of.
619 all_forts(fp, offensive)
624 FILEDESC *scene, *obscene;
626 auto char *datfile, *posfile;
628 if (fp->child != NULL) /* this is a directory, not a file */
630 if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
632 if ((fd = open(offensive, 0)) < 0)
634 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
639 fp->num_children = 2;
641 scene->next = obscene;
642 obscene->next = NULL;
643 scene->child = obscene->child = NULL;
644 scene->parent = obscene->parent = fp;
647 scene->percent = obscene->percent = NO_PROB;
651 obscene->path = offensive;
652 if ((sp = rindex(offensive, '/')) == NULL)
653 obscene->name = offensive;
655 obscene->name = ++sp;
656 obscene->datfile = datfile;
657 obscene->posfile = posfile;
658 obscene->read_tbl = FALSE;
659 #ifdef OK_TO_WRITE_DISK
660 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
661 #endif /* OK_TO_WRITE_DISK */
666 * Add the contents of an entire directory.
673 struct dirent *dirent;
674 auto FILEDESC *tailp;
677 (void) close(fp->fd);
679 if ((dir = opendir(fp->path)) == NULL) {
684 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
685 fp->num_children = 0;
686 while ((dirent = readdir(dir)) != NULL) {
687 if (dirent->d_namlen == 0)
689 name = copy(dirent->d_name, dirent->d_namlen);
690 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
695 if (fp->num_children == 0) {
696 (void) fprintf(stderr,
697 "fortune: %s: No fortune files in directory.\n", fp->path);
705 * Return TRUE if the file is a directory, FALSE otherwise.
711 auto struct stat sbuf;
713 if (stat(file, &sbuf) < 0)
715 return (sbuf.st_mode & S_IFDIR);
720 * Return TRUE if the file is a fortune database file. We try and
721 * exclude files without reading them if possible to avoid
722 * overhead. Files which start with ".", or which have "illegal"
723 * suffixes, as contained in suflist[], are ruled out.
727 is_fortfile(file, datp, posp, check_for_offend)
728 char *file, **datp, **posp;
729 int check_for_offend;
734 static char *suflist[] = { /* list of "illegal" suffixes" */
735 "dat", "pos", "c", "h", "p", "i", "f",
736 "pas", "ftn", "ins.c", "ins,pas",
741 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
744 * Preclude any -o files for offendable people, and any non -o
745 * files for completely offensive people.
747 if (check_for_offend && !All_forts) {
749 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
750 DPRINTF(2, (stderr, "FALSE (offending file)\n"));
755 if ((sp = rindex(file, '/')) == NULL)
760 DPRINTF(2, (stderr, "FALSE (file starts with '.')\n"));
763 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
764 DPRINTF(2, (stderr, "FALSE (check fortunes only)\n"));
767 if ((sp = rindex(sp, '.')) != NULL) {
769 for (i = 0; suflist[i] != NULL; i++)
770 if (strcmp(sp, suflist[i]) == 0) {
771 DPRINTF(2, (stderr, "FALSE (file has suffix \".%s\")\n", sp));
776 datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
777 strcat(datfile, ".dat");
778 if (access(datfile, R_OK) < 0) {
779 DPRINTF(2, (stderr, "FALSE (no readable \".dat\" file)\n"));
782 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
792 #ifdef OK_TO_WRITE_DISK
793 *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
794 (void) strcat(*posp, ".pos");
797 #endif /* OK_TO_WRITE_DISK */
799 DPRINTF(2, (stderr, "TRUE\n"));
805 * Return a malloc()'ed copy of the string
814 new = do_malloc(len + 1);
824 * Do a malloc, checking for NULL return.
832 if ((new = malloc(size)) == NULL) {
833 (void) fprintf(stderr, "fortune: out of memory.\n");
841 * Free malloc'ed space, if any.
853 * Initialize the fortune probabilities.
858 FILEDESC *fp, *last = NULL;
859 int percent, num_noprob, frac;
862 * Distribute the residual probability (if any) across all
863 * files with unspecified probability (i.e., probability of 0)
869 for (fp = File_tail; fp != NULL; fp = fp->prev)
870 if (fp->percent == NO_PROB) {
876 percent += fp->percent;
877 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
878 percent, num_noprob));
880 (void) fprintf(stderr,
881 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
884 else if (percent < 100 && num_noprob == 0) {
885 (void) fprintf(stderr,
886 "fortune: no place to put residual probability (%d%% < 100%%)\n",
890 else if (percent == 100 && num_noprob != 0) {
891 (void) fprintf(stderr,
892 "fortune: no probability left to put in residual files (100%%)\n");
895 percent = 100 - percent;
897 if (num_noprob != 0) {
898 if (num_noprob > 1) {
899 frac = percent / num_noprob;
900 DPRINTF(1, (stderr, ", frac = %d%%", frac));
901 for (fp = File_list; fp != last; fp = fp->next)
902 if (fp->percent == NO_PROB) {
907 last->percent = percent;
908 DPRINTF(1, (stderr, ", residual = %d%%", percent));
912 ", %d%% distributed over remaining fortunes\n",
915 DPRINTF(1, (stderr, "\n"));
925 * Get the fortune data file's seek pointer for the next fortune.
933 if (File_list->next == NULL || File_list->percent == NO_PROB)
936 choice = random() % 100;
937 DPRINTF(1, (stderr, "choice = %d\n", choice));
938 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
939 if (choice < fp->percent)
942 choice -= fp->percent;
944 " skip \"%s\", %d%% (choice = %d)\n",
945 fp->name, fp->percent, choice));
948 "using \"%s\", %d%% (choice = %d)\n",
949 fp->name, fp->percent, choice));
951 if (fp->percent != NO_PROB)
954 if (fp->next != NULL) {
956 choice = random() % Noprob_tbl.str_numstr;
957 DPRINTF(1, (stderr, "choice = %d (of %u) \n", choice,
958 Noprob_tbl.str_numstr));
959 while (choice >= fp->tbl.str_numstr) {
960 choice -= fp->tbl.str_numstr;
963 " skip \"%s\", %u (choice = %d)\n",
964 fp->name, fp->tbl.str_numstr,
967 DPRINTF(1, (stderr, "using \"%s\", %u\n", fp->name,
968 fp->tbl.str_numstr));
972 if (fp->child != NULL) {
973 DPRINTF(1, (stderr, "picking child\n"));
979 (void) lseek(fp->datfd,
980 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0);
981 read(fp->datfd, Seekpts, sizeof Seekpts);
982 Seekpts[0] = be64toh(Seekpts[0]);
983 Seekpts[1] = be64toh(Seekpts[1]);
988 * Pick a child from a chosen 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 %u)\n",
1010 choice, parent->tbl.str_numstr));
1011 for (fp = parent->child; choice >= fp->tbl.str_numstr;
1013 choice -= fp->tbl.str_numstr;
1014 DPRINTF(1, (stderr, "\tskip %s, %u (choice = %d)\n",
1015 fp->name, fp->tbl.str_numstr, choice));
1017 DPRINTF(1, (stderr, " using %s, %u\n", fp->name,
1018 fp->tbl.str_numstr));
1025 * Sum up all the noprob probabilities, starting with fp.
1031 static bool did_noprobs = FALSE;
1035 zero_tbl(&Noprob_tbl);
1036 while (fp != NULL) {
1038 sum_tbl(&Noprob_tbl, &fp->tbl);
1048 return (i >= j ? i : j);
1053 * Assocatiate a FILE * with the given FILEDESC.
1059 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1067 * Open up the dat file if we need to.
1073 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, 0)) < 0) {
1074 perror(fp->datfile);
1081 * Get the position from the pos file, if there is one. If not,
1082 * return a random number.
1088 #ifdef OK_TO_WRITE_DISK
1090 #endif /* OK_TO_WRITE_DISK */
1092 assert(fp->read_tbl);
1093 if (fp->pos == POS_UNKNOWN) {
1094 #ifdef OK_TO_WRITE_DISK
1095 if ((fd = open(fp->posfile, 0)) < 0 ||
1096 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1097 fp->pos = random() % fp->tbl.str_numstr;
1098 else if (fp->pos >= fp->tbl.str_numstr)
1099 fp->pos %= fp->tbl.str_numstr;
1103 fp->pos = random() % fp->tbl.str_numstr;
1104 #endif /* OK_TO_WRITE_DISK */
1106 if (++(fp->pos) >= fp->tbl.str_numstr)
1107 fp->pos -= fp->tbl.str_numstr;
1108 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, (long)fp->pos));
1113 * Get the tbl data file the datfile.
1124 if (fp->child == NULL) {
1125 if ((fd = open(fp->datfile, 0)) < 0) {
1126 perror(fp->datfile);
1129 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1130 (void)fprintf(stderr,
1131 "fortune: %s corrupted\n", fp->path);
1134 /* fp->tbl.str_version = be32toh(fp->tbl.str_version); */
1135 fp->tbl.str_numstr = be32toh(fp->tbl.str_numstr);
1136 fp->tbl.str_longlen = be32toh(fp->tbl.str_longlen);
1137 fp->tbl.str_shortlen = be32toh(fp->tbl.str_shortlen);
1138 fp->tbl.str_flags = be32toh(fp->tbl.str_flags);
1143 for (child = fp->child; child != NULL; child = child->next) {
1145 sum_tbl(&fp->tbl, &child->tbl);
1148 fp->read_tbl = TRUE;
1153 * Zero out the fields we care about in a tbl structure.
1160 tp->str_longlen = 0;
1161 tp->str_shortlen = ~0;
1166 * Merge the tbl data of t2 into t1.
1172 t1->str_numstr += t2->str_numstr;
1173 if (t1->str_longlen < t2->str_longlen)
1174 t1->str_longlen = t2->str_longlen;
1175 if (t1->str_shortlen > t2->str_shortlen)
1176 t1->str_shortlen = t2->str_shortlen;
1179 #define STR(str) ((str) == NULL ? "NULL" : (str))
1183 * Print out the file list
1188 print_list(File_list, 0);
1193 * Print out the actual list, recursively.
1196 print_list(list, lev)
1200 while (list != NULL) {
1201 fprintf(stderr, "%*s", lev * 4, "");
1202 if (list->percent == NO_PROB)
1203 fprintf(stderr, "___%%");
1205 fprintf(stderr, "%3d%%", list->percent);
1206 fprintf(stderr, " %s", STR(list->name));
1207 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1208 STR(list->datfile), STR(list->posfile)));
1209 fprintf(stderr, "\n");
1210 if (list->child != NULL)
1211 print_list(list->child, lev + 1);
1218 * Convert the pattern to an ignore-case equivalent.
1228 cnt = 1; /* allow for '\0' */
1229 for (sp = orig; *sp != '\0'; sp++)
1230 if (isalpha((unsigned char)*sp))
1234 if ((new = malloc(cnt)) == NULL) {
1235 fprintf(stderr, "pattern too long for ignoring case\n");
1239 for (sp = new; *orig != '\0'; orig++) {
1240 if (islower((unsigned char)*orig)) {
1243 *sp++ = toupper((unsigned char)*orig);
1246 else if (isupper((unsigned char)*orig)) {
1249 *sp++ = tolower((unsigned char)*orig);
1261 * Find all the fortunes which match the pattern we've been given.
1266 Fort_len = maxlen_in_list(File_list);
1267 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1268 /* extra length, "%\n" is appended */
1269 Fortbuf = do_malloc((unsigned int) Fort_len + 10);
1272 matches_in_list(File_list);
1279 * Return the maximum fortune len in the file list.
1282 maxlen_in_list(list)
1289 for (fp = list; fp != NULL; fp = fp->next) {
1290 if (fp->child != NULL) {
1291 if ((len = maxlen_in_list(fp->child)) > maxlen)
1296 if (fp->tbl.str_longlen > maxlen)
1297 maxlen = fp->tbl.str_longlen;
1305 * Print out the matches from the files in the list.
1308 matches_in_list(list)
1316 for (fp = list; fp != NULL; fp = fp->next) {
1317 if (fp->child != NULL) {
1318 matches_in_list(fp->child);
1321 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1325 while (fgets(sp, Fort_len, fp->inf) != NULL)
1326 if (fp->tbl.str_flags & STR_COMMENTS
1327 && sp[0] == fp->tbl.str_delim
1328 && sp[1] == fp->tbl.str_delim)
1330 else if (!STR_ENDSTRING(sp, fp->tbl))
1334 if (fp->tbl.str_flags & STR_ROTATED)
1335 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1338 *p = 'A' + (ch - 'A' + 13) % 26;
1339 else if (islower(ch))
1340 *p = 'a' + (ch - 'a' + 13) % 26;
1343 if (regexec(&Re_pat, Fortbuf, 0, NULL, 0) != REG_NOMATCH) {
1344 printf("%c%c", fp->tbl.str_delim,
1347 printf(" (%s)", fp->name);
1352 (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1362 (void) fprintf(stderr, "fortune [-a");
1364 (void) fprintf(stderr, "D");
1366 (void) fprintf(stderr, "efilosw]");
1367 (void) fprintf(stderr, " [-m pattern]");
1368 (void) fprintf(stderr, " [[N%%] file/directory/all]\n");