1*4483d188SThomas Cort /* $NetBSD: colrm.c,v 1.9 2011/08/30 21:35:09 joerg Exp $ */
2*4483d188SThomas Cort
3*4483d188SThomas Cort /*-
4*4483d188SThomas Cort * Copyright (c) 1991, 1993
5*4483d188SThomas Cort * The Regents of the University of California. All rights reserved.
6*4483d188SThomas Cort *
7*4483d188SThomas Cort * Redistribution and use in source and binary forms, with or without
8*4483d188SThomas Cort * modification, are permitted provided that the following conditions
9*4483d188SThomas Cort * are met:
10*4483d188SThomas Cort * 1. Redistributions of source code must retain the above copyright
11*4483d188SThomas Cort * notice, this list of conditions and the following disclaimer.
12*4483d188SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
13*4483d188SThomas Cort * notice, this list of conditions and the following disclaimer in the
14*4483d188SThomas Cort * documentation and/or other materials provided with the distribution.
15*4483d188SThomas Cort * 3. Neither the name of the University nor the names of its contributors
16*4483d188SThomas Cort * may be used to endorse or promote products derived from this software
17*4483d188SThomas Cort * without specific prior written permission.
18*4483d188SThomas Cort *
19*4483d188SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*4483d188SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*4483d188SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*4483d188SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*4483d188SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*4483d188SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*4483d188SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*4483d188SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*4483d188SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*4483d188SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*4483d188SThomas Cort * SUCH DAMAGE.
30*4483d188SThomas Cort */
31*4483d188SThomas Cort
32*4483d188SThomas Cort #include <sys/cdefs.h>
33*4483d188SThomas Cort #ifndef lint
34*4483d188SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
35*4483d188SThomas Cort The Regents of the University of California. All rights reserved.");
36*4483d188SThomas Cort #endif /* not lint */
37*4483d188SThomas Cort
38*4483d188SThomas Cort #ifndef lint
39*4483d188SThomas Cort #if 0
40*4483d188SThomas Cort static char sccsid[] = "@(#)colrm.c 8.2 (Berkeley) 5/4/95";
41*4483d188SThomas Cort #endif
42*4483d188SThomas Cort __RCSID("$NetBSD: colrm.c,v 1.9 2011/08/30 21:35:09 joerg Exp $");
43*4483d188SThomas Cort #endif /* not lint */
44*4483d188SThomas Cort
45*4483d188SThomas Cort #include <sys/types.h>
46*4483d188SThomas Cort
47*4483d188SThomas Cort #include <err.h>
48*4483d188SThomas Cort #include <errno.h>
49*4483d188SThomas Cort #include <limits.h>
50*4483d188SThomas Cort #include <stdio.h>
51*4483d188SThomas Cort #include <stdlib.h>
52*4483d188SThomas Cort #include <string.h>
53*4483d188SThomas Cort #include <unistd.h>
54*4483d188SThomas Cort
55*4483d188SThomas Cort #define TAB 8
56*4483d188SThomas Cort
57*4483d188SThomas Cort static void check(FILE *);
58*4483d188SThomas Cort __dead static void usage(void);
59*4483d188SThomas Cort
60*4483d188SThomas Cort int
main(int argc,char * argv[])61*4483d188SThomas Cort main(int argc, char *argv[])
62*4483d188SThomas Cort {
63*4483d188SThomas Cort u_long column, start, stop;
64*4483d188SThomas Cort int ch;
65*4483d188SThomas Cort char *p;
66*4483d188SThomas Cort
67*4483d188SThomas Cort while ((ch = getopt(argc, argv, "")) != -1)
68*4483d188SThomas Cort switch(ch) {
69*4483d188SThomas Cort case '?':
70*4483d188SThomas Cort default:
71*4483d188SThomas Cort usage();
72*4483d188SThomas Cort }
73*4483d188SThomas Cort argc -= optind;
74*4483d188SThomas Cort argv += optind;
75*4483d188SThomas Cort
76*4483d188SThomas Cort start = stop = 0;
77*4483d188SThomas Cort switch(argc) {
78*4483d188SThomas Cort case 2:
79*4483d188SThomas Cort stop = strtol(argv[1], &p, 10);
80*4483d188SThomas Cort if (stop <= 0 || *p)
81*4483d188SThomas Cort errx(1, "illegal column -- %s", argv[1]);
82*4483d188SThomas Cort /* FALLTHROUGH */
83*4483d188SThomas Cort case 1:
84*4483d188SThomas Cort start = strtol(argv[0], &p, 10);
85*4483d188SThomas Cort if (start <= 0 || *p)
86*4483d188SThomas Cort errx(1, "illegal column -- %s", argv[0]);
87*4483d188SThomas Cort break;
88*4483d188SThomas Cort case 0:
89*4483d188SThomas Cort break;
90*4483d188SThomas Cort default:
91*4483d188SThomas Cort usage();
92*4483d188SThomas Cort }
93*4483d188SThomas Cort
94*4483d188SThomas Cort if (stop && start > stop)
95*4483d188SThomas Cort err(1, "illegal start and stop columns");
96*4483d188SThomas Cort
97*4483d188SThomas Cort for (column = 0;;) {
98*4483d188SThomas Cort switch (ch = getchar()) {
99*4483d188SThomas Cort case EOF:
100*4483d188SThomas Cort check(stdin);
101*4483d188SThomas Cort break;
102*4483d188SThomas Cort case '\b':
103*4483d188SThomas Cort if (column)
104*4483d188SThomas Cort --column;
105*4483d188SThomas Cort break;
106*4483d188SThomas Cort case '\n':
107*4483d188SThomas Cort column = 0;
108*4483d188SThomas Cort break;
109*4483d188SThomas Cort case '\t':
110*4483d188SThomas Cort column = (column + TAB) & ~(TAB - 1);
111*4483d188SThomas Cort break;
112*4483d188SThomas Cort default:
113*4483d188SThomas Cort ++column;
114*4483d188SThomas Cort break;
115*4483d188SThomas Cort }
116*4483d188SThomas Cort
117*4483d188SThomas Cort if ((!start || column < start || (stop && column > stop)) &&
118*4483d188SThomas Cort putchar(ch) == EOF)
119*4483d188SThomas Cort check(stdout);
120*4483d188SThomas Cort }
121*4483d188SThomas Cort }
122*4483d188SThomas Cort
123*4483d188SThomas Cort static void
check(FILE * stream)124*4483d188SThomas Cort check(FILE *stream)
125*4483d188SThomas Cort {
126*4483d188SThomas Cort if (feof(stream))
127*4483d188SThomas Cort exit(0);
128*4483d188SThomas Cort if (ferror(stream))
129*4483d188SThomas Cort err(1, "%s", stream == stdin ? "stdin" : "stdout");
130*4483d188SThomas Cort }
131*4483d188SThomas Cort
132*4483d188SThomas Cort static void
usage(void)133*4483d188SThomas Cort usage(void)
134*4483d188SThomas Cort {
135*4483d188SThomas Cort (void)fprintf(stderr, "usage: colrm [start [stop]]\n");
136*4483d188SThomas Cort exit(1);
137*4483d188SThomas Cort }
138