coverity appeasement - redundant check
[minix.git] / commands / fortune / fortune.c
blobd8d631b60bc57210bb7119aa6c82f96ebfadf133
1 /* fortune - hand out Chinese fortune cookies Author: Bert Reuling */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <time.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
10 #define COOKIEJAR "/usr/lib/fortune.dat"
12 static char *Copyright = "\0Copyright (c) 1990 Bert Reuling";
13 static unsigned long seed;
15 int main(int argc, char **argv);
16 unsigned long magic(unsigned long range);
18 int main(argc, argv)
19 int argc;
20 char *argv[];
22 int c1, c2, c3;
23 struct stat cookie_stat;
24 FILE *cookie;
26 if ((cookie = fopen(COOKIEJAR, "r")) == NULL) {
27 printf("\nSome things better stay closed.\n - %s\n", argv[0]);
28 exit (-1);
31 /* Create seed from : date, time, user-id and process-id. we can't get
32 * the position of the moon, unfortunately.
34 seed = time( (time_t *) 0) ^ (long) getuid() ^ (long) getpid();
36 if (stat(COOKIEJAR, &cookie_stat) != 0) {
37 printf("\nIt furthers one to see the super guru.\n - %s\n", argv[0]);
38 exit (-1);
40 fseek(cookie, magic((unsigned long) cookie_stat.st_size), 0); /* m ove bu magic... */
42 c2 = c3 = '\n';
43 while (((c1 = getc(cookie)) != EOF) && ((c1 != '%') || (c2 != '%') || (c3 != '\n'))) {
44 c3 = c2;
45 c2 = c1;
48 if (c1 == EOF) {
49 printf("\nSomething unexpected has happened.\n - %s", argv[0]);
50 exit (-1);
53 c2 = c3 = '\n';
54 while (((c1 = getc(cookie)) != '%') || (c2 != '%') || (c3 != '\n')) {
55 if (c1 == EOF) {
56 rewind(cookie);
57 continue;
59 putc(c2, stdout);
60 c3 = c2;
61 c2 = c1;
63 putc('\n', stdout);
64 fclose(cookie);
65 return (0);
68 /* magic - please study carefull: there is more than meets the eye */
69 unsigned long magic(range)
70 unsigned long range;
73 seed = 9065531L * (seed % 9065533L) - 2 * (seed / 9065531L) + 1L;
74 return (seed % range);