[9] | 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 |
|
---|
| 12 | static char *Copyright = "\0Copyright (c) 1990 Bert Reuling";
|
---|
| 13 | static unsigned long seed;
|
---|
| 14 |
|
---|
| 15 | _PROTOTYPE(int main, (int argc, char **argv));
|
---|
| 16 | _PROTOTYPE(unsigned long magic, (unsigned long range));
|
---|
| 17 |
|
---|
| 18 | int main(argc, argv)
|
---|
| 19 | int argc;
|
---|
| 20 | char *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 */
|
---|
| 69 | unsigned long magic(range)
|
---|
| 70 | unsigned long range;
|
---|
| 71 | {
|
---|
| 72 |
|
---|
| 73 | seed = 9065531L * (seed % 9065533L) - 2 * (seed / 9065531L) + 1L;
|
---|
| 74 | return (seed % range);
|
---|
| 75 | }
|
---|