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 */
130 char **Fortune_path_arr;
132 int add_dir(FILEDESC *);
133 int add_file __P((int,
134 char *, char *, FILEDESC **, FILEDESC **, FILEDESC *));
135 void all_forts(FILEDESC *, char *);
136 char *copy(char *, u_int);
137 void display(FILEDESC *);
138 void do_free(void *);
139 void *do_malloc(u_int);
140 int form_file_list(char **, int);
143 void get_pos(FILEDESC *);
144 void get_tbl(FILEDESC *);
145 void getargs(int, char *[]);
147 void init_prob(void);
149 int is_fortfile(char *, char **, char **, int);
150 int is_off_name(char *);
154 char *off_name(char *);
155 void open_dat(FILEDESC *);
156 void open_fp(FILEDESC *);
158 pick_child(FILEDESC *);
159 void print_file_list(void);
160 void print_list(FILEDESC *, int);
161 void sum_noprobs(FILEDESC *);
162 void sum_tbl(STRFILE *, STRFILE *);
164 void zero_tbl(STRFILE *);
166 char *conv_pat(char *);
167 int find_matches(void);
168 void matches_in_list(FILEDESC *);
169 int maxlen_in_list(FILEDESC *);
171 static regex_t Re_pat;
178 #ifdef OK_TO_WRITE_DISK
180 #endif /* OK_TO_WRITE_DISK */
182 (void) setlocale(LC_ALL, "");
188 exit(find_matches() != 0);
194 } while ((Short_only && fortlen() > SLEN) ||
195 (Long_only && fortlen() <= SLEN));
199 #ifdef OK_TO_WRITE_DISK
200 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
201 perror(Fortfile->posfile);
206 * if we can, we exclusive lock, but since it isn't very
207 * important, we just punt if we don't have easy locking
210 (void) flock(fd, LOCK_EX);
212 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
213 if (!Fortfile->was_pos_file)
214 (void) chmod(Fortfile->path, 0666);
216 (void) flock(fd, LOCK_UN);
218 #endif /* OK_TO_WRITE_DISK */
222 sleep((unsigned int) max(Fort_len / CPERS, MINW));
237 (void) fseeko(fp->inf, Seekpts[0], 0);
238 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
239 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
240 if (fp->tbl.str_flags & STR_ROTATED)
241 for (p = line; (ch = *p) != '\0'; ++p) {
244 *p = 'A' + (ch - 'A' + 13) % 26;
245 else if (islower(ch))
246 *p = 'a' + (ch - 'a' + 13) % 26;
249 if (fp->tbl.str_flags & STR_COMMENTS
250 && line[0] == fp->tbl.str_delim
251 && line[1] == fp->tbl.str_delim)
255 (void) fflush(stdout);
260 * Return the length of the fortune.
268 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
269 nchar = (int)(Seekpts[1] - Seekpts[0]);
272 (void) fseeko(Fortfile->inf, Seekpts[0], 0);
274 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
275 !STR_ENDSTRING(line, Fortfile->tbl))
276 nchar += strlen(line);
283 * This routine evaluates the arguments on the command line
300 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
302 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
305 case 'a': /* any fortune */
314 Equal_probs++; /* scatter un-allocted prob equally */
316 case 'f': /* find fortune files */
319 case 'l': /* long ones only */
323 case 'o': /* offensive ones only */
326 case 's': /* short ones only */
330 case 'w': /* give time to read */
333 case 'm': /* dump out the fortunes */
337 case 'i': /* case-insensitive match */
347 if (!form_file_list(argv, argc))
348 exit(1); /* errors printed through form_file_list() */
363 error = regcomp(&Re_pat, pat, REG_BASIC);
365 fprintf(stderr, "regcomp(%s) fails\n", pat);
373 * Form the file list from the file specifications.
376 form_file_list(files, file_cnt)
386 Fortunes_only = TRUE;
387 pstr = Fortune_path_arr;
390 i += add_file(NO_PROB, *pstr++, NULL,
391 &File_list, &File_tail, NULL);
393 Fortunes_only = FALSE;
395 fprintf(stderr, "No fortunes found in %s.\n",
400 pstr = Fortune_path_arr;
403 i += add_file(NO_PROB, "fortunes", *pstr++,
404 &File_list, &File_tail, NULL);
407 fprintf(stderr, "No fortunes found in %s.\n",
413 for (i = 0; i < file_cnt; i++) {
415 if (!isdigit((unsigned char)files[i][0]))
419 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
420 percent = percent * 10 + *sp - '0';
422 fprintf(stderr, "percentages must be <= 100\n");
426 fprintf(stderr, "percentages must be integers\n");
430 * If the number isn't followed by a '%', then
431 * it was not a percentage, just the first part
432 * of a file name which starts with digits.
438 else if (*++sp == '\0') {
439 if (++i >= file_cnt) {
440 fprintf(stderr, "percentages must precede files\n");
446 if (strcmp(sp, "all") == 0) {
447 pstr = Fortune_path_arr;
450 i += add_file(NO_PROB, *pstr++, NULL,
451 &File_list, &File_tail, NULL);
454 fprintf(stderr, "No fortunes found in %s.\n",
458 } else if (!add_file(percent, sp, NULL, &File_list,
468 * Add a file to the file list.
471 add_file(percent, file, dir, head, tail, parent)
475 FILEDESC **head, **tail;
480 char *path, *offensive;
489 path = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2));
490 (void) strcat(strcat(strcpy(path, dir), "/"), file);
493 if ((isdir = is_dir(path)) && parent != NULL) {
496 return FALSE; /* don't recurse */
499 if (!isdir && parent == NULL && (All_forts || Offend) &&
500 !is_off_name(path)) {
501 offensive = off_name(path);
508 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
509 file = off_name(file);
513 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
515 if ((fd = open(path, 0)) < 0) {
517 * This is a sneak. If the user said -a, and if the
518 * file we're given isn't a file, we check to see if
519 * there is a -o version. If there is, we treat it as
520 * if *that* were the file given. We only do this for
521 * individual files -- if we're scanning a directory,
522 * we'll pick up the -o file anyway.
524 if (All_forts && offensive != NULL) {
530 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
531 file = off_name(file);
534 if (dir == NULL && file[0] != '/') {
536 char **pstr = Fortune_path_arr;
539 i += add_file(percent, file, *pstr++,
543 fprintf(stderr, "No '%s' found in %s.\n",
557 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
561 fp->percent = percent;
566 if ((isdir && !add_dir(fp)) ||
568 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
572 "fortune:%s not a fortune file or directory\n",
576 do_free(fp->datfile);
577 do_free(fp->posfile);
583 * If the user said -a, we need to make this node a pointer to
584 * both files, if there are two. We don't need to do this if
585 * we are scanning a directory, since the scan will pick up the
588 if (All_forts && parent == NULL && !is_off_name(path))
589 all_forts(fp, offensive);
592 else if (fp->percent == NO_PROB) {
602 #ifdef OK_TO_WRITE_DISK
603 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
604 #endif /* OK_TO_WRITE_DISK */
611 * Return a pointer to an initialized new FILEDESC.
618 fp = (FILEDESC *) do_malloc(sizeof *fp);
620 fp->pos = POS_UNKNOWN;
623 fp->percent = NO_PROB;
624 fp->read_tbl = FALSE;
636 * Return a pointer to the offensive version of a file of this name.
644 new = copy(file, (unsigned int) (strlen(file) + 2));
645 return strcat(new, "-o");
650 * Is the file an offensive-style name?
659 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
664 * Modify a FILEDESC element to be the parent of two children if
665 * there are two children to be a parent of.
668 all_forts(fp, offensive)
673 FILEDESC *scene, *obscene;
675 auto char *datfile, *posfile;
677 if (fp->child != NULL) /* this is a directory, not a file */
679 if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
681 if ((fd = open(offensive, 0)) < 0)
683 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
688 fp->num_children = 2;
690 scene->next = obscene;
691 obscene->next = NULL;
692 scene->child = obscene->child = NULL;
693 scene->parent = obscene->parent = fp;
696 scene->percent = obscene->percent = NO_PROB;
700 obscene->path = offensive;
701 if ((sp = rindex(offensive, '/')) == NULL)
702 obscene->name = offensive;
704 obscene->name = ++sp;
705 obscene->datfile = datfile;
706 obscene->posfile = posfile;
707 obscene->read_tbl = FALSE;
708 #ifdef OK_TO_WRITE_DISK
709 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
710 #endif /* OK_TO_WRITE_DISK */
715 * Add the contents of an entire directory.
722 struct dirent *dirent;
723 auto FILEDESC *tailp;
726 (void) close(fp->fd);
728 if ((dir = opendir(fp->path)) == NULL) {
733 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
734 fp->num_children = 0;
735 while ((dirent = readdir(dir)) != NULL) {
736 if (dirent->d_namlen == 0)
738 name = copy(dirent->d_name, dirent->d_namlen);
739 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
744 if (fp->num_children == 0) {
745 (void) fprintf(stderr,
746 "fortune: %s: No fortune files in directory.\n", fp->path);
754 * Return TRUE if the file is a directory, FALSE otherwise.
760 auto struct stat sbuf;
762 if (stat(file, &sbuf) < 0)
764 return (sbuf.st_mode & S_IFDIR);
769 * Return TRUE if the file is a fortune database file. We try and
770 * exclude files without reading them if possible to avoid
771 * overhead. Files which start with ".", or which have "illegal"
772 * suffixes, as contained in suflist[], are ruled out.
776 is_fortfile(file, datp, posp, check_for_offend)
777 char *file, **datp, **posp;
778 int check_for_offend;
783 static char *suflist[] = { /* list of "illegal" suffixes" */
784 "dat", "pos", "c", "h", "p", "i", "f",
785 "pas", "ftn", "ins.c", "ins,pas",
790 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
793 * Preclude any -o files for offendable people, and any non -o
794 * files for completely offensive people.
796 if (check_for_offend && !All_forts) {
798 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
799 DPRINTF(2, (stderr, "FALSE (offending file)\n"));
804 if ((sp = rindex(file, '/')) == NULL)
809 DPRINTF(2, (stderr, "FALSE (file starts with '.')\n"));
812 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
813 DPRINTF(2, (stderr, "FALSE (check fortunes only)\n"));
816 if ((sp = rindex(sp, '.')) != NULL) {
818 for (i = 0; suflist[i] != NULL; i++)
819 if (strcmp(sp, suflist[i]) == 0) {
820 DPRINTF(2, (stderr, "FALSE (file has suffix \".%s\")\n", sp));
825 datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
826 strcat(datfile, ".dat");
827 if (access(datfile, R_OK) < 0) {
828 DPRINTF(2, (stderr, "FALSE (no readable \".dat\" file)\n"));
831 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
841 #ifdef OK_TO_WRITE_DISK
842 *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
843 (void) strcat(*posp, ".pos");
846 #endif /* OK_TO_WRITE_DISK */
848 DPRINTF(2, (stderr, "TRUE\n"));
854 * Return a malloc()'ed copy of the string
863 new = do_malloc(len + 1);
873 * Do a malloc, checking for NULL return.
881 if ((new = malloc(size)) == NULL) {
882 (void) fprintf(stderr, "fortune: out of memory.\n");
890 * Free malloc'ed space, if any.
902 * Initialize the fortune probabilities.
907 FILEDESC *fp, *last = NULL;
908 int percent, num_noprob, frac;
911 * Distribute the residual probability (if any) across all
912 * files with unspecified probability (i.e., probability of 0)
918 for (fp = File_tail; fp != NULL; fp = fp->prev)
919 if (fp->percent == NO_PROB) {
925 percent += fp->percent;
926 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
927 percent, num_noprob));
929 (void) fprintf(stderr,
930 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
933 else if (percent < 100 && num_noprob == 0) {
934 (void) fprintf(stderr,
935 "fortune: no place to put residual probability (%d%% < 100%%)\n",
939 else if (percent == 100 && num_noprob != 0) {
940 (void) fprintf(stderr,
941 "fortune: no probability left to put in residual files (100%%)\n");
944 percent = 100 - percent;
946 if (num_noprob != 0) {
947 if (num_noprob > 1) {
948 frac = percent / num_noprob;
949 DPRINTF(1, (stderr, ", frac = %d%%", frac));
950 for (fp = File_tail; fp != last; fp = fp->prev)
951 if (fp->percent == NO_PROB) {
956 last->percent = percent;
957 DPRINTF(1, (stderr, ", residual = %d%%", percent));
961 ", %d%% distributed over remaining fortunes\n",
964 DPRINTF(1, (stderr, "\n"));
974 * Get the fortune data file's seek pointer for the next fortune.
982 if (File_list->next == NULL || File_list->percent == NO_PROB)
985 choice = random() % 100;
986 DPRINTF(1, (stderr, "choice = %d\n", choice));
987 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
988 if (choice < fp->percent)
991 choice -= fp->percent;
993 " skip \"%s\", %d%% (choice = %d)\n",
994 fp->name, fp->percent, choice));
997 "using \"%s\", %d%% (choice = %d)\n",
998 fp->name, fp->percent, choice));
1000 if (fp->percent != NO_PROB)
1003 if (fp->next != NULL) {
1005 choice = random() % Noprob_tbl.str_numstr;
1006 DPRINTF(1, (stderr, "choice = %d (of %u) \n", choice,
1007 Noprob_tbl.str_numstr));
1008 while (choice >= fp->tbl.str_numstr) {
1009 choice -= fp->tbl.str_numstr;
1012 " skip \"%s\", %u (choice = %d)\n",
1013 fp->name, fp->tbl.str_numstr,
1016 DPRINTF(1, (stderr, "using \"%s\", %u\n", fp->name,
1017 fp->tbl.str_numstr));
1021 if (fp->child != NULL) {
1022 DPRINTF(1, (stderr, "picking child\n"));
1023 fp = pick_child(fp);
1028 (void) lseek(fp->datfd,
1029 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0);
1030 read(fp->datfd, Seekpts, sizeof Seekpts);
1031 Seekpts[0] = be64toh(Seekpts[0]);
1032 Seekpts[1] = be64toh(Seekpts[1]);
1037 * Pick a child from a chosen parent.
1047 choice = random() % parent->num_children;
1048 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1049 choice, parent->num_children));
1050 for (fp = parent->child; choice--; fp = fp->next)
1052 DPRINTF(1, (stderr, " using %s\n", fp->name));
1057 choice = random() % parent->tbl.str_numstr;
1058 DPRINTF(1, (stderr, " choice = %d (of %u)\n",
1059 choice, parent->tbl.str_numstr));
1060 for (fp = parent->child; choice >= fp->tbl.str_numstr;
1062 choice -= fp->tbl.str_numstr;
1063 DPRINTF(1, (stderr, "\tskip %s, %u (choice = %d)\n",
1064 fp->name, fp->tbl.str_numstr, choice));
1066 DPRINTF(1, (stderr, " using %s, %u\n", fp->name,
1067 fp->tbl.str_numstr));
1074 * Sum up all the noprob probabilities, starting with fp.
1080 static bool did_noprobs = FALSE;
1084 zero_tbl(&Noprob_tbl);
1085 while (fp != NULL) {
1087 sum_tbl(&Noprob_tbl, &fp->tbl);
1097 return (i >= j ? i : j);
1102 * Assocatiate a FILE * with the given FILEDESC.
1108 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1116 * Open up the dat file if we need to.
1122 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, 0)) < 0) {
1123 perror(fp->datfile);
1130 * Get the position from the pos file, if there is one. If not,
1131 * return a random number.
1137 #ifdef OK_TO_WRITE_DISK
1139 #endif /* OK_TO_WRITE_DISK */
1141 assert(fp->read_tbl);
1142 if (fp->pos == POS_UNKNOWN) {
1143 #ifdef OK_TO_WRITE_DISK
1144 if ((fd = open(fp->posfile, 0)) < 0 ||
1145 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1146 fp->pos = random() % fp->tbl.str_numstr;
1147 else if (fp->pos >= fp->tbl.str_numstr)
1148 fp->pos %= fp->tbl.str_numstr;
1152 fp->pos = random() % fp->tbl.str_numstr;
1153 #endif /* OK_TO_WRITE_DISK */
1155 if (++(fp->pos) >= fp->tbl.str_numstr)
1156 fp->pos -= fp->tbl.str_numstr;
1157 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, (long)fp->pos));
1162 * Get the tbl data file the datfile.
1173 if (fp->child == NULL) {
1174 if ((fd = open(fp->datfile, 0)) < 0) {
1175 perror(fp->datfile);
1178 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1179 (void)fprintf(stderr,
1180 "fortune: %s corrupted\n", fp->path);
1183 /* fp->tbl.str_version = be32toh(fp->tbl.str_version); */
1184 fp->tbl.str_numstr = be32toh(fp->tbl.str_numstr);
1185 fp->tbl.str_longlen = be32toh(fp->tbl.str_longlen);
1186 fp->tbl.str_shortlen = be32toh(fp->tbl.str_shortlen);
1187 fp->tbl.str_flags = be32toh(fp->tbl.str_flags);
1192 for (child = fp->child; child != NULL; child = child->next) {
1194 sum_tbl(&fp->tbl, &child->tbl);
1197 fp->read_tbl = TRUE;
1202 * Zero out the fields we care about in a tbl structure.
1209 tp->str_longlen = 0;
1210 tp->str_shortlen = ~0;
1215 * Merge the tbl data of t2 into t1.
1221 t1->str_numstr += t2->str_numstr;
1222 if (t1->str_longlen < t2->str_longlen)
1223 t1->str_longlen = t2->str_longlen;
1224 if (t1->str_shortlen > t2->str_shortlen)
1225 t1->str_shortlen = t2->str_shortlen;
1228 #define STR(str) ((str) == NULL ? "NULL" : (str))
1232 * Print out the file list
1237 print_list(File_list, 0);
1242 * Print out the actual list, recursively.
1245 print_list(list, lev)
1249 while (list != NULL) {
1250 fprintf(stderr, "%*s", lev * 4, "");
1251 if (list->percent == NO_PROB)
1252 fprintf(stderr, "___%%");
1254 fprintf(stderr, "%3d%%", list->percent);
1255 fprintf(stderr, " %s", STR(list->name));
1256 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1257 STR(list->datfile), STR(list->posfile)));
1258 fprintf(stderr, "\n");
1259 if (list->child != NULL)
1260 print_list(list->child, lev + 1);
1267 * Convert the pattern to an ignore-case equivalent.
1277 cnt = 1; /* allow for '\0' */
1278 for (sp = orig; *sp != '\0'; sp++)
1279 if (isalpha((unsigned char)*sp))
1283 if ((new = malloc(cnt)) == NULL) {
1284 fprintf(stderr, "pattern too long for ignoring case\n");
1288 for (sp = new; *orig != '\0'; orig++) {
1289 if (islower((unsigned char)*orig)) {
1292 *sp++ = toupper((unsigned char)*orig);
1295 else if (isupper((unsigned char)*orig)) {
1298 *sp++ = tolower((unsigned char)*orig);
1310 * Find all the fortunes which match the pattern we've been given.
1315 Fort_len = maxlen_in_list(File_list);
1316 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1317 /* extra length, "%\n" is appended */
1318 Fortbuf = do_malloc((unsigned int) Fort_len + 10);
1321 matches_in_list(File_list);
1328 * Return the maximum fortune len in the file list.
1331 maxlen_in_list(list)
1338 for (fp = list; fp != NULL; fp = fp->next) {
1339 if (fp->child != NULL) {
1340 if ((len = maxlen_in_list(fp->child)) > maxlen)
1345 if (fp->tbl.str_longlen > maxlen)
1346 maxlen = fp->tbl.str_longlen;
1354 * Print out the matches from the files in the list.
1357 matches_in_list(list)
1365 for (fp = list; fp != NULL; fp = fp->next) {
1366 if (fp->child != NULL) {
1367 matches_in_list(fp->child);
1370 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1374 while (fgets(sp, Fort_len, fp->inf) != NULL)
1375 if (fp->tbl.str_flags & STR_COMMENTS
1376 && sp[0] == fp->tbl.str_delim
1377 && sp[1] == fp->tbl.str_delim)
1379 else if (!STR_ENDSTRING(sp, fp->tbl))
1383 if (fp->tbl.str_flags & STR_ROTATED)
1384 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1387 *p = 'A' + (ch - 'A' + 13) % 26;
1388 else if (islower(ch))
1389 *p = 'a' + (ch - 'a' + 13) % 26;
1392 if (regexec(&Re_pat, Fortbuf, 0, NULL, 0) != REG_NOMATCH) {
1393 printf("%c%c", fp->tbl.str_delim,
1396 printf(" (%s)", fp->name);
1401 (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1411 (void) fprintf(stderr, "fortune [-a");
1413 (void) fprintf(stderr, "D");
1415 (void) fprintf(stderr, "efilosw]");
1416 (void) fprintf(stderr, " [-m pattern]");
1417 (void) fprintf(stderr, " [[N%%] file/directory/all]\n");
1423 * Set up file search patch from environment var FORTUNE_PATH;
1424 * if not set, use the compiled in FORTDIR.
1431 char *pch, **ppch, *str, *path;
1434 Fortune_path = getenv("FORTUNE_PATH");
1435 if (Fortune_path == NULL) {
1436 Fortune_path = FORTDIR;
1439 path = strdup(Fortune_path);
1441 for (nstr = 2, pch = path; *pch != '\0'; pch++) {
1446 ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *));
1449 str = strtok(path, ":");
1455 str = strtok(NULL, ":");
1459 if (foundenv == 1) {
1461 "fortune: FORTUNE_PATH: None of the specified "
1462 "directories found.\n");
1466 Fortune_path_arr[0] = FORTDIR;