1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)colrm.c 5.5 (Berkeley) 01/08/92"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <limits.h> 20 #include <errno.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 25 #define TAB 8 26 27 void err __P((const char *, ...)); 28 void check __P((FILE *)); 29 void usage __P((void)); 30 31 int 32 main(argc, argv) 33 int argc; 34 char *argv[]; 35 { 36 register u_long column, start, stop; 37 register int ch; 38 char *p; 39 40 while ((ch = getopt(argc, argv, "")) != EOF) 41 switch(ch) { 42 case '?': 43 default: 44 usage(); 45 } 46 argc -= optind; 47 argv += optind; 48 49 start = stop = 0; 50 switch(argc) { 51 case 2: 52 stop = strtol(argv[1], &p, 10); 53 if (stop <= 0 || *p) 54 err("illegal column -- %s", argv[1]); 55 /* FALLTHROUGH */ 56 case 1: 57 start = strtol(argv[0], &p, 10); 58 if (start <= 0 || *p) 59 err("illegal column -- %s", argv[0]); 60 break; 61 case 0: 62 break; 63 default: 64 usage(); 65 } 66 67 if (stop && start > stop) 68 err("illegal start and stop columns"); 69 70 for (column = 0;;) { 71 switch (ch = getchar()) { 72 case EOF: 73 check(stdin); 74 break; 75 case '\b': 76 if (column) 77 --column; 78 break; 79 case '\n': 80 column = 0; 81 break; 82 case '\t': 83 column = (column + TAB) & ~(TAB - 1); 84 break; 85 default: 86 ++column; 87 break; 88 } 89 90 if ((!start || column < start || stop && column > stop) && 91 putchar(ch) == EOF) 92 check(stdout); 93 } 94 } 95 96 void 97 check(stream) 98 FILE *stream; 99 { 100 if (feof(stream)) 101 exit(0); 102 if (ferror(stream)) 103 err("%s: %s", 104 stream == stdin ? "stdin" : "stdout", strerror(errno)); 105 } 106 107 void 108 usage() 109 { 110 (void)fprintf(stderr, "usage: colrm [start [stop]]\n"); 111 exit(1); 112 } 113 114 #if __STDC__ 115 #include <stdarg.h> 116 #else 117 #include <varargs.h> 118 #endif 119 120 void 121 #if __STDC__ 122 err(const char *fmt, ...) 123 #else 124 err(fmt, va_alist) 125 char *fmt; 126 va_dcl 127 #endif 128 { 129 va_list ap; 130 #if __STDC__ 131 va_start(ap, fmt); 132 #else 133 va_start(ap); 134 #endif 135 (void)fprintf(stderr, "colrm: "); 136 (void)vfprintf(stderr, fmt, ap); 137 va_end(ap); 138 (void)fprintf(stderr, "\n"); 139 exit(1); 140 /* NOTREACHED */ 141 } 142