Annotation of 42BSD/games/fortune/fortune.c, revision 1.1.1.1

1.1       root        1: char   *sccsid = "@(#)fortune.c        1.4 (Berkeley) 2/11/83";
                      2: 
                      3: # include      <sys/types.h>
                      4: # include      <stdio.h>
                      5: # include      "strfile.h"
                      6: 
                      7: # define       MINW    6               /* minimum wait if desired      */
                      8: # define       CPERS   20              /* # of chars for each sec      */
                      9: # define       SLEN    160             /* # of chars in short fortune  */
                     10: 
                     11: # define       reg     register
                     12: 
                     13: short  wflag           = 0,            /* wait desired after fortune   */
                     14:        sflag           = 0,            /* short fortune desired        */
                     15:        lflag           = 0,            /* long fortune desired         */
                     16:        oflag           = 0,            /* offensive fortunes only      */
                     17:        aflag           = 0;            /* any fortune allowed          */
                     18: 
                     19: char   fortfile[100]   = FORTFILE,     /* fortune database             */
                     20:        *usage[]        = {
                     21:        "usage:  fortune [ - ] [ -wsloa ] [ file ]",
                     22:        "       - - give this summary of usage",
                     23:        "       w - have program wait after printing message in order",
                     24:        "           to give time to read",
                     25:        "       s - short fortune only",
                     26:        "       l - long fortune only",
                     27:        "       o - offensive fortunes only",
                     28:        "       a - any fortune",
                     29:        "               Mail suggested fortunes to \"fortune\""
                     30:        };
                     31: 
                     32: long   seekpts[2];                     /* seek pointers to fortunes    */
                     33: 
                     34: time_t seed;
                     35: 
                     36: STRFILE        tbl;                            /* input table                  */
                     37: 
                     38: time_t time();
                     39: 
                     40: main(ac, av)
                     41: int    ac;
                     42: char   *av[]; {
                     43: 
                     44:        reg char        c;
                     45:        reg int         nchar = 0;
                     46:        reg FILE        *inf;
                     47:        reg int         numforts,       /* number of fortunes           */
                     48:                        fortune;        /* fortune number               */
                     49:        reg time_t      tm;
                     50: 
                     51:        getargs(ac, av);
                     52:        if ((inf = fopen(fortfile, "r")) == NULL) {
                     53:                perror(fortfile);
                     54:                exit(-1);
                     55:        }
                     56:        fread(&tbl, (sizeof tbl), 1, inf);
                     57:        numforts = tbl.str_numstr - 1;   /* always a null string at the end */
                     58:        if (tbl.str_longlen < SLEN && lflag) {
                     59:                puts("Sorry, no long strings in this file");
                     60:                exit(0);
                     61:        }
                     62:        if (tbl.str_shortlen > SLEN && sflag) {
                     63:                puts("Sorry, no short strings in this file");
                     64:                exit(0);
                     65:        }
                     66:        if (oflag)
                     67:                numforts -= tbl.str_delims[0];
                     68:        else if (!aflag)
                     69:                numforts = tbl.str_delims[0];
                     70:        tm = time(NULL);
                     71:        seed = tm + getpid();
                     72:        getfort(numforts, inf);
                     73:        if (sflag)
                     74:                while (seekpts[1] - seekpts[0] >= SLEN)
                     75:                        getfort(numforts, inf);
                     76:        else if (lflag)
                     77:                while (seekpts[1] - seekpts[0] < SLEN)
                     78:                        getfort(numforts, inf);
                     79:        fseek(inf, seekpts[0], 0);
                     80:        while (c = getc(inf)) {
                     81:                nchar++;
                     82:                putchar(c);
                     83:        }
                     84:        fflush(stdout);
                     85:        if (wflag)
                     86:                sleep(max((int) nchar/CPERS, MINW));
                     87: }
                     88: 
                     89: /*
                     90:  *     This routine evaluates the arguments on the command line
                     91:  */
                     92: getargs(ac, av)
                     93: int            ac;
                     94: reg char       *av[]; {
                     95: 
                     96:        reg short       bad = 0;
                     97:        reg int         i, j;
                     98: 
                     99:        for (i = 1; i < ac; i++)  {
                    100:                if (av[i][0] != '-')
                    101:                        strcpy(fortfile, av[i]);
                    102:                else
                    103:                        switch (av[i][1]) {
                    104:                          case '\0':    /* give usage                   */
                    105:                                for (j = 0; j < sizeof usage / sizeof (char *); j++)
                    106:                                        puts(usage[j]);
                    107:                                exit(0);
                    108:                          case 'w':     /* give time to read            */
                    109:                                wflag++;
                    110:                                break;
                    111:                          case 's':     /* short ones only              */
                    112:                                sflag++;
                    113:                                break;
                    114:                          case 'l':     /* long ones only               */
                    115:                                lflag++;
                    116:                                break;
                    117:                          case 'o':     /* offensive ones only          */
                    118:                                oflag++;
                    119:                                break;
                    120:                          case 'a':     /* any fortune                  */
                    121:                                aflag++;
                    122:                                break;
                    123:                          default:
                    124:                                printf("unknown flag: '%c'\n", av[1][1]);
                    125:                                bad++;
                    126:                                break;
                    127:                        }
                    128:        }
                    129:        if (bad) {
                    130:                printf("use \"%s -\" to get usage\n", av[0]);
                    131:                exit(-1);
                    132:        }
                    133: }
                    134: 
                    135: getfort(numforts, inf)
                    136: reg int                numforts;
                    137: reg FILE       *inf; {
                    138: 
                    139:        reg int         fortune;
                    140: 
                    141:        fortune = rnd(numforts);
                    142:        if (oflag && !aflag)
                    143:                fortune += tbl.str_delims[0];
                    144:        fseek(inf, (long)(sizeof seekpts[0]) * fortune + sizeof tbl, 0);
                    145:        fread(seekpts, (sizeof seekpts[0]), 2, inf);
                    146: }
                    147: 
                    148: max(i, j)
                    149: reg int        i, j; {
                    150: 
                    151:        return (i >= j ? i : j);
                    152: }
                    153: 
                    154: rnd(num)
                    155: int    num; {
                    156: 
                    157:        return ((seed = seed*11109+13849) & 0xffff) % num;
                    158: }

unix.superglobalmegacorp.com

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