Annotation of researchv10no/games/fortune.c, revision 1.1

1.1     ! root        1: /*% cc -go # %
        !             2:  * too-complex fortune program.
        !             3:  * Tom Duff at Lucasfilm, Ltd. 81.11.23
        !             4:  */
        !             5: #include <stdio.h>
        !             6: #include <sys/types.h>
        !             7: #include <sys/stat.h>
        !             8: char line[512], choice[512];
        !             9: char index[]="/usr/games/lib/fortunes.index";
        !            10: char fortunes[]="/usr/games/lib/fortunes";
        !            11: main(argc, argv)
        !            12: char *argv[];
        !            13: {
        !            14:        int i;
        !            15:        long offs;
        !            16:        register FILE *f, *ix;
        !            17:        int newindex=0, oldindex=0;
        !            18:        struct stat fbuf, ixbuf;
        !            19:        if(argc==1){
        !            20:                if(stat(fortunes, &fbuf)>=0
        !            21:                && (stat(index, &ixbuf)<0 || fbuf.st_mtime>=ixbuf.st_mtime)
        !            22:                && (ix=fopen(index, "w"))!=NULL)
        !            23:                        newindex++;
        !            24:                else if((ix=fopen(index, "r"))==NULL){
        !            25:                        if((ix=fopen(index, "w"))!=NULL)
        !            26:                                newindex++;
        !            27:                }
        !            28:                else
        !            29:                        oldindex++;
        !            30:        }
        !            31:        if((f=fopen(argc>1?argv[1]:fortunes, "r"))==NULL){
        !            32:                printf("Misfortune!\n");
        !            33:                exit(1);
        !            34:        }
        !            35:        srand((int)(time((long)0)+getpid()*getpid()));
        !            36:        if(oldindex){
        !            37:                fseek(ix, 0L, 2);
        !            38:                fseek(ix, nrand(ftell(ix)/sizeof(offs))*sizeof(offs), 0);
        !            39:                fread(&offs, sizeof(offs), 1, ix);
        !            40:                fseek(f, offs, 0);
        !            41:                fgets(choice, 512, f);
        !            42:        }
        !            43:        else{
        !            44:                for(i=1;;i++){
        !            45:                        if(newindex)
        !            46:                                offs=ftell(f);
        !            47:                        fgets(line, 512, f);
        !            48:                        if(feof(f))
        !            49:                                break;
        !            50:                        if(newindex)
        !            51:                                fwrite(&offs, sizeof(offs), 1, ix);
        !            52:                        if(nrand(i)==0)
        !            53:                                strcpy(choice, line);
        !            54:                }
        !            55:        }
        !            56:        printf("%s", choice);
        !            57:        return 0;
        !            58: }

unix.superglobalmegacorp.com

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