|
|
1.1 ! root 1: /* ! 2: * wrap.c - fold over lines > 80 characters to avoid truncation during printout ! 3: * ! 4: * usage: wrap [-ttablen] [-wwraplen] < infile > outfile ! 5: * tab length defaults to 8 ! 6: * wrap column defaults to 80 ! 7: */ ! 8: #include <stdio.h> ! 9: ! 10: #define WRAPLEN 80 ! 11: #define TABLEN 8 ! 12: ! 13: main(argc, argv) ! 14: int argc; ! 15: char * argv[]; ! 16: { ! 17: /* ! 18: * get tab length from command line or default to TABLEN ! 19: * get wrap column from command line or default to WRAPLEN ! 20: */ ! 21: ! 22: int chars_this_line = 0; ! 23: int ch, wl = WRAPLEN, tl = TABLEN; ! 24: int argnum; ! 25: ! 26: for (argnum = 1; argnum < argc; argnum++) { ! 27: if (argv[argnum][0] == '-') ! 28: switch (argv[argnum][1]) { ! 29: case 't': ! 30: tl = atoi(argv[argnum]+2); ! 31: if (tl <= 0 || tl >= 256) ! 32: tl = TABLEN; ! 33: break; ! 34: case 'w': ! 35: wl = atoi(argv[argnum]+2); ! 36: if (wl <= 0 || wl >= 256) ! 37: wl = WRAPLEN; ! 38: break; ! 39: } ! 40: } ! 41: ! 42: /* ! 43: * while (not at end of stdin) ! 44: * get next character ! 45: * if it's newline ! 46: * reset chars_this_line to zero ! 47: * else if it's backspace ! 48: * if chars_this_line is > 0 ! 49: * decrement chars_this_line ! 50: * else if it's tab ! 51: * insert the right number of spaces ! 52: * else ! 53: * increment chars_this_line ! 54: * if chars_this_line > WRAPLEN ! 55: * write newline to stdout ! 56: * decrement chars_this_line by WRAPLEN ! 57: * write character to stdout ! 58: */ ! 59: while ((ch = getchar()) != EOF) { ! 60: if (ch == '\n') { ! 61: chars_this_line = 0; ! 62: putchar(ch); ! 63: } else if (ch == '\b') { ! 64: if (chars_this_line) ! 65: chars_this_line--; ! 66: putchar(ch); ! 67: } else if (ch == '\t') { ! 68: do { ! 69: chars_this_line++; ! 70: if (chars_this_line > wl) { ! 71: putchar('\n'); ! 72: chars_this_line -= wl; ! 73: } ! 74: putchar(' '); ! 75: } while (chars_this_line % tl); ! 76: } else { ! 77: chars_this_line++; ! 78: if (chars_this_line > wl) { ! 79: putchar('\n'); ! 80: chars_this_line -= wl; ! 81: } ! 82: putchar(ch); ! 83: } ! 84: } ! 85: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.