152142Sbostic /*- 2*61948Sbostic * Copyright (c) 1991, 1993 3*61948Sbostic * The Regents of the University of California. All rights reserved. 432749Sbostic * 542723Sbostic * %sccs.include.redist.c% 621551Sdist */ 721551Sdist 821551Sdist #ifndef lint 9*61948Sbostic static char copyright[] = 10*61948Sbostic "@(#) Copyright (c) 1991, 1993\n\ 11*61948Sbostic The Regents of the University of California. All rights reserved.\n"; 1232749Sbostic #endif /* not lint */ 1321551Sdist 1421551Sdist #ifndef lint 15*61948Sbostic static char sccsid[] = "@(#)colrm.c 8.1 (Berkeley) 06/06/93"; 1632749Sbostic #endif /* not lint */ 1721551Sdist 1852142Sbostic #include <sys/types.h> 1952142Sbostic #include <limits.h> 2052142Sbostic #include <errno.h> 21986Sbill #include <stdio.h> 2252142Sbostic #include <stdlib.h> 2352142Sbostic #include <string.h> 24986Sbill 2552142Sbostic #define TAB 8 26986Sbill 2752142Sbostic void err __P((const char *, ...)); 2852142Sbostic void check __P((FILE *)); 2952142Sbostic void usage __P((void)); 3052142Sbostic 3152142Sbostic int 3252142Sbostic main(argc, argv) 3352142Sbostic int argc; 3452142Sbostic char *argv[]; 35986Sbill { 3652142Sbostic register u_long column, start, stop; 3752142Sbostic register int ch; 3852142Sbostic char *p; 39986Sbill 4052142Sbostic while ((ch = getopt(argc, argv, "")) != EOF) 4152142Sbostic switch(ch) { 4252142Sbostic case '?': 4352142Sbostic default: 4452142Sbostic usage(); 4552142Sbostic } 4652142Sbostic argc -= optind; 4752142Sbostic argv += optind; 48986Sbill 4952142Sbostic start = stop = 0; 5052142Sbostic switch(argc) { 5152142Sbostic case 2: 5252142Sbostic stop = strtol(argv[1], &p, 10); 5352142Sbostic if (stop <= 0 || *p) 5452142Sbostic err("illegal column -- %s", argv[1]); 5552142Sbostic /* FALLTHROUGH */ 5652142Sbostic case 1: 5752142Sbostic start = strtol(argv[0], &p, 10); 5852142Sbostic if (start <= 0 || *p) 5952142Sbostic err("illegal column -- %s", argv[0]); 6052142Sbostic break; 6152142Sbostic case 0: 6252142Sbostic break; 6352142Sbostic default: 6452142Sbostic usage(); 65986Sbill } 66986Sbill 6752142Sbostic if (stop && start > stop) 6852142Sbostic err("illegal start and stop columns"); 6952142Sbostic 7052142Sbostic for (column = 0;;) { 7152142Sbostic switch (ch = getchar()) { 7252142Sbostic case EOF: 7352142Sbostic check(stdin); 7452142Sbostic break; 7552142Sbostic case '\b': 7652142Sbostic if (column) 7752142Sbostic --column; 7852142Sbostic break; 7952142Sbostic case '\n': 8052142Sbostic column = 0; 8152142Sbostic break; 8252142Sbostic case '\t': 8352142Sbostic column = (column + TAB) & ~(TAB - 1); 8452142Sbostic break; 8552142Sbostic default: 8652142Sbostic ++column; 8752142Sbostic break; 88986Sbill } 89986Sbill 9052142Sbostic if ((!start || column < start || stop && column > stop) && 9152142Sbostic putchar(ch) == EOF) 9252142Sbostic check(stdout); 93986Sbill } 94986Sbill } 95986Sbill 9652142Sbostic void 9752142Sbostic check(stream) 9852142Sbostic FILE *stream; 99986Sbill { 10052142Sbostic if (feof(stream)) 10152142Sbostic exit(0); 10252142Sbostic if (ferror(stream)) 10352142Sbostic err("%s: %s", 10452142Sbostic stream == stdin ? "stdin" : "stdout", strerror(errno)); 10552142Sbostic } 106986Sbill 10752142Sbostic void 10852142Sbostic usage() 10952142Sbostic { 11052142Sbostic (void)fprintf(stderr, "usage: colrm [start [stop]]\n"); 11152142Sbostic exit(1); 112986Sbill } 11352142Sbostic 11452142Sbostic #if __STDC__ 11552142Sbostic #include <stdarg.h> 11652142Sbostic #else 11752142Sbostic #include <varargs.h> 11852142Sbostic #endif 11952142Sbostic 12052142Sbostic void 12152142Sbostic #if __STDC__ 12252142Sbostic err(const char *fmt, ...) 12352142Sbostic #else 12452142Sbostic err(fmt, va_alist) 12552142Sbostic char *fmt; 12652142Sbostic va_dcl 12752142Sbostic #endif 12852142Sbostic { 12952142Sbostic va_list ap; 13052142Sbostic #if __STDC__ 13152142Sbostic va_start(ap, fmt); 13252142Sbostic #else 13352142Sbostic va_start(ap); 13452142Sbostic #endif 13552142Sbostic (void)fprintf(stderr, "colrm: "); 13652142Sbostic (void)vfprintf(stderr, fmt, ap); 13752142Sbostic va_end(ap); 13852142Sbostic (void)fprintf(stderr, "\n"); 13952142Sbostic exit(1); 14052142Sbostic /* NOTREACHED */ 14152142Sbostic } 142