Annotation of coherent/d/bin/from.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * from - generate a list of numbers
                      3:  * usage - from number to number [ by number ]
                      4:  */
                      5: #include <stdio.h>
                      6: 
                      7: #define sign(x)        (x<0?-1:1)      /* returns sign of number */
                      8: 
                      9: char usage[] = "Usage: from number to number [by number]\n";
                     10: char signmsg[] = "Increment has wrong sign\n";
                     11: 
                     12: main(argc, argv)
                     13: char *argv[];
                     14: {
                     15:        int i;
                     16:        int start, end, incr;
                     17: 
                     18:        switch ( argc ) {
                     19: 
                     20:        case 4:
                     21:                incr = 1;
                     22:                break;
                     23: 
                     24:        case 6:
                     25:                if ( strcmp(argv[4], "by") )
                     26:                        error(usage);
                     27:                else
                     28:                        if ( (incr = numeric(argv[5])) == 0 )
                     29:                                error("Increment must be non-zero\n");
                     30:                break;
                     31: 
                     32:        default:
                     33:                error(usage);
                     34:                break;
                     35:        }
                     36:        if ( strcmp(argv[2], "to") )
                     37:                error(usage);
                     38:        start = numeric(argv[1]);
                     39:        end = numeric(argv[3]);
                     40:        if ( sign(end-start) * sign(incr) < 0 )
                     41:                error(signmsg);
                     42: 
                     43:        if ( incr > 0 )
                     44:                for ( i = start; i <= end; i += incr )
                     45:                        printf("%d\n", i);
                     46:        else
                     47:                for ( i = start; i >= end; i += incr )
                     48:                        printf("%d\n", i);
                     49:        exit(0);
                     50: }
                     51: 
                     52: /*
                     53:  * Return the value of a numeric arg;
                     54:  * otherwise, call usage().
                     55:  */
                     56: numeric(s)
                     57: register char *s;
                     58: {
                     59:        register int n;
                     60: 
                     61:        n = atoi(s);
                     62:        if ( *s == '-' )
                     63:                s++;
                     64:        for ( ; *s; s++)
                     65:                if (*s<'0' || *s>'9')
                     66:                        error(usage);
                     67:        return(n);
                     68: }
                     69: 
                     70: error(x)
                     71: {
                     72:        fprintf(stderr, "%r", &x);
                     73:        exit(1);
                     74: }

unix.superglobalmegacorp.com

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