source: trunk/minix/commands/simple/fortune.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.7 KB
Line 
1/* fortune - hand out Chinese fortune cookies Author: Bert Reuling */
2
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>
9
10#define COOKIEJAR "/usr/lib/fortune.dat"
11
12static char *Copyright = "\0Copyright (c) 1990 Bert Reuling";
13static unsigned long seed;
14
15_PROTOTYPE(int main, (int argc, char **argv));
16_PROTOTYPE(unsigned long magic, (unsigned long range));
17
18int main(argc, argv)
19int argc;
20char *argv[];
21{
22 int c1, c2, c3;
23 struct stat cookie_stat;
24 FILE *cookie;
25
26 if ((cookie = fopen(COOKIEJAR, "r")) == NULL) {
27 printf("\nSome things better stay closed.\n - %s\n", argv[0]);
28 exit (-1);
29 }
30
31 /* Create seed from : date, time, user-id and process-id. we can't get
32 * the position of the moon, unfortunately.
33 */
34 seed = time( (time_t *) 0) ^ (long) getuid() ^ (long) getpid();
35
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);
39 }
40 fseek(cookie, magic((unsigned long) cookie_stat.st_size), 0); /* m ove bu magic... */
41
42 c2 = c3 = '\n';
43 while (((c1 = getc(cookie)) != EOF) && ((c1 != '%') || (c2 != '%') || (c3 != '\n'))) {
44 c3 = c2;
45 c2 = c1;
46 }
47
48 if (c1 == EOF) {
49 printf("\nSomething unexpected has happened.\n - %s", argv[0]);
50 exit (-1);
51 }
52
53 c2 = c3 = '\n';
54 while (((c1 = getc(cookie)) != '%') || (c2 != '%') || (c3 != '\n')) {
55 if (c1 == EOF) {
56 rewind(cookie);
57 continue;
58 }
59 putc(c2, stdout);
60 c3 = c2;
61 c2 = c1;
62 }
63 putc('\n', stdout);
64 fclose(cookie);
65 return (0);
66}
67
68/* magic - please study carefull: there is more than meets the eye */
69unsigned long magic(range)
70unsigned long range;
71{
72
73 seed = 9065531L * (seed % 9065533L) - 2 * (seed / 9065531L) + 1L;
74 return (seed % range);
75}
Note: See TracBrowser for help on using the repository browser.