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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1986, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 #include <sys/endian.h>
63 #include "pathnames.h"
69 #define MINW 6 /* minimum wait if desired */
70 #define CPERS 20 /* # of chars for each sec */
71 #define SLEN 160 /* # of chars in short fortune */
73 #define POS_UNKNOWN ((uint32_t) -1) /* pos for file unknown */
74 #define NO_PROB (-1) /* no prob specified for file */
77 #define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
91 char *datfile, *posfile;
96 struct fd *child, *parent;
97 struct fd *next, *prev;
100 bool Found_one; /* did we find a match? */
101 bool Find_files = FALSE; /* just find a list of proper fortune files */
102 bool Fortunes_only = FALSE; /* check only "fortunes" files */
103 bool Wait = FALSE; /* wait desired after fortune */
104 bool Short_only = FALSE; /* short fortune desired */
105 bool Long_only = FALSE; /* long fortune desired */
106 bool Offend = FALSE; /* offensive fortunes only */
107 bool All_forts = FALSE; /* any fortune allowed */
108 bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
109 bool Match = FALSE; /* dump fortunes matching a pattern */
111 bool Debug = FALSE; /* print debug messages */
114 char *Fortbuf = NULL; /* fortune buffer for -m */
118 off_t Seekpts[2]; /* seek pointers to fortunes */
120 FILEDESC *File_list = NULL, /* Head of file list */
121 *File_tail = NULL; /* Tail of file list */
122 FILEDESC *Fortfile; /* Fortune file to use */
124 STRFILE Noprob_tbl; /* sum of data for all no prob files */
126 const char *Fortune_path;
127 char **Fortune_path_arr;
129 int add_dir(FILEDESC *);
130 int add_file(int, const char *, const char *, FILEDESC **, FILEDESC **,
132 void all_forts(FILEDESC *, char *);
133 char *copy(const 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 *[]);
144 void init_prob(void);
145 int is_dir(const char *);
146 int is_fortfile(const char *, char **, char **, int);
147 int is_off_name(const char *);
151 char *off_name(const char *);
152 void open_dat(FILEDESC *);
153 void open_fp(FILEDESC *);
155 pick_child(FILEDESC *);
156 void print_file_list(void);
157 void print_list(FILEDESC *, int);
158 void sum_noprobs(FILEDESC *);
159 void sum_tbl(STRFILE *, STRFILE *);
161 void zero_tbl(STRFILE *);
163 char *conv_pat(char *);
164 int find_matches(void);
165 void matches_in_list(FILEDESC *);
166 int maxlen_in_list(FILEDESC *);
168 static regex_t Re_pat;
171 main(int argc, char *argv[])
173 #ifdef OK_TO_WRITE_DISK
175 #endif /* OK_TO_WRITE_DISK */
177 (void) setlocale(LC_ALL, "");
183 exit(find_matches() != 0);
188 } while ((Short_only && fortlen() > SLEN) ||
189 (Long_only && fortlen() <= SLEN));
193 #ifdef OK_TO_WRITE_DISK
194 if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
195 perror(Fortfile->posfile);
200 * if we can, we exclusive lock, but since it isn't very
201 * important, we just punt if we don't have easy locking
204 (void) flock(fd, LOCK_EX);
206 write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
207 if (!Fortfile->was_pos_file)
208 (void) chmod(Fortfile->path, 0666);
210 (void) flock(fd, LOCK_UN);
212 #endif /* OK_TO_WRITE_DISK */
216 sleep((unsigned int) max(Fort_len / CPERS, MINW));
223 display(FILEDESC *fp)
230 fseeko(fp->inf, Seekpts[0], SEEK_SET);
231 for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
232 !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
233 if (fp->tbl.str_flags & STR_ROTATED)
234 for (p = line; (ch = *p) != '\0'; ++p) {
237 *p = 'A' + (ch - 'A' + 13) % 26;
238 else if (islower(ch))
239 *p = 'a' + (ch - 'a' + 13) % 26;
242 if (fp->tbl.str_flags & STR_COMMENTS
243 && line[0] == fp->tbl.str_delim
244 && line[1] == fp->tbl.str_delim)
248 (void) fflush(stdout);
253 * Return the length of the fortune.
261 if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
262 nchar = (int)(Seekpts[1] - Seekpts[0]);
265 fseeko(Fortfile->inf, Seekpts[0], SEEK_SET);
267 while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
268 !STR_ENDSTRING(line, Fortfile->tbl))
269 nchar += strlen(line);
277 * This routine evaluates the arguments on the command line
280 getargs(int argc, char *argv[])
290 while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
292 while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
295 case 'a': /* any fortune */
304 Equal_probs++; /* scatter un-allocted prob equally */
306 case 'f': /* find fortune files */
309 case 'l': /* long ones only */
313 case 'o': /* offensive ones only */
316 case 's': /* short ones only */
320 case 'w': /* give time to read */
323 case 'm': /* dump out the fortunes */
327 case 'i': /* case-insensitive match */
337 if (!form_file_list(argv, argc))
338 exit(1); /* errors printed through form_file_list() */
353 error = regcomp(&Re_pat, pat, REG_BASIC);
355 fprintf(stderr, "regcomp(%s) fails\n", pat);
363 * Form the file list from the file specifications.
366 form_file_list(char **files, int file_cnt)
374 Fortunes_only = TRUE;
375 pstr = Fortune_path_arr;
378 i += add_file(NO_PROB, *pstr++, NULL,
379 &File_list, &File_tail, NULL);
381 Fortunes_only = FALSE;
383 fprintf(stderr, "No fortunes found in %s.\n",
388 pstr = Fortune_path_arr;
391 i += add_file(NO_PROB, "fortunes", *pstr++,
392 &File_list, &File_tail, NULL);
395 fprintf(stderr, "No fortunes found in %s.\n",
401 for (i = 0; i < file_cnt; i++) {
403 if (!isdigit((unsigned char)files[i][0]))
407 for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
408 percent = percent * 10 + *sp - '0';
410 fprintf(stderr, "percentages must be <= 100\n");
414 fprintf(stderr, "percentages must be integers\n");
418 * If the number isn't followed by a '%', then
419 * it was not a percentage, just the first part
420 * of a file name which starts with digits.
426 else if (*++sp == '\0') {
427 if (++i >= file_cnt) {
428 fprintf(stderr, "percentages must precede files\n");
434 if (strcmp(sp, "all") == 0) {
435 pstr = Fortune_path_arr;
438 i += add_file(NO_PROB, *pstr++, NULL,
439 &File_list, &File_tail, NULL);
442 fprintf(stderr, "No fortunes found in %s.\n",
446 } else if (!add_file(percent, sp, NULL, &File_list,
457 * Add a file to the file list.
460 add_file(int percent, const char *file, const char *dir, FILEDESC **head,
461 FILEDESC **tail, FILEDESC *parent)
466 char *tpath, *offensive;
476 tpath = do_malloc((unsigned int)(strlen(dir) + strlen(file) + 2));
477 strcat(strcat(strcpy(tpath, dir), "/"), file);
481 if ((isdir = is_dir(path)) && parent != NULL) {
484 return (FALSE); /* don't recurse */
487 if (!isdir && parent == NULL && (All_forts || Offend) &&
488 !is_off_name(path)) {
489 offensive = off_name(path);
496 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
497 file = off_name(file);
501 DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
503 if ((fd = open(path, O_RDONLY)) < 0) {
505 * This is a sneak. If the user said -a, and if the
506 * file we're given isn't a file, we check to see if
507 * there is a -o version. If there is, we treat it as
508 * if *that* were the file given. We only do this for
509 * individual files -- if we're scanning a directory,
510 * we'll pick up the -o file anyway.
512 if (All_forts && offensive != NULL) {
518 DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
519 file = off_name(file);
522 if (dir == NULL && file[0] != '/') {
524 char **pstr = Fortune_path_arr;
527 i += add_file(percent, file, *pstr++,
531 fprintf(stderr, "No '%s' found in %s.\n",
545 DPRINTF(2, (stderr, "path = \"%s\"\n", path));
549 fp->percent = percent;
554 if ((isdir && !add_dir(fp)) ||
556 !is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
560 "fortune:%s not a fortune file or directory\n",
564 do_free(fp->datfile);
565 do_free(fp->posfile);
571 * If the user said -a, we need to make this node a pointer to
572 * both files, if there are two. We don't need to do this if
573 * we are scanning a directory, since the scan will pick up the
576 if (All_forts && parent == NULL && !is_off_name(path))
577 all_forts(fp, offensive);
580 else if (fp->percent == NO_PROB) {
590 #ifdef OK_TO_WRITE_DISK
591 fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
592 #endif /* OK_TO_WRITE_DISK */
599 * Return a pointer to an initialized new FILEDESC.
606 fp = do_malloc(sizeof(*fp));
608 fp->pos = POS_UNKNOWN;
611 fp->percent = NO_PROB;
612 fp->read_tbl = FALSE;
625 * Return a pointer to the offensive version of a file of this name.
628 off_name(const char *file)
632 new = copy(file, (unsigned int) (strlen(file) + 2));
634 return (strcat(new, "-o"));
639 * Is the file an offensive-style name?
642 is_off_name(const char *file)
648 return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
653 * Modify a FILEDESC element to be the parent of two children if
654 * there are two children to be a parent of.
657 all_forts(FILEDESC *fp, char *offensive)
660 FILEDESC *scene, *obscene;
662 char *datfile, *posfile;
664 if (fp->child != NULL) /* this is a directory, not a file */
666 if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
668 if ((fd = open(offensive, O_RDONLY)) < 0)
670 DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
675 fp->num_children = 2;
677 scene->next = obscene;
678 obscene->next = NULL;
679 scene->child = obscene->child = NULL;
680 scene->parent = obscene->parent = fp;
683 scene->percent = obscene->percent = NO_PROB;
687 obscene->path = offensive;
688 if ((sp = rindex(offensive, '/')) == NULL)
689 obscene->name = offensive;
691 obscene->name = ++sp;
692 obscene->datfile = datfile;
693 obscene->posfile = posfile;
694 obscene->read_tbl = FALSE;
695 #ifdef OK_TO_WRITE_DISK
696 obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
697 #endif /* OK_TO_WRITE_DISK */
702 * Add the contents of an entire directory.
705 add_dir(FILEDESC *fp)
708 struct dirent *dirent;
712 (void) close(fp->fd);
714 if ((dir = opendir(fp->path)) == NULL) {
719 DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
720 fp->num_children = 0;
721 while ((dirent = readdir(dir)) != NULL) {
722 if (dirent->d_namlen == 0)
724 name = copy(dirent->d_name, dirent->d_namlen);
725 if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
730 if (fp->num_children == 0) {
731 (void) fprintf(stderr,
732 "fortune: %s: No fortune files in directory.\n", fp->path);
741 * Return TRUE if the file is a directory, FALSE otherwise.
744 is_dir(const char *file)
748 if (stat(file, &sbuf) < 0)
751 return (sbuf.st_mode & S_IFDIR);
756 * Return TRUE if the file is a fortune database file. We try and
757 * exclude files without reading them if possible to avoid
758 * overhead. Files which start with ".", or which have "illegal"
759 * suffixes, as contained in suflist[], are ruled out.
763 is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
768 static const char *suflist[] = {
769 /* list of "illegal" suffixes" */
770 "dat", "pos", "c", "h", "p", "i", "f",
771 "pas", "ftn", "ins.c", "ins,pas",
776 DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
779 * Preclude any -o files for offendable people, and any non -o
780 * files for completely offensive people.
782 if (check_for_offend && !All_forts) {
784 if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
785 DPRINTF(2, (stderr, "FALSE (offending file)\n"));
790 if ((sp = rindex(file, '/')) == NULL)
795 DPRINTF(2, (stderr, "FALSE (file starts with '.')\n"));
798 if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
799 DPRINTF(2, (stderr, "FALSE (check fortunes only)\n"));
802 if ((sp = rindex(sp, '.')) != NULL) {
804 for (i = 0; suflist[i] != NULL; i++)
805 if (strcmp(sp, suflist[i]) == 0) {
806 DPRINTF(2, (stderr, "FALSE (file has suffix \".%s\")\n", sp));
811 datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
812 strcat(datfile, ".dat");
813 if (access(datfile, R_OK) < 0) {
814 DPRINTF(2, (stderr, "FALSE (no readable \".dat\" file)\n"));
817 DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
827 #ifdef OK_TO_WRITE_DISK
828 *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
829 (void) strcat(*posp, ".pos");
832 #endif /* OK_TO_WRITE_DISK */
834 DPRINTF(2, (stderr, "TRUE\n"));
841 * Return a malloc()'ed copy of the string
844 copy(const char *str, unsigned int len)
848 new = do_malloc(len + 1);
859 * Do a malloc, checking for NULL return.
862 do_malloc(unsigned int size)
866 if ((new = malloc(size)) == NULL) {
867 (void) fprintf(stderr, "fortune: out of memory.\n");
876 * Free malloc'ed space, if any.
887 * Initialize the fortune probabilities.
892 FILEDESC *fp, *last = NULL;
893 int percent, num_noprob, frac;
896 * Distribute the residual probability (if any) across all
897 * files with unspecified probability (i.e., probability of 0)
903 for (fp = File_tail; fp != NULL; fp = fp->prev)
904 if (fp->percent == NO_PROB) {
909 percent += fp->percent;
910 DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
911 percent, num_noprob));
913 (void) fprintf(stderr,
914 "fortune: probabilities sum to %d%% > 100%%!\n", percent);
916 } else if (percent < 100 && num_noprob == 0) {
917 (void) fprintf(stderr,
918 "fortune: no place to put residual probability (%d%% < 100%%)\n",
921 } else if (percent == 100 && num_noprob != 0) {
922 (void) fprintf(stderr,
923 "fortune: no probability left to put in residual files (100%%)\n");
926 percent = 100 - percent;
928 if (num_noprob != 0) {
929 if (num_noprob > 1) {
930 frac = percent / num_noprob;
931 DPRINTF(1, (stderr, ", frac = %d%%", frac));
932 for (fp = File_tail; fp != last; fp = fp->prev)
933 if (fp->percent == NO_PROB) {
938 last->percent = percent;
939 DPRINTF(1, (stderr, ", residual = %d%%", percent));
943 ", %d%% distributed over remaining fortunes\n",
946 DPRINTF(1, (stderr, "\n"));
956 * Get the fortune data file's seek pointer for the next fortune.
964 if (File_list->next == NULL || File_list->percent == NO_PROB)
967 choice = arc4random_uniform(100);
968 DPRINTF(1, (stderr, "choice = %d\n", choice));
969 for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
970 if (choice < fp->percent)
973 choice -= fp->percent;
975 " skip \"%s\", %d%% (choice = %d)\n",
976 fp->name, fp->percent, choice));
979 "using \"%s\", %d%% (choice = %d)\n",
980 fp->name, fp->percent, choice));
982 if (fp->percent != NO_PROB)
985 if (fp->next != NULL) {
987 choice = arc4random_uniform(Noprob_tbl.str_numstr);
988 DPRINTF(1, (stderr, "choice = %d (of %u) \n", choice,
989 Noprob_tbl.str_numstr));
990 while ((unsigned int)choice >= fp->tbl.str_numstr) {
991 choice -= fp->tbl.str_numstr;
994 " skip \"%s\", %u (choice = %d)\n",
995 fp->name, fp->tbl.str_numstr,
998 DPRINTF(1, (stderr, "using \"%s\", %u\n", fp->name,
999 fp->tbl.str_numstr));
1003 if (fp->child != NULL) {
1004 DPRINTF(1, (stderr, "picking child\n"));
1005 fp = pick_child(fp);
1011 (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), SEEK_SET);
1012 read(fp->datfd, Seekpts, sizeof Seekpts);
1013 Seekpts[0] = be64toh(Seekpts[0]);
1014 Seekpts[1] = be64toh(Seekpts[1]);
1019 * Pick a child from a chosen parent.
1022 pick_child(FILEDESC *parent)
1028 choice = arc4random_uniform(parent->num_children);
1029 DPRINTF(1, (stderr, " choice = %d (of %d)\n",
1030 choice, parent->num_children));
1031 for (fp = parent->child; choice--; fp = fp->next)
1033 DPRINTF(1, (stderr, " using %s\n", fp->name));
1038 choice = arc4random_uniform(parent->tbl.str_numstr);
1039 DPRINTF(1, (stderr, " choice = %d (of %u)\n",
1040 choice, parent->tbl.str_numstr));
1041 for (fp = parent->child; (unsigned)choice >= fp->tbl.str_numstr;
1043 choice -= fp->tbl.str_numstr;
1044 DPRINTF(1, (stderr, "\tskip %s, %u (choice = %d)\n",
1045 fp->name, fp->tbl.str_numstr, choice));
1047 DPRINTF(1, (stderr, " using %s, %u\n", fp->name,
1048 fp->tbl.str_numstr));
1055 * Sum up all the noprob probabilities, starting with fp.
1058 sum_noprobs(FILEDESC *fp)
1060 static bool did_noprobs = FALSE;
1064 zero_tbl(&Noprob_tbl);
1065 while (fp != NULL) {
1067 sum_tbl(&Noprob_tbl, &fp->tbl);
1076 return (i >= j ? i : j);
1081 * Assocatiate a FILE * with the given FILEDESC.
1084 open_fp(FILEDESC *fp)
1086 if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
1094 * Open up the dat file if we need to.
1097 open_dat(FILEDESC *fp)
1099 if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY)) < 0) {
1100 perror(fp->datfile);
1107 * Get the position from the pos file, if there is one. If not,
1108 * return a random number.
1111 get_pos(FILEDESC *fp)
1113 #ifdef OK_TO_WRITE_DISK
1115 #endif /* OK_TO_WRITE_DISK */
1117 assert(fp->read_tbl);
1118 if (fp->pos == POS_UNKNOWN) {
1119 #ifdef OK_TO_WRITE_DISK
1120 if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
1121 read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
1122 fp->pos = arc4random_uniform(fp->tbl.str_numstr);
1123 else if (fp->pos >= fp->tbl.str_numstr)
1124 fp->pos %= fp->tbl.str_numstr;
1128 fp->pos = arc4random_uniform(fp->tbl.str_numstr);
1129 #endif /* OK_TO_WRITE_DISK */
1131 if (++(fp->pos) >= fp->tbl.str_numstr)
1132 fp->pos -= fp->tbl.str_numstr;
1133 DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, (long)fp->pos));
1138 * Get the tbl data file the datfile.
1141 get_tbl(FILEDESC *fp)
1148 if (fp->child == NULL) {
1149 if ((fd = open(fp->datfile, O_RDONLY)) < 0) {
1150 perror(fp->datfile);
1153 if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
1154 (void)fprintf(stderr,
1155 "fortune: %s corrupted\n", fp->path);
1158 /* fp->tbl.str_version = be32toh(fp->tbl.str_version); */
1159 fp->tbl.str_numstr = be32toh(fp->tbl.str_numstr);
1160 fp->tbl.str_longlen = be32toh(fp->tbl.str_longlen);
1161 fp->tbl.str_shortlen = be32toh(fp->tbl.str_shortlen);
1162 fp->tbl.str_flags = be32toh(fp->tbl.str_flags);
1167 for (child = fp->child; child != NULL; child = child->next) {
1169 sum_tbl(&fp->tbl, &child->tbl);
1172 fp->read_tbl = TRUE;
1177 * Zero out the fields we care about in a tbl structure.
1180 zero_tbl(STRFILE *tp)
1183 tp->str_longlen = 0;
1184 tp->str_shortlen = ~0;
1189 * Merge the tbl data of t2 into t1.
1192 sum_tbl(STRFILE *t1, STRFILE *t2)
1194 t1->str_numstr += t2->str_numstr;
1195 if (t1->str_longlen < t2->str_longlen)
1196 t1->str_longlen = t2->str_longlen;
1197 if (t1->str_shortlen > t2->str_shortlen)
1198 t1->str_shortlen = t2->str_shortlen;
1201 #define STR(str) ((str) == NULL ? "NULL" : (str))
1205 * Print out the file list
1208 print_file_list(void)
1210 print_list(File_list, 0);
1215 * Print out the actual list, recursively.
1218 print_list(FILEDESC *list, int lev)
1220 while (list != NULL) {
1221 fprintf(stderr, "%*s", lev * 4, "");
1222 if (list->percent == NO_PROB)
1223 fprintf(stderr, "___%%");
1225 fprintf(stderr, "%3d%%", list->percent);
1226 fprintf(stderr, " %s", STR(list->name));
1227 DPRINTF(1, (stderr, " (%s, %s, %s)", STR(list->path),
1228 STR(list->datfile), STR(list->posfile)));
1229 fprintf(stderr, "\n");
1230 if (list->child != NULL)
1231 print_list(list->child, lev + 1);
1238 * Convert the pattern to an ignore-case equivalent.
1241 conv_pat(char *orig)
1247 cnt = 1; /* allow for '\0' */
1248 for (sp = orig; *sp != '\0'; sp++)
1249 if (isalpha((unsigned char)*sp))
1253 if ((new = malloc(cnt)) == NULL) {
1254 fprintf(stderr, "pattern too long for ignoring case\n");
1258 for (sp = new; *orig != '\0'; orig++) {
1259 if (islower((unsigned char)*orig)) {
1262 *sp++ = toupper((unsigned char)*orig);
1265 else if (isupper((unsigned char)*orig)) {
1268 *sp++ = tolower((unsigned char)*orig);
1281 * Find all the fortunes which match the pattern we've been given.
1286 Fort_len = maxlen_in_list(File_list);
1287 DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
1288 /* extra length, "%\n" is appended */
1289 Fortbuf = do_malloc((unsigned int) Fort_len + 10);
1292 matches_in_list(File_list);
1299 * Return the maximum fortune len in the file list.
1302 maxlen_in_list(FILEDESC *list)
1308 for (fp = list; fp != NULL; fp = fp->next) {
1309 if (fp->child != NULL) {
1310 if ((len = maxlen_in_list(fp->child)) > maxlen)
1315 if (fp->tbl.str_longlen > (unsigned int)maxlen)
1316 maxlen = fp->tbl.str_longlen;
1325 * Print out the matches from the files in the list.
1328 matches_in_list(FILEDESC *list)
1335 for (fp = list; fp != NULL; fp = fp->next) {
1336 if (fp->child != NULL) {
1337 matches_in_list(fp->child);
1340 DPRINTF(1, (stderr, "searching in %s\n", fp->path));
1344 while (fgets(sp, Fort_len, fp->inf) != NULL)
1345 if (fp->tbl.str_flags & STR_COMMENTS
1346 && sp[0] == fp->tbl.str_delim
1347 && sp[1] == fp->tbl.str_delim)
1349 else if (!STR_ENDSTRING(sp, fp->tbl))
1353 if (fp->tbl.str_flags & STR_ROTATED)
1354 for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
1357 *p = 'A' + (ch - 'A' + 13) % 26;
1358 else if (islower(ch))
1359 *p = 'a' + (ch - 'a' + 13) % 26;
1362 if (regexec(&Re_pat, Fortbuf, 0, NULL, 0) != REG_NOMATCH) {
1363 printf("%c%c", fp->tbl.str_delim,
1366 printf(" (%s)", fp->name);
1371 (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout);
1381 (void) fprintf(stderr, "fortune [-a");
1383 (void) fprintf(stderr, "D");
1385 (void) fprintf(stderr, "efilosw]");
1386 (void) fprintf(stderr, " [-m pattern]");
1387 (void) fprintf(stderr, " [[N%%] file/directory/all]\n");
1393 * Set up file search patch from environment var FORTUNE_PATH;
1394 * if not set, use the compiled in FORTDIR.
1401 char *pch, **ppch, *str, *path;
1404 Fortune_path = getenv("FORTUNE_PATH");
1405 if (Fortune_path == NULL) {
1406 Fortune_path = FORTDIR;
1409 path = strdup(Fortune_path);
1411 for (nstr = 2, pch = path; *pch != '\0'; pch++) {
1416 ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *));
1419 str = strtok(path, ":");
1425 str = strtok(NULL, ":");
1429 if (foundenv == 1) {
1431 "fortune: FORTUNE_PATH: None of the specified "
1432 "directories found.\n");
1436 Fortune_path_arr[0] = strdup(FORTDIR);