/* numa.c -- take stream of decimal numbers off stdin and put the corresponding stream of ascii codes to stdout according to the formula n + base Usage: numa [base] If "base" is not specified on the command line, it defaults to BASE. */ #define BASE 0 #define MAXC 255 /* max ascii code */ #define ASCLF 10 #include #include main(argc,argv) int argc; char **argv; { int base, chx, num, onum, pchx; if( (argc != 1) && (argc !=2) ) { printf(" %s: One optional argument \"base\" -- defaults to %d.\n", argv[0], BASE); exit(20); } base = BASE; if(argc==2) { base = atoi(argv[1]); if(base < 0) { printf(" %s: Argument \"base\" cannot be negative.\n", argv[0]); exit(21); } if(base > MAXC) { printf(" %s: Argument \"base\" may not exceed %d.\n", argv[0], MAXC); exit(22); } } num = 0; pchx = '\n'; while( (chx=getc(stdin)) != EOF ) { if( (chx == ' ') || (chx == '\n') ) { if(num > 0) { onum = num + base; if(onum <= MAXC) { putchar(onum); num = 0; } else { fprintf(stderr,"\n %s: Code too high -- %d\n", argv[0], num); exit(5); } } if(chx == '\n') putchar('\n'); } else { if( (chx < '0') || (chx > '9') ) { fprintf(stderr,"\n %s: Bad input -- %c (hex %x)\n",argv[0],chx,chx); exit(5); } num = 10*num + (chx - '0'); } pchx = chx; } if ( (pchx != '\n') && (onum != ASCLF) ) putchar('\n'); exit(0); }