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 bool Found_one; /* did we find a match? */
90 bool Find_files = false; /* just find a list of proper fortune files */
91 bool Fortunes_only = false; /* check only "fortunes" files */
92 bool Wait = false; /* wait desired after fortune */
93 bool Short_only = false; /* short fortune desired */
94 bool Long_only = false; /* long fortune desired */
95 bool Offend = false; /* offensive fortunes only */
96 bool All_forts = false; /* any fortune allowed */
97 bool Equal_probs= false; /* scatter un-allocted prob equally */
98 bool Match = false; /* dump fortunes matching a pattern */
100 bool Debug = false; /* print debug messages */
103 char *Fortbuf = NULL; /* fortune buffer for -m */
107 long Seekpts[2]; /* seek pointers to fortunes */
109 FILEDESC *File_list = NULL, /* Head of file list */
110 *File_tail = NULL; /* Tail of file list */
111 FILEDESC *Fortfile; /* Fortune file to use */
113 STRFILE Noprob_tbl; /* sum of data for all no prob files */
115 bool add_dir(FILEDESC *);
117 const char *, const char *, FILEDESC **, FILEDESC **, FILEDESC *);
118 void all_forts(FILEDESC *, char *);
119 char *copy(const char *, u_int);
120 void display(FILEDESC *);
121 void do_free(void *);
122 void *do_malloc(u_int);
123 bool form_file_list(char **, int);
126 void get_pos(FILEDESC *);
127 void get_tbl(FILEDESC *);
128 void getargs(int, char *[]);
129 void init_prob(void);
130 bool is_dir(const char *);
131 bool is_fortfile(const char *, char **, char **, int);
132 bool is_off_name(const char *);
134 FILEDESC *new_fp(void);
135 char *off_name(const char *);
136 void open_dat(FILEDESC *);
137 void open_fp(FILEDESC *);
138 FILEDESC *pick_child(FILEDESC *);
139 void print_file_list(void);
140 void print_list(FILEDESC *, int);
141 void sum_noprobs(FILEDESC *);
142 void sum_tbl(STRFILE *, STRFILE *);
144 void zero_tbl(STRFILE *);
146 char *conv_pat(char *);
147 int find_matches(void);
148 void matches_in_list(FILEDESC *);
149 int maxlen_in_list(FILEDESC *);
151 static regex_t Re_pat;
154 main(int ac, char *av[])
156 #ifdef OK_TO_WRITE_DISK
158 #endif /* OK_TO_WRITE_DISK */
160 setlocale(LC_ALL, "");
165 exit(find_matches() != 0);
171 } while ((Short_only && fortlen() > SLEN) ||
172 (Long_only && fortlen() <= SLEN));
176 #ifdef OK_TO_WRITE_DISK
177 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
178 perror(Fortfile->posfile);
183 * if we can, we exclusive lock, but since it isn't very
184 * 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);
195 #endif /* OK_TO_WRITE_DISK */
199 sleep((unsigned int)max(Fort_len / CPERS, MINW));
206 display(FILEDESC *fp)
213 fseek(fp->inf, Seekpts[0], SEEK_SET);
214 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
215 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
216 if (fp->tbl.str_flags & STR_ROTATED)
217 for (p = line; (ch = *p) != '\0'; ++p) {
220 *p = 'A' + (ch - 'A' + 13) % 26;
221 else if (islower(ch))
222 *p = 'a' + (ch - 'a' + 13) % 26;
225 if (fp->tbl.str_flags & STR_COMMENTS
226 && line[0] == fp->tbl.str_delim
227 && line[1] == fp->tbl.str_delim)
236 * Return the length of the fortune.
244 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
245 nchar = (Seekpts[1] - Seekpts[0] <= SLEN);
248 fseek(Fortfile->inf, Seekpts[0], SEEK_SET);
250 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
251 !STR_ENDSTRING(line, Fortfile->tbl))
252 nchar += strlen(line);
260 * This routine evaluates the arguments on the command line
263 getargs(int argc, char **argv)
273 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
275 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
278 case 'a': /* any fortune */
287 Equal_probs++; /* scatter un-allocted prob equally */
289 case 'f': /* find fortune files */
292 case 'l': /* long ones only */
296 case 'o': /* offensive ones only */
299 case 's': /* short ones only */
303 case 'w': /* give time to read */
306 case 'm': /* dump out the fortunes */
310 case 'i': /* case-insensitive match */
320 if (!form_file_list(argv, argc))
321 exit(1); /* errors printed through form_file_list() */
336 error = regcomp(&Re_pat, pat, REG_BASIC);
338 fprintf(stderr, "regcomp(%s) fails\n", pat);
346 * Form the file list from the file specifications.
349 form_file_list(char **files, int file_cnt)
357 Fortunes_only = true;
358 i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
360 Fortunes_only = false;
363 return (add_file(NO_PROB, "fortunes", FORTDIR,
364 &File_list, &File_tail, NULL));
366 for (i = 0; i < file_cnt; i++) {
368 if (!isdigit((unsigned char)files[i][0]))
372 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
373 percent = percent * 10 + *sp - '0';
375 fprintf(stderr, "percentages must be <= 100\n");
379 fprintf(stderr, "percentages must be integers\n");
383 * If the number isn't followed by a '%', then
384 * it was not a percentage, just the first part
385 * of a file name which starts with digits.
391 else if (*++sp == '\0') {
392 if (++i >= file_cnt) {
393 fprintf(stderr, "percentages must precede files\n");
399 if (strcmp(sp, "all") == 0)
401 if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
410 * Add a file to the file list.
413 add_file(int percent, const char *file, const char *dir,
414 FILEDESC **head, FILEDESC **tail, FILEDESC *parent)
419 char *tpath, *offensive;
429 tpath = do_malloc((unsigned int)(strlen(dir) + strlen(file) + 2));
430 strcat(strcat(strcpy(tpath, dir), "/"), file);
434 if ((isdir = is_dir(path)) && parent != NULL) {
437 return (false); /* don't recurse */
440 if (!isdir && parent == NULL && (All_forts || Offend) &&
441 !is_off_name(path)) {
442 offensive = off_name(path);
449 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
450 file = off_name(file);
454 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
456 if ((fd = open(path, O_RDONLY)) < 0) {
458 * This is a sneak. If the user said -a, and if the
459 * file we're given isn't a file, we check to see if
460 * there is a -o version. If there is, we treat it as
461 * if *that* were the file given. We only do this for
462 * individual files -- if we're scanning a directory,
463 * we'll pick up the -o file anyway.
465 if (All_forts && offensive != NULL) {
471 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
472 file = off_name(file);
475 if (dir == NULL && file[0] != '/')
476 return (add_file(percent, file, FORTDIR, head, tail,
485 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
489 fp->percent = percent;
494 if ((isdir && !add_dir(fp)) ||
496 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
500 "fortune:%s not a fortune file or directory\n",
504 do_free(fp->datfile);
505 do_free(fp->posfile);
511 * If the user said -a, we need to make this node a pointer to
512 * both files, if there are two. We don't need to do this if
513 * we are scanning a directory, since the scan will pick up the
516 if (All_forts && parent == NULL && !is_off_name(path))
517 all_forts(fp, offensive);
520 else if (fp->percent == NO_PROB) {
530 #ifdef OK_TO_WRITE_DISK
531 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
532 #endif /* OK_TO_WRITE_DISK */
539 * Return a pointer to an initialized new FILEDESC.
546 fp = (FILEDESC *)do_malloc(sizeof(*fp));
548 fp->pos = POS_UNKNOWN;
551 fp->percent = NO_PROB;
552 fp->read_tbl = false;
565 * Return a pointer to the offensive version of a file of this name.
568 off_name(const char *file)
572 new = copy(file, (unsigned int)(strlen(file) + 2));
574 return (strcat(new, "-o"));
579 * Is the file an offensive-style name?
582 is_off_name(const char *file)
588 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
593 * Modify a FILEDESC element to be the parent of two children if
594 * there are two children to be a parent of.
597 all_forts(FILEDESC *fp, char *offensive)
600 FILEDESC *scene, *obscene;
602 char *datfile, *posfile;
604 if (fp->child != NULL) /* this is a directory, not a file */
606 if (!is_fortfile(offensive, &datfile, &posfile, false))
608 if ((fd = open(offensive, O_RDONLY)) < 0)
610 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
615 fp->num_children = 2;
617 scene->next = obscene;
618 obscene->next = NULL;
619 scene->child = obscene->child = NULL;
620 scene->parent = obscene->parent = fp;
623 scene->percent = obscene->percent = NO_PROB;
627 obscene->path = offensive;
628 if ((sp = rindex(offensive, '/')) == NULL)
629 obscene->name = offensive;
631 obscene->name = ++sp;
632 obscene->datfile = datfile;
633 obscene->posfile = posfile;
634 obscene->read_tbl = false;
635 #ifdef OK_TO_WRITE_DISK
636 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
637 #endif /* OK_TO_WRITE_DISK */
642 * Add the contents of an entire directory.
645 add_dir(FILEDESC *fp)
648 struct dirent *dirent;
654 if ((dir = opendir(fp->path)) == NULL) {
659 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
660 fp->num_children = 0;
661 while ((dirent = readdir(dir)) != NULL) {
662 if ((name = strdup(dirent->d_name)) == NULL)
663 err(1, "strdup failed");
664 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
669 if (fp->num_children == 0) {
671 "fortune: %s: No fortune files in directory.\n", fp->path);
680 * Return true if the file is a directory, false otherwise.
683 is_dir(const char *file)
687 if (stat(file, &sbuf) < 0)
690 return (sbuf.st_mode & S_IFDIR);
695 * Return true if the file is a fortune database file. We try and
696 * exclude files without reading them if possible to avoid
697 * overhead. Files which start with ".", or which have "illegal"
698 * suffixes, as contained in suflist[], are ruled out.
702 is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
707 static const char *suflist[] = {
708 /* list of "illegal" suffixes" */
709 "dat", "pos", "c", "h", "p", "i", "f",
710 "pas", "ftn", "ins.c", "ins,pas",
715 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
718 * Preclude any -o files for offendable people, and any non -o
719 * files for completely offensive people.
721 if (check_for_offend && !All_forts) {
723 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
724 DPRINTF(2, (stderr, "false (offending file)\n"));
729 if ((sp = rindex(file, '/')) == NULL)
734 DPRINTF(2, (stderr, "false (file starts with '.')\n"));
737 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
738 DPRINTF(2, (stderr, "false (check fortunes only)\n"));
741 if ((sp = rindex(sp, '.')) != NULL) {
743 for (i = 0; suflist[i] != NULL; i++)
744 if (strcmp(sp, suflist[i]) == 0) {
745 DPRINTF(2, (stderr, "false (file has suffix \".%s\")\n", sp));
750 datfile = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */
751 strcat(datfile, ".dat");
752 if (access(datfile, R_OK) < 0) {
753 DPRINTF(2, (stderr, "false (no readable \".dat\" file)\n"));
756 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
765 #ifdef OK_TO_WRITE_DISK
767 *posp = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */
768 strcat(*posp, ".pos");
774 #endif /* OK_TO_WRITE_DISK */
775 DPRINTF(2, (stderr, "true\n"));
782 * Return a malloc()'ed copy of the string
785 copy(const char *str, unsigned int len)
789 new = do_malloc(len + 1);
800 * Do a malloc, checking for NULL return.
803 do_malloc(unsigned int size)
807 if ((new = malloc(size)) == NULL) {
808 fprintf(stderr, "fortune: out of memory.\n");
817 * Free malloc'ed space, if any.
828 * Initialize the fortune probabilities.
834 int percent, num_noprob, frac;
838 * Distribute the residual probability (if any) across all
839 * files with unspecified probability (i.e., probability of 0)
845 for (fp = File_tail; fp != NULL; fp = fp->prev)
846 if (fp->percent == NO_PROB) {
851 percent += fp->percent;
852 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
853 percent, num_noprob));
856 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
858 } else if (percent < 100 && num_noprob == 0) {
860 "fortune: no place to put residual probability (%d%% < 100%%)\n",
863 } else if (percent == 100 && num_noprob != 0) {
865 "fortune: no probability left to put in residual files (100%%)\n");
868 percent = 100 - percent;
870 if (num_noprob != 0) {
871 if (num_noprob > 1) {
872 frac = percent / num_noprob;
873 DPRINTF(1, (stderr, ", frac = %d%%", frac));
874 for (fp = File_list; fp != last; fp = fp->next)
875 if (fp->percent == NO_PROB) {
880 last->percent = percent;
881 DPRINTF(1, (stderr, ", residual = %d%%", percent));
885 ", %d%% distributed over remaining fortunes\n",
888 DPRINTF(1, (stderr, "\n"));
898 * Get the fortune data file's seek pointer for the next fortune.
906 if (File_list->next == NULL || File_list->percent == NO_PROB)
909 choice = random() % 100;
910 DPRINTF(1, (stderr, "choice = %d\n", choice));
911 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
912 if (choice < fp->percent)
915 choice -= fp->percent;
917 " skip \"%s\", %d%% (choice = %d)\n",
918 fp->name, fp->percent, choice));
920 DPRINTF(1, (stderr, "using \"%s\", %d%% (choice = %d)\n",
921 fp->name, fp->percent, choice));
923 if (fp->percent != NO_PROB)
926 if (fp->next != NULL) {
928 choice = random() % Noprob_tbl.str_numstr;
929 DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice,
930 Noprob_tbl.str_numstr));
931 while ((unsigned int)choice >= fp->tbl.str_numstr) {
932 choice -= fp->tbl.str_numstr;
935 " skip \"%s\", %ld (choice = %d)\n",
936 fp->name, fp->tbl.str_numstr,
939 DPRINTF(1, (stderr, "using \"%s\", %ld\n", fp->name,
940 fp->tbl.str_numstr));
944 if (fp->child != NULL) {
945 DPRINTF(1, (stderr, "picking child\n"));
952 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), SEEK_SET);
953 read(fp->datfd, Seekpts, sizeof Seekpts);
954 Seekpts[0] = ntohl(Seekpts[0]);
955 Seekpts[1] = ntohl(Seekpts[1]);
960 * Pick a child from a chosen parent.
963 pick_child(FILEDESC *parent)
969 choice = random() % parent->num_children;
970 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
971 choice, parent->num_children));
972 for (fp = parent->child; choice--; fp = fp->next)
974 DPRINTF(1, (stderr, " using %s\n", fp->name));
979 choice = random() % parent->tbl.str_numstr;
980 DPRINTF(1, (stderr, " choice = %d (of %ld)\n",
981 choice, parent->tbl.str_numstr));
982 for (fp = parent->child; (unsigned int)choice >= fp->tbl.str_numstr;
984 choice -= fp->tbl.str_numstr;
985 DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n",
986 fp->name, fp->tbl.str_numstr, choice));
988 DPRINTF(1, (stderr, " using %s, %ld\n", fp->name,
989 fp->tbl.str_numstr));
996 * Sum up all the noprob probabilities, starting with fp.
999 sum_noprobs(FILEDESC *fp)
1001 static bool did_noprobs = false;
1005 zero_tbl(&Noprob_tbl);
1006 while (fp != NULL) {
1008 sum_tbl(&Noprob_tbl, &fp->tbl);
1017 return (i >= j ? i : j);
1022 * Assocatiate a FILE * with the given FILEDESC.
1025 open_fp(FILEDESC *fp)
1027 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1035 * Open up the dat file if we need to.
1038 open_dat(FILEDESC *fp)
1040 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY)) < 0) {
1041 perror(fp->datfile);
1048 * Get the position from the pos file, if there is one. If not,
1049 * return a random number.
1052 get_pos(FILEDESC *fp)
1054 #ifdef OK_TO_WRITE_DISK
1056 #endif /* OK_TO_WRITE_DISK */
1058 assert(fp->read_tbl);
1059 if (fp->pos == POS_UNKNOWN) {
1060 #ifdef OK_TO_WRITE_DISK
1061 if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
1062 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1063 fp->pos = random() % fp->tbl.str_numstr;
1064 else if (fp->pos >= fp->tbl.str_numstr)
1065 fp->pos %= fp->tbl.str_numstr;
1069 fp->pos = random() % fp->tbl.str_numstr;
1070 #endif /* OK_TO_WRITE_DISK */
1072 if ((unsigned int)++(fp->pos) >= fp->tbl.str_numstr)
1073 fp->pos -= fp->tbl.str_numstr;
1074 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, fp->pos));
1079 * Get the tbl data file the datfile.
1082 get_tbl(FILEDESC *fp)
1089 if (fp->child == NULL) {
1090 if ((fd = open(fp->datfile, O_RDONLY)) < 0) {
1091 perror(fp->datfile);
1094 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1096 "fortune: %s corrupted\n", fp->path);
1099 /* fp->tbl.str_version = ntohl(fp->tbl.str_version); */
1100 fp->tbl.str_numstr = ntohl(fp->tbl.str_numstr);
1101 fp->tbl.str_longlen = ntohl(fp->tbl.str_longlen);
1102 fp->tbl.str_shortlen = ntohl(fp->tbl.str_shortlen);
1103 fp->tbl.str_flags = ntohl(fp->tbl.str_flags);
1108 for (child = fp->child; child != NULL; child = child->next) {
1110 sum_tbl(&fp->tbl, &child->tbl);
1113 fp->read_tbl = true;
1118 * Zero out the fields we care about in a tbl structure.
1121 zero_tbl(STRFILE *tp)
1124 tp->str_longlen = 0;
1125 tp->str_shortlen = ~((unsigned long)0);
1130 * Merge the tbl data of t2 into t1.
1133 sum_tbl(STRFILE *t1, STRFILE *t2)
1135 t1->str_numstr += t2->str_numstr;
1136 if (t1->str_longlen < t2->str_longlen)
1137 t1->str_longlen = t2->str_longlen;
1138 if (t1->str_shortlen > t2->str_shortlen)
1139 t1->str_shortlen = t2->str_shortlen;
1142 #define STR(str) ((str) == NULL ? "NULL" : (str))
1146 * Print out the file list
1149 print_file_list(void)
1151 print_list(File_list, 0);
1156 * Print out the actual list, recursively.
1159 print_list(FILEDESC *list, int lev)
1161 while (list != NULL) {
1162 fprintf(stderr, "%*s", lev * 4, "");
1163 if (list->percent == NO_PROB)
1164 fprintf(stderr, "___%%");
1166 fprintf(stderr, "%3d%%", list->percent);
1167 fprintf(stderr, " %s", STR(list->name));
1168 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1169 STR(list->datfile), STR(list->posfile)));
1170 fprintf(stderr, "\n");
1171 if (list->child != NULL)
1172 print_list(list->child, lev + 1);
1179 * Convert the pattern to an ignore-case equivalent.
1182 conv_pat(char *orig)
1188 cnt = 1; /* allow for '\0' */
1189 for (sp = orig; *sp != '\0'; sp++)
1190 if (isalpha((unsigned char)*sp))
1194 if ((new = malloc(cnt)) == NULL) {
1195 fprintf(stderr, "pattern too long for ignoring case\n");
1199 for (sp = new; *orig != '\0'; orig++) {
1200 if (islower((unsigned char)*orig)) {
1203 *sp++ = toupper((unsigned char)*orig);
1206 else if (isupper((unsigned char)*orig)) {
1209 *sp++ = tolower((unsigned char)*orig);
1222 * Find all the fortunes which match the pattern we've been given.
1227 Fort_len = maxlen_in_list(File_list);
1228 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1229 /* extra length, "%\n" is appended */
1230 Fortbuf = do_malloc((unsigned int)Fort_len + 10);
1233 matches_in_list(File_list);
1240 * Return the maximum fortune len in the file list.
1243 maxlen_in_list(FILEDESC *list)
1249 for (fp = list; fp != NULL; fp = fp->next) {
1250 if (fp->child != NULL) {
1251 if ((len = maxlen_in_list(fp->child)) > maxlen)
1256 if (fp->tbl.str_longlen > (unsigned int)maxlen)
1257 maxlen = fp->tbl.str_longlen;
1266 * Print out the matches from the files in the list.
1269 matches_in_list(FILEDESC *list)
1276 for (fp = list; fp != NULL; fp = fp->next) {
1277 if (fp->child != NULL) {
1278 matches_in_list(fp->child);
1281 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1285 while (fgets(sp, Fort_len, fp->inf) != NULL)
1286 if (fp->tbl.str_flags & STR_COMMENTS
1287 && sp[0] == fp->tbl.str_delim
1288 && sp[1] == fp->tbl.str_delim)
1290 else if (!STR_ENDSTRING(sp, fp->tbl))
1294 if (fp->tbl.str_flags & STR_ROTATED)
1295 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1298 *p = 'A' + (ch - 'A' + 13) % 26;
1299 else if (islower(ch))
1300 *p = 'a' + (ch - 'a' + 13) % 26;
1303 if (regexec(&Re_pat, Fortbuf, 0, NULL, 0) != REG_NOMATCH) {
1304 printf("%c%c", fp->tbl.str_delim,
1307 printf(" (%s)", fp->name);
1312 fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1322 fprintf(stderr, "fortune [-a");
1324 fprintf(stderr, "D");
1326 fprintf(stderr, "f");
1327 fprintf(stderr, "i");
1328 fprintf(stderr, "losw]");
1329 fprintf(stderr, " [-m pattern]");
1330 fprintf(stderr, "[[#%%] file/directory/all]\n");