This repository has been archived by the owner on Apr 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fortune.c
1442 lines (1318 loc) · 31.3 KB
/
fortune.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 1986, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ken Arnold.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1986, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static const char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
# include <sys/param.h>
# include <sys/stat.h>
# include <dirent.h>
# include <fcntl.h>
# include <assert.h>
# include <unistd.h>
# include <stdio.h>
# include <ctype.h>
# include <stdlib.h>
# include <string.h>
# include <locale.h>
# include <time.h>
# include "strfile.h"
# include "pathnames.h"
/* CFS - additions for MacOS X Server and OpenStep */
#include <sys/types.h>
#include <sys/dir.h>
#include <libc.h>
#include <regex.h>
/* END CFS ADDITIONS */
/* CFS - NOT REQUIRED in OpenStep or MacOS X Server
# define TRUE 1
# define FALSE 0
*/
# define bool short
# define MINW 6 /* minimum wait if desired */
# define CPERS 20 /* # of chars for each sec */
# define SLEN 160 /* # of chars in short fortune */
# define POS_UNKNOWN ((long) -1) /* pos for file unknown */
# define NO_PROB (-1) /* no prob specified for file */
# ifdef DEBUG
# define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
# undef NDEBUG
# else
# define DPRINTF(l,x)
# define NDEBUG 1
# endif
typedef struct fd {
int percent;
int fd, datfd;
long pos;
FILE *inf;
char *name;
char *path;
char *datfile, *posfile;
bool read_tbl;
bool was_pos_file;
STRFILE tbl;
int num_children;
struct fd *child, *parent;
struct fd *next, *prev;
} FILEDESC;
bool Found_one; /* did we find a match? */
bool Find_files = FALSE; /* just find a list of proper fortune files */
bool Fortunes_only = FALSE; /* check only "fortunes" files */
bool Wait = FALSE; /* wait desired after fortune */
bool Short_only = FALSE; /* short fortune desired */
bool Long_only = FALSE; /* long fortune desired */
bool Offend = FALSE; /* offensive fortunes only */
bool All_forts = FALSE; /* any fortune allowed */
bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
#ifndef NO_REGEX
bool Match = FALSE; /* dump fortunes matching a pattern */
#endif
#ifdef DEBUG
bool Debug = FALSE; /* print debug messages */
#endif
char *Fortbuf = NULL; /* fortune buffer for -m */
int Fort_len = 0;
long Seekpts[2]; /* seek pointers to fortunes */
FILEDESC *File_list = NULL, /* Head of file list */
*File_tail = NULL; /* Tail of file list */
FILEDESC *Fortfile; /* Fortune file to use */
STRFILE Noprob_tbl; /* sum of data for all no prob files */
int add_dir __P((FILEDESC *));
int add_file __P((int,
char *, char *, FILEDESC **, FILEDESC **, FILEDESC *));
void all_forts __P((FILEDESC *, char *));
char *copy __P((char *, u_int));
void display __P((FILEDESC *));
void do_free __P((void *));
void *do_malloc __P((u_int));
int form_file_list __P((char **, int));
int fortlen __P((void));
void get_fort __P((void));
void get_pos __P((FILEDESC *));
void get_tbl __P((FILEDESC *));
void getargs __P((int, char *[]));
void init_prob __P((void));
int is_dir __P((char *));
int is_fortfile __P((char *, char **, char **, int));
int is_off_name __P((char *));
int max __P((int, int));
FILEDESC *
new_fp __P((void));
char *off_name __P((char *));
void open_dat __P((FILEDESC *));
void open_fp __P((FILEDESC *));
FILEDESC *
pick_child __P((FILEDESC *));
void print_file_list __P((void));
void print_list __P((FILEDESC *, int));
void sum_noprobs __P((FILEDESC *));
void sum_tbl __P((STRFILE *, STRFILE *));
void usage __P((void));
void zero_tbl __P((STRFILE *));
#ifndef NO_REGEX
char *conv_pat __P((char *));
int find_matches __P((void));
void matches_in_list __P((FILEDESC *));
int maxlen_in_list __P((FILEDESC *));
#endif
#ifndef NO_REGEX
#ifdef REGCMP
# define RE_COMP(p) (Re_pat = regcmp(p, NULL))
# define BAD_COMP(f) (NULL == (f))
# define RE_EXEC(p) regex(Re_pat, (p))
char *Re_pat;
char *regcmp(), *regex();
#else
# define RE_COMP(p) (p = (char*)re_comp(p))
# define BAD_COMP(f) ((f) != NULL)
# define RE_EXEC(p) re_exec(p)
#endif
#endif
int
main(ac, av)
int ac;
char *av[];
{
#ifdef OK_TO_WRITE_DISK
int fd;
#endif /* OK_TO_WRITE_DISK */
(void) setlocale(LC_ALL, "");
getargs(ac, av);
#ifndef NO_REGEX
if (Match)
exit(find_matches() != 0);
#endif
init_prob();
/* CFS - replaced call to srandomdev with srandom, since we don't have
srandomdev on OpenStep or MacOS X Server */
srandom((int)(time((time_t *) NULL) + getpid()));
do {
get_fort();
} while ((Short_only && fortlen() > SLEN) ||
(Long_only && fortlen() <= SLEN));
display(Fortfile);
#ifdef OK_TO_WRITE_DISK
if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
perror(Fortfile->posfile);
exit(1);
}
#ifdef LOCK_EX
/*
* if we can, we exclusive lock, but since it isn't very
* important, we just punt if we don't have easy locking
* available.
*/
(void) flock(fd, LOCK_EX);
#endif /* LOCK_EX */
write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
if (!Fortfile->was_pos_file)
(void) chmod(Fortfile->path, 0666);
#ifdef LOCK_EX
(void) flock(fd, LOCK_UN);
#endif /* LOCK_EX */
#endif /* OK_TO_WRITE_DISK */
if (Wait) {
if (Fort_len == 0)
(void) fortlen();
sleep((unsigned int) max(Fort_len / CPERS, MINW));
}
exit(0);
/* NOTREACHED */
}
void
display(fp)
FILEDESC *fp;
{
register char *p;
register unsigned char ch;
char line[BUFSIZ];
open_fp(fp);
(void) fseek(fp->inf, Seekpts[0], 0);
for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
!STR_ENDSTRING(line, fp->tbl); Fort_len++) {
if (fp->tbl.str_flags & STR_ROTATED)
for (p = line; (ch = *p) != '\0'; ++p) {
if (isascii(ch)) {
if (isupper(ch))
*p = 'A' + (ch - 'A' + 13) % 26;
else if (islower(ch))
*p = 'a' + (ch - 'a' + 13) % 26;
}
}
fputs(line, stdout);
}
(void) fflush(stdout);
}
/*
* fortlen:
* Return the length of the fortune.
*/
int
fortlen()
{
register int nchar;
char line[BUFSIZ];
if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
nchar = (Seekpts[1] - Seekpts[0] <= SLEN);
else {
open_fp(Fortfile);
(void) fseek(Fortfile->inf, Seekpts[0], 0);
nchar = 0;
while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
!STR_ENDSTRING(line, Fortfile->tbl))
nchar += strlen(line);
}
Fort_len = nchar;
return nchar;
}
/*
* This routine evaluates the arguments on the command line
*/
void
getargs(argc, argv)
register int argc;
register char **argv;
{
register int ignore_case;
# ifndef NO_REGEX
register char *pat;
# endif /* NO_REGEX */
extern char *optarg;
extern int optind;
int ch;
ignore_case = FALSE;
# ifndef NO_REGEX
pat = NULL;
# endif /* NO_REGEX */
# ifdef DEBUG
while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
#else
while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
#endif /* DEBUG */
switch(ch) {
case 'a': /* any fortune */
All_forts++;
break;
# ifdef DEBUG
case 'D':
Debug++;
break;
# endif /* DEBUG */
case 'e':
Equal_probs++; /* scatter un-allocted prob equally */
break;
case 'f': /* find fortune files */
Find_files++;
break;
case 'l': /* long ones only */
Long_only++;
Short_only = FALSE;
break;
case 'o': /* offensive ones only */
Offend++;
break;
case 's': /* short ones only */
Short_only++;
Long_only = FALSE;
break;
case 'w': /* give time to read */
Wait++;
break;
# ifdef NO_REGEX
case 'i': /* case-insensitive match */
case 'm': /* dump out the fortunes */
(void) fprintf(stderr,
"fortune: can't match fortunes on this system (Sorry)\n");
exit(0);
# else /* NO_REGEX */
case 'm': /* dump out the fortunes */
Match++;
pat = optarg;
break;
case 'i': /* case-insensitive match */
ignore_case++;
break;
# endif /* NO_REGEX */
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (!form_file_list(argv, argc))
exit(1); /* errors printed through form_file_list() */
if (Find_files) {
print_file_list();
exit(0);
}
#ifdef DEBUG
else if (Debug >= 1)
print_file_list();
#endif /* DEBUG */
# ifndef NO_REGEX
if (pat != NULL) {
if (ignore_case)
pat = conv_pat(pat);
if (BAD_COMP(RE_COMP(pat))) {
#ifndef REGCMP
fprintf(stderr, "%s\n", pat);
#else /* REGCMP */
fprintf(stderr, "bad pattern: %s\n", pat);
#endif /* REGCMP */
}
}
# endif /* NO_REGEX */
}
/*
* form_file_list:
* Form the file list from the file specifications.
*/
int form_file_list(files, file_cnt)
register char **files;
register int file_cnt;
{
register int i, percent;
register char *sp;
if (file_cnt == 0)
{
if (Find_files)
{
Fortunes_only = TRUE;
i = add_file(NO_PROB, TRUEFORTDIR, NULL, &File_list, &File_tail, NULL);
Fortunes_only = FALSE;
return i;
}
else
return add_file(NO_PROB, "fortunes", FORTDIR, &File_list, &File_tail, NULL);
}
for (i = 0; i < file_cnt; i++)
{
percent = NO_PROB;
if (!isdigit((unsigned char)files[i][0]))
sp = files[i];
else
{
percent = 0;
for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
percent = percent * 10 + *sp - '0';
if (percent > 100)
{
fprintf(stderr, "percentages must be <= 100\n");
return FALSE;
}
if (*sp == '.')
{
fprintf(stderr, "percentages must be integers\n");
return FALSE;
}
/*
* If the number isn't followed by a '%', then
* it was not a percentage, just the first part
* of a file name which starts with digits.
*/
if (*sp != '%') {
percent = NO_PROB;
sp = files[i];
}
else if (*++sp == '\0') {
if (++i >= file_cnt) {
fprintf(stderr, "percentages must precede files\n");
return FALSE;
}
sp = files[i];
}
}
if (strcmp(sp, "all") == 0)
sp = FORTDIR;
if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
return FALSE;
}
return TRUE;
}
/*
* add_file:
* Add a file to the file list.
*/
int
add_file(percent, file, dir, head, tail, parent)
int percent;
register char *file;
char *dir;
FILEDESC **head, **tail;
FILEDESC *parent;
{
register FILEDESC *fp;
register int fd;
register char *path, *offensive;
register bool was_malloc;
register bool isdir;
if (dir == NULL) {
path = file;
was_malloc = FALSE;
}
else {
path = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2));
(void) strcat(strcat(strcpy(path, dir), "/"), file);
was_malloc = TRUE;
}
if ((isdir = is_dir(path)) && parent != NULL) {
if (was_malloc)
free(path);
return FALSE; /* don't recurse */
}
offensive = NULL;
if (!isdir && parent == NULL && (All_forts || Offend) &&
!is_off_name(path)) {
offensive = off_name(path);
if (Offend) {
if (was_malloc)
free(path);
path = offensive;
offensive = NULL;
was_malloc = TRUE;
DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
file = off_name(file);
}
}
DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
over:
if ((fd = open(path, 0)) < 0) {
/*
* This is a sneak. If the user said -a, and if the
* file we're given isn't a file, we check to see if
* there is a -o version. If there is, we treat it as
* if *that* were the file given. We only do this for
* individual files -- if we're scanning a directory,
* we'll pick up the -o file anyway.
*/
if (All_forts && offensive != NULL) {
if (was_malloc)
free(path);
path = offensive;
offensive = NULL;
was_malloc = TRUE;
DPRINTF(1, (stderr, "\ttrying \"%s\"\n", path));
file = off_name(file);
goto over;
}
if (dir == NULL && file[0] != '/')
return add_file(percent, file, TRUEFORTDIR, head, tail,
parent);
if (parent == NULL)
perror(path);
if (was_malloc)
free(path);
return FALSE;
}
DPRINTF(2, (stderr, "path = \"%s\"\n", path));
fp = new_fp();
fp->fd = fd;
fp->percent = percent;
fp->name = file;
fp->path = path;
fp->parent = parent;
if ((isdir && !add_dir(fp)) ||
(!isdir &&
!is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
{
if (parent == NULL)
fprintf(stderr,
"fortune:%s not a fortune file or directory\n",
path);
free((char *) fp);
if (was_malloc)
free(path);
do_free(fp->datfile);
do_free(fp->posfile);
do_free(offensive);
return FALSE;
}
/*
* If the user said -a, we need to make this node a pointer to
* both files, if there are two. We don't need to do this if
* we are scanning a directory, since the scan will pick up the
* -o file anyway.
*/
if (All_forts && parent == NULL && !is_off_name(path))
all_forts(fp, offensive);
if (*head == NULL)
*head = *tail = fp;
else if (fp->percent == NO_PROB) {
(*tail)->next = fp;
fp->prev = *tail;
*tail = fp;
}
else {
(*head)->prev = fp;
fp->next = *head;
*head = fp;
}
#ifdef OK_TO_WRITE_DISK
fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
#endif /* OK_TO_WRITE_DISK */
return TRUE;
}
/*
* new_fp:
* Return a pointer to an initialized new FILEDESC.
*/
FILEDESC *
new_fp()
{
register FILEDESC *fp;
fp = (FILEDESC *) do_malloc(sizeof *fp);
fp->datfd = -1;
fp->pos = POS_UNKNOWN;
fp->inf = NULL;
fp->fd = -1;
fp->percent = NO_PROB;
fp->read_tbl = FALSE;
fp->next = NULL;
fp->prev = NULL;
fp->child = NULL;
fp->parent = NULL;
fp->datfile = NULL;
fp->posfile = NULL;
return fp;
}
/*
* off_name:
* Return a pointer to the offensive version of a file of this name.
*/
char *
off_name(file)
char *file;
{
char *new;
new = copy(file, (unsigned int) (strlen(file) + 2));
return strcat(new, "-o");
}
/*
* is_off_name:
* Is the file an offensive-style name?
*/
int
is_off_name(file)
char *file;
{
int len;
len = strlen(file);
return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
}
/*
* all_forts:
* Modify a FILEDESC element to be the parent of two children if
* there are two children to be a parent of.
*/
void
all_forts(fp, offensive)
register FILEDESC *fp;
char *offensive;
{
register char *sp;
register FILEDESC *scene, *obscene;
register int fd;
auto char *datfile, *posfile;
if (fp->child != NULL) /* this is a directory, not a file */
return;
if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
return;
if ((fd = open(offensive, 0)) < 0)
return;
DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
scene = new_fp();
obscene = new_fp();
*scene = *fp;
fp->num_children = 2;
fp->child = scene;
scene->next = obscene;
obscene->next = NULL;
scene->child = obscene->child = NULL;
scene->parent = obscene->parent = fp;
fp->fd = -1;
scene->percent = obscene->percent = NO_PROB;
obscene->fd = fd;
obscene->inf = NULL;
obscene->path = offensive;
if ((sp = rindex(offensive, '/')) == NULL)
obscene->name = offensive;
else
obscene->name = ++sp;
obscene->datfile = datfile;
obscene->posfile = posfile;
obscene->read_tbl = FALSE;
#ifdef OK_TO_WRITE_DISK
obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
#endif /* OK_TO_WRITE_DISK */
}
/*
* add_dir:
* Add the contents of an entire directory.
*/
int
add_dir(fp)
register FILEDESC *fp;
{
register DIR *dir;
/* CFS - Changed "dirent" to "direct" for OpenStep/MacOS X Server build */
register struct direct *dirent;
auto FILEDESC *tailp;
auto char *name;
(void) close(fp->fd);
fp->fd = -1;
if ((dir = opendir(fp->path)) == NULL) {
perror(fp->path);
return FALSE;
}
tailp = NULL;
DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
fp->num_children = 0;
while ((dirent = readdir(dir)) != NULL) {
if (dirent->d_namlen == 0)
continue;
name = copy(dirent->d_name, dirent->d_namlen);
if (add_file(NO_PROB, name, fp->path, &fp->child, &tailp, fp))
fp->num_children++;
else
free(name);
}
if (fp->num_children == 0) {
(void) fprintf(stderr,
"fortune: %s: No fortune files in directory.\n", fp->path);
return FALSE;
}
return TRUE;
}
/*
* is_dir:
* Return TRUE if the file is a directory, FALSE otherwise.
*/
int
is_dir(file)
char *file;
{
auto struct stat sbuf;
if (stat(file, &sbuf) < 0)
return FALSE;
return (sbuf.st_mode & S_IFDIR);
}
/*
* is_fortfile:
* Return TRUE if the file is a fortune database file. We try and
* exclude files without reading them if possible to avoid
* overhead. Files which start with ".", or which have "illegal"
* suffixes, as contained in suflist[], are ruled out.
*/
/* ARGSUSED */
int
is_fortfile(file, datp, posp, check_for_offend)
char *file, **datp, **posp;
int check_for_offend;
{
register int i;
register char *sp;
register char *datfile;
static char *suflist[] = { /* list of "illegal" suffixes" */
"dat", "pos", "c", "h", "p", "i", "f",
"pas", "ftn", "ins.c", "ins,pas",
"ins.ftn", "sml",
NULL
};
DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
/*
* Preclude any -o files for offendable people, and any non -o
* files for completely offensive people.
*/
if (check_for_offend && !All_forts) {
i = strlen(file);
if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
DPRINTF(2, (stderr, "FALSE (offending file)\n"));
return FALSE;
}
}
if ((sp = rindex(file, '/')) == NULL)
sp = file;
else
sp++;
if (*sp == '.') {
DPRINTF(2, (stderr, "FALSE (file starts with '.')\n"));
return FALSE;
}
if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
DPRINTF(2, (stderr, "FALSE (check fortunes only)\n"));
return FALSE;
}
if ((sp = rindex(sp, '.')) != NULL) {
sp++;
for (i = 0; suflist[i] != NULL; i++)
if (strcmp(sp, suflist[i]) == 0) {
DPRINTF(2, (stderr, "FALSE (file has suffix \".%s\")\n", sp));
return FALSE;
}
}
datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
strcat(datfile, ".dat");
if (access(datfile, R_OK) < 0) {
DPRINTF(2, (stderr, "FALSE (no readable \".dat\" file)\n"));
#ifdef DEBUG
if (Debug < 2)
DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
#endif
free(datfile);
return FALSE;
}
if (datp != NULL)
*datp = datfile;
else
free(datfile);
if (posp != NULL) {
#ifdef OK_TO_WRITE_DISK
*posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
(void) strcat(*posp, ".pos");
#else
*posp = NULL;
#endif /* OK_TO_WRITE_DISK */
}
DPRINTF(2, (stderr, "TRUE\n"));
return TRUE;
}
/*
* copy:
* Return a malloc()'ed copy of the string
*/
char *
copy(str, len)
char *str;
unsigned int len;
{
char *new, *sp;
new = do_malloc(len + 1);
sp = new;
do {
*sp++ = *str;
} while (*str++);
return new;
}
/*
* do_malloc:
* Do a malloc, checking for NULL return.
*/
void *
do_malloc(size)
unsigned int size;
{
void *new;
if ((new = malloc(size)) == NULL) {
(void) fprintf(stderr, "fortune: out of memory.\n");
exit(1);
}
return new;
}
/*
* do_free:
* Free malloc'ed space, if any.
*/
void
do_free(ptr)
void *ptr;
{
if (ptr != NULL)
free(ptr);
}
/*
* init_prob:
* Initialize the fortune probabilities.
*/
void
init_prob()
{
register FILEDESC *fp, *last = NULL;
register int percent, num_noprob, frac;
/*
* Distribute the residual probability (if any) across all
* files with unspecified probability (i.e., probability of 0)
* (if any).
*/
percent = 0;
num_noprob = 0;
for (fp = File_tail; fp != NULL; fp = fp->prev)
if (fp->percent == NO_PROB) {
num_noprob++;
if (Equal_probs)
last = fp;
}
else
percent += fp->percent;
DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
percent, num_noprob));
if (percent > 100) {
(void) fprintf(stderr,
"fortune: probabilities sum to %d%% > 100%%!\n", percent);
exit(1);
}
else if (percent < 100 && num_noprob == 0) {
(void) fprintf(stderr,
"fortune: no place to put residual probability (%d%% < 100%%)\n",
percent);
exit(1);
}
else if (percent == 100 && num_noprob != 0) {
(void) fprintf(stderr,
"fortune: no probability left to put in residual files (100%%)\n");
exit(1);
}
percent = 100 - percent;
if (Equal_probs)
{
if (num_noprob != 0)
{
if (num_noprob > 1)
{
frac = percent / num_noprob;
DPRINTF(1, (stderr, ", frac = %d%%", frac));
for (fp = File_list; fp != last; fp = fp->next)
{
if (fp->percent == NO_PROB)
{
fp->percent = frac;
percent -= frac;
}
}
}
last->percent = percent;
DPRINTF(1, (stderr, ", residual = %d%%", percent));
}
}
else
{
DPRINTF(1, (stderr,
", %d%% distributed over remaining fortunes\n",
percent));
}
DPRINTF(1, (stderr, "\n"));
#ifdef DEBUG
if (Debug >= 1) print_file_list();
#endif
}
/*
* get_fort:
* Get the fortune data file's seek pointer for the next fortune.
*/
void get_fort()
{
register FILEDESC *fp;
register int choice;
if (File_list->next == NULL || File_list->percent == NO_PROB)
fp = File_list;
else