1 /* fortune - hand out Chinese fortune cookies Author: Bert Reuling */
10 #define COOKIEJAR "/usr/lib/fortune.dat"
12 static char *Copyright
= "\0Copyright (c) 1990 Bert Reuling";
13 static unsigned long seed
;
15 _PROTOTYPE(int main
, (int argc
, char **argv
));
16 _PROTOTYPE(unsigned long magic
, (unsigned long range
));
23 struct stat cookie_stat
;
26 if ((cookie
= fopen(COOKIEJAR
, "r")) == NULL
) {
27 printf("\nSome things better stay closed.\n - %s\n", argv
[0]);
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]);
40 fseek(cookie
, magic((unsigned long) cookie_stat
.st_size
), 0); /* m ove bu magic... */
43 while (((c1
= getc(cookie
)) != EOF
) && ((c1
!= '%') || (c2
!= '%') || (c3
!= '\n'))) {
49 printf("\nSomething unexpected has happened.\n - %s", argv
[0]);
54 while (((c1
= getc(cookie
)) != '%') || (c2
!= '%') || (c3
!= '\n')) {
68 /* magic - please study carefull: there is more than meets the eye */
69 unsigned long magic(range
)
73 seed
= 9065531L * (seed
% 9065533L) - 2 * (seed
/ 9065531L) + 1L;
74 return (seed
% range
);