# include /* This program, range, accepts 2 or 3 command line arguments and checks that they are integers. Range prints a range of numbers one per line on the standard output, starting with the first argument, incrementing (or decrementing) with the third argument until the next number printed would be greater (or lesser) than the second argument. If the third argument is missing, then the increment is set to 1 (similarly when the third argument is specified as zero). The absolute of the third argument is used as the increment, whereas the sign of the other arguments is never changed. Author: Brett Jordan, CiTR Date created: 20 March 1991 */ main(argc, argv) int argc; char *argv[]; { int err, i, stnum, endnum, incr, *argi[3]; err = 0; argi[0] = &stnum; /* Make argis point to the final resting */ argi[1] = &endnum; /* place of the command line arguments */ argi[2] = &incr; /* if incorrect number of arguments ... */ if (argc < 3 || argc > 4) err = 1; else { /* argc is the count of all words on the command line, whereas argc-1 is the number of arguments, therefore for all arguments ... */ for (i=0; i<(argc-1); i++) /* increment argv to point to next argument, initially pointing to 'range', and pass it by value, pass argi element so that sscanf can convert text into integer and place the integer arguments in their final resting places. If sscanf returns 0 then there was an error. */ if (sscanf(*++argv, "%d", argi[i]) == 0) { err = 1; break; } } /* if an error occurred, print usage info */ if (err != 0) { usage(); return(1); } else { /* if no increment specified, or set to 0, make it 1 */ if (i==2 || incr==0) incr = 1; /* ensure that increment is positive */ if (incr < 0) incr = 0 - incr; /* if decrementing... */ if (stnum > endnum) { for (i=stnum; i>=endnum; i-=incr) printf("%d\n", i); } /* if incrementing... */ else { for (i = stnum; i <= endnum; i+=incr) printf("%d\n", i); } return(0); } } usage() { printf("usage: range start end [increment]\n"); printf("\n"); printf("start, end and increment must be integers.\n"); }