xref: /netbsd-src/usr.bin/colrm/colrm.c (revision 1f81257a3acf251c44e90d68c8525d394ed2a0c0)
1*1f81257aSjoerg /*	$NetBSD: colrm.c,v 1.9 2011/08/30 21:35:09 joerg Exp $	*/
2af74bd6cSglass 
3af74bd6cSglass /*-
4af74bd6cSglass  * Copyright (c) 1991, 1993
5af74bd6cSglass  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32f6d6033fSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3498e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
3598e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
39af74bd6cSglass #if 0
403e4b73c5Sjtc static char sccsid[] = "@(#)colrm.c	8.2 (Berkeley) 5/4/95";
41af74bd6cSglass #endif
42*1f81257aSjoerg __RCSID("$NetBSD: colrm.c,v 1.9 2011/08/30 21:35:09 joerg Exp $");
4361f28255Scgd #endif /* not lint */
4461f28255Scgd 
45af74bd6cSglass #include <sys/types.h>
46af74bd6cSglass 
47af74bd6cSglass #include <err.h>
48af74bd6cSglass #include <errno.h>
49af74bd6cSglass #include <limits.h>
5061f28255Scgd #include <stdio.h>
51af74bd6cSglass #include <stdlib.h>
52af74bd6cSglass #include <string.h>
533e4b73c5Sjtc #include <unistd.h>
5461f28255Scgd 
55af74bd6cSglass #define	TAB	8
5661f28255Scgd 
57*1f81257aSjoerg static void	check(FILE *);
58*1f81257aSjoerg __dead static void	usage(void);
59af74bd6cSglass 
60af74bd6cSglass int
main(int argc,char * argv[])61abda2398Sxtraeme main(int argc, char *argv[])
6261f28255Scgd {
63f6d6033fSlukem 	u_long column, start, stop;
64f6d6033fSlukem 	int ch;
65af74bd6cSglass 	char *p;
6661f28255Scgd 
67f6d6033fSlukem 	while ((ch = getopt(argc, argv, "")) != -1)
68af74bd6cSglass 		switch(ch) {
69af74bd6cSglass 		case '?':
70af74bd6cSglass 		default:
71af74bd6cSglass 			usage();
72af74bd6cSglass 		}
73af74bd6cSglass 	argc -= optind;
74af74bd6cSglass 	argv += optind;
7561f28255Scgd 
76af74bd6cSglass 	start = stop = 0;
77af74bd6cSglass 	switch(argc) {
78af74bd6cSglass 	case 2:
79af74bd6cSglass 		stop = strtol(argv[1], &p, 10);
80af74bd6cSglass 		if (stop <= 0 || *p)
81af74bd6cSglass 			errx(1, "illegal column -- %s", argv[1]);
82af74bd6cSglass 		/* FALLTHROUGH */
83af74bd6cSglass 	case 1:
84af74bd6cSglass 		start = strtol(argv[0], &p, 10);
85af74bd6cSglass 		if (start <= 0 || *p)
86af74bd6cSglass 			errx(1, "illegal column -- %s", argv[0]);
8761f28255Scgd 		break;
88af74bd6cSglass 	case 0:
89af74bd6cSglass 		break;
90af74bd6cSglass 	default:
91af74bd6cSglass 		usage();
9261f28255Scgd 	}
9361f28255Scgd 
94af74bd6cSglass 	if (stop && start > stop)
95af74bd6cSglass 		err(1, "illegal start and stop columns");
96af74bd6cSglass 
97af74bd6cSglass 	for (column = 0;;) {
98af74bd6cSglass 		switch (ch = getchar()) {
99af74bd6cSglass 		case EOF:
100af74bd6cSglass 			check(stdin);
101af74bd6cSglass 			break;
102af74bd6cSglass 		case '\b':
103af74bd6cSglass 			if (column)
104af74bd6cSglass 				--column;
105af74bd6cSglass 			break;
106af74bd6cSglass 		case '\n':
107af74bd6cSglass 			column = 0;
108af74bd6cSglass 			break;
109af74bd6cSglass 		case '\t':
110af74bd6cSglass 			column = (column + TAB) & ~(TAB - 1);
111af74bd6cSglass 			break;
112af74bd6cSglass 		default:
113af74bd6cSglass 			++column;
114af74bd6cSglass 			break;
115af74bd6cSglass 		}
116af74bd6cSglass 
117f6d6033fSlukem 		if ((!start || column < start || (stop && column > stop)) &&
118af74bd6cSglass 		    putchar(ch) == EOF)
119af74bd6cSglass 			check(stdout);
120af74bd6cSglass 	}
121af74bd6cSglass }
122af74bd6cSglass 
123*1f81257aSjoerg static void
check(FILE * stream)124abda2398Sxtraeme check(FILE *stream)
12561f28255Scgd {
126af74bd6cSglass 	if (feof(stream))
127af74bd6cSglass 		exit(0);
128af74bd6cSglass 	if (ferror(stream))
129af74bd6cSglass 		err(1, "%s", stream == stdin ? "stdin" : "stdout");
130af74bd6cSglass }
13161f28255Scgd 
132*1f81257aSjoerg static void
usage(void)133*1f81257aSjoerg usage(void)
134af74bd6cSglass {
135af74bd6cSglass 	(void)fprintf(stderr, "usage: colrm [start [stop]]\n");
136af74bd6cSglass 	exit(1);
13761f28255Scgd }
138