Annotation of coherent/d/usr/games/fortune.c, revision 1.1.1.1

1.1       root        1: /* 
                      2:  * Display a random line on a file. Suitable for automated
                      3:  * fortune cookies. Multi line fortunes such as poems should
                      4:  * have their lines separated by @'s.
                      5:  */
                      6: #include <sys/timeb.h>
                      7: #include <stdio.h>
                      8: #define RAND_MAX 32767
                      9: 
                     10: char *fortfile = "/usr/games/lib/fortunes";
                     11: 
                     12: void
                     13: main(argc, argv)
                     14: char *argv[];
                     15: {
                     16:        FILE  *ifp;
                     17:        double  randomAdj;
                     18:        int c;
                     19:        struct timeb tb;
                     20: 
                     21:        if(argc > 1)
                     22:                fortfile = argv[1];
                     23: 
                     24:        if(NULL == (ifp = fopen(fortfile, "r"))) {
                     25:                printf("Cannot open %s\n", fortfile);
                     26:                exit(1);
                     27:        }
                     28: 
                     29:        fseek(ifp, 0L, 2);
                     30:        randomAdj = (double)ftell(ifp) / ((double)RAND_MAX);
                     31: 
                     32:        ftime(&tb);
                     33:        srand((unsigned int)(tb.time ^ tb.millitm));
                     34:        /* a user running this several times in quick succession will
                     35:         * be dissapointed if he keeps getting the same result.
                     36:         * Running rand() 100 times distributes the low order bit
                     37:         * changes in the seed to the high half returned by rand().
                     38:         */
                     39:        for(c = 0; c < 100; c++)
                     40:                rand();
                     41: 
                     42:        fseek(ifp, (long)(randomAdj * (double)rand()), 0);
                     43:        while('\n' != (c = fgetc(ifp)) && EOF != c)
                     44:                ;
                     45: 
                     46:        if(c == EOF) {
                     47:                printf("File does not end with nl\n");
                     48:                exit(1);
                     49:        }
                     50:                
                     51:        while('\n' != (c = fgetc(ifp))) {
                     52:                if(EOF == c) {
                     53:                        fseek(ifp, 0L, 0);
                     54:                        continue;
                     55:                }
                     56:                if('@' == c) /* to display multi line fortunes */
                     57:                        c = '\n';
                     58:                putchar(c);
                     59:        }
                     60:        putchar('\n');
                     61: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.