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