Annotation of researchv10no/cmd/seq.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: 
        !             3: double min=1.0;
        !             4: double max=0.0;
        !             5: double incr=1.0;
        !             6: int    const=0;
        !             7: int    nsteps;
        !             8: char   *format;
        !             9: 
        !            10: extern double atof();
        !            11: extern char *strchr();
        !            12: 
        !            13: main(argc, argv)
        !            14:        char *argv[];
        !            15: {
        !            16:        register i;
        !            17:        char buf[BUFSIZ], ffmt[BUFSIZ];
        !            18: 
        !            19:        while(argc>1 && argv[1][0]=='-'){
        !            20:                switch(argv[1][1]){
        !            21:                case 'w':
        !            22:                        const++;
        !            23:                        break;
        !            24:                case 'f':
        !            25:                        format= &argv[1][2];
        !            26:                        if(format[strlen(format)-1] != '\n')
        !            27:                                sprintf(format = ffmt, "%s\n", &argv[1][2]);
        !            28:                        break;
        !            29:                default:
        !            30:                        goto out;
        !            31:                }
        !            32:                --argc;
        !            33:                argv++;
        !            34:        }
        !            35:     out:
        !            36:        if(argc<2 || argc>4)
        !            37:                usage();
        !            38:        max=atof(argv[argc-1]);
        !            39:        if(argc>2)
        !            40:                min=atof(argv[1]);
        !            41:        if(argc>3)
        !            42:                incr=atof(argv[2]);
        !            43:        if(incr==0){
        !            44:                fprintf(stderr, "seq: zero increment\n");
        !            45:                exit(1);
        !            46:        }
        !            47:        nsteps=(max-min)/incr+.5;
        !            48:        if(!format)
        !            49:                buildfmt();
        !            50:        for(i=0; i<=nsteps; i++)
        !            51:                printf(format, min+i*incr);
        !            52:        return 0;
        !            53: }
        !            54: usage(){
        !            55:        fprintf(stderr, "usage: seq [-fformat] [-w] [first [incr]] last\n");
        !            56:        exit(1);
        !            57: }
        !            58: buildfmt()
        !            59: {
        !            60:        register i;
        !            61:        register char *dp;
        !            62:        int w, p, maxw=0, maxp=0;
        !            63:        static char fmt[16];
        !            64:        char buf[32];
        !            65: 
        !            66:        format = "%g\n";
        !            67:        if(!const)
        !            68:                return;
        !            69:        for(i=0; i<=nsteps; i++) {
        !            70:                sprintf(buf,"%g",min+i*incr);
        !            71:                if(strchr(buf,'e')!=0)
        !            72:                        return;
        !            73:                dp = strchr(buf,'.');
        !            74:                w = dp==0? strlen(buf): dp-buf;
        !            75:                p = dp==0? 0: strlen(strchr(buf,'.')+1);
        !            76:                if(w>maxw) maxw = w;
        !            77:                if(p>maxp) maxp = p;
        !            78:        }
        !            79:        if(maxp>0) maxw += maxp+1;
        !            80:        sprintf(fmt,"%%0%d.%df\n",maxw,maxp);
        !            81:        format = fmt;
        !            82: }

unix.superglobalmegacorp.com

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