home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / OS2MNX1.ZIP / FORTUNE.C < prev    next >
Text File  |  1989-12-24  |  3KB  |  110 lines

  1. /*
  2.  *  fortune  -  hand out Chinese fortune cookies
  3.  */
  4. /* $Header: D:/RCS/RCS/fortune.c 1.1 89/12/24 21:43:01 RCA Exp $
  5.  * $Log:    fortune.c $
  6.  * Revision 1.1  89/12/24  21:43:01  RCA
  7.  * Initial revision
  8.  * 
  9.  */
  10.  
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <stdio.h>
  14. #include <time.h>
  15.  
  16. /* RCA no, I don't think so...
  17.  * #define COOKIEJAR "/usr/games/lib/fortunes.dat"
  18.  */
  19. #define COOKIEJAR "fortunes.dat"
  20.  
  21. static char *Copyright = "\0fortune v1.1 Copyright (c) 1988 Bert Reuling";
  22.  
  23. long seed;
  24.  
  25. main(argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29.    int c1, c2, c3;
  30.    long magic();
  31.    struct stat cookie_stat;
  32.    FILE *cookie, *out, *fopen(), *popen();
  33.  
  34.    if ((cookie = fopen(COOKIEJAR, "r")) == NULL) {
  35.       fprintf(stderr, "%s:\nSomeone closed the cookie jar!\n", argv[0]);
  36.       exit(-1);
  37.    }
  38.  
  39.    /*
  40.     *  create seed from : date, time, user-id and process-id
  41.     *  we can't get the position of the moon, unfortunately.
  42.     *  Note that super cookies are not affected by chance...
  43.     */
  44. /* RCA revise for OS/2
  45.  *  seed = time(0L) * (long) getuid() * (long) getpid();
  46.  */
  47.     seed = time(0L) * (long) getpid(); 
  48.  
  49.    if (stat(COOKIEJAR, &cookie_stat) != 0) {
  50.       fprintf(stderr, "%s:\nCannot stat cookie jar\n", argv[0]);
  51.       exit(-1);
  52.    }
  53.  
  54.    fseek(cookie, magic((long) cookie_stat.st_size), 0); /* move by magic... */
  55.  
  56.    c2 = c3 = '\n';
  57.    while (((c1 = getc(cookie)) != EOF) && ((c1 != '%') || (c2 != '%') || (c3 != '\n'))) {
  58.       c3 = c2;
  59.       c2 = c1;
  60.    }
  61.  
  62.    if (c1 == EOF) {
  63.       fprintf(stderr, "%s:\n", argv[0]);
  64.       fprintf(stderr, "The cookie jar does not have a bottom!\n");
  65.       fprintf(stderr, "All cookies have fallen out...\n");
  66.       exit(-1);
  67.    }
  68.  
  69. #ifdef FORMATTER
  70.    if ((out = popen(FORMATTER, "w")) == NULL) {
  71.       fprintf(stderr, "%s:\nIt furthers one to see a plumber!\n", argv[0]);
  72.       exit(-1);
  73.    }
  74. #else
  75.    out = stdout;
  76. #endif
  77.  
  78.    c2 = c3 = '\n';
  79.    while (((c1 = getc(cookie)) != '%') || (c2 != '%') || (c3 != '\n')) {
  80.       if (c1 == EOF) {
  81.          rewind(cookie);
  82.          continue;
  83.       }
  84.       putc(c2, out);
  85.       c3 = c2;
  86.       c2 = c1;
  87.    }
  88.    putc('\n', out);
  89.    fclose(cookie);
  90. #ifdef FORMATTER
  91.    pclose(out);
  92. #endif
  93.    exit(0);
  94. }
  95.  
  96. /*
  97.  *  magic  -  please study carefull: there is more than meets the eye
  98.  */
  99. long magic(range)
  100. long range;
  101. {
  102.    int i;
  103.  
  104.    for (i = 0; i < 1234; i++)
  105.       seed = 883L * (seed % 881L) - 2 * (seed / 883L) + 1L;
  106.    return ((long)((int)(seed & 0x7fffL) * range / 0x7fffL));
  107. }
  108.  
  109. 
  110.