colrm.c revision 24360
11558Srgrimes/*-
298542Smckusick * Copyright (c) 1991, 1993
398542Smckusick *	The Regents of the University of California.  All rights reserved.
498542Smckusick *
598542Smckusick * Redistribution and use in source and binary forms, with or without
698542Smckusick * modification, are permitted provided that the following conditions
798542Smckusick * are met:
898542Smckusick * 1. Redistributions of source code must retain the above copyright
998542Smckusick *    notice, this list of conditions and the following disclaimer.
1098542Smckusick * 2. Redistributions in binary form must reproduce the above copyright
1198542Smckusick *    notice, this list of conditions and the following disclaimer in the
1298542Smckusick *    documentation and/or other materials provided with the distribution.
1398542Smckusick * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
351558Srgrimesstatic char copyright[] =
361558Srgrimes"@(#) Copyright (c) 1991, 1993\n\
371558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381558Srgrimes#endif /* not lint */
391558Srgrimes
401558Srgrimes#ifndef lint
411558Srgrimesstatic char sccsid[] = "@(#)colrm.c	8.2 (Berkeley) 5/4/95";
421558Srgrimes#endif /* not lint */
431558Srgrimes
441558Srgrimes#include <sys/types.h>
451558Srgrimes#include <limits.h>
461558Srgrimes#include <errno.h>
4736998Scharnier#include <stdio.h>
481558Srgrimes#include <stdlib.h>
491558Srgrimes#include <string.h>
501558Srgrimes#include <unistd.h>
511558Srgrimes
521558Srgrimes#define	TAB	8
5336998Scharnier
5423673Spetervoid err __P((const char *, ...));
5536998Scharniervoid check __P((FILE *));
5636998Scharniervoid usage __P((void));
5750476Speter
581558Srgrimesint
591558Srgrimesmain(argc, argv)
601558Srgrimes	int argc;
611558Srgrimes	char *argv[];
6296478Sphk{
631558Srgrimes	register u_long column, start, stop;
6498542Smckusick	register int ch;
651558Srgrimes	char *p;
661558Srgrimes
6723673Speter	while ((ch = getopt(argc, argv, "")) != -1)
68105741Sjmallett		switch(ch) {
691558Srgrimes		case '?':
701558Srgrimes		default:
7199826Sjmallett			usage();
721558Srgrimes		}
731558Srgrimes	argc -= optind;
7423673Speter	argv += optind;
751558Srgrimes
7699826Sjmallett	start = stop = 0;
771558Srgrimes	switch(argc) {
781558Srgrimes	case 2:
791558Srgrimes		stop = strtol(argv[1], &p, 10);
801558Srgrimes		if (stop <= 0 || *p)
811558Srgrimes			err("illegal column -- %s", argv[1]);
821558Srgrimes		/* FALLTHROUGH */
831558Srgrimes	case 1:
8499826Sjmallett		start = strtol(argv[0], &p, 10);
851558Srgrimes		if (start <= 0 || *p)
8692839Simp			err("illegal column -- %s", argv[0]);
87101688Sjmallett		break;
8892839Simp	case 0:
8992839Simp		break;
901558Srgrimes	default:
911558Srgrimes		usage();
9292839Simp	}
931558Srgrimes
9492806Sobrien	if (stop && start > stop)
951558Srgrimes		err("illegal start and stop columns");
961558Srgrimes
9723673Speter	for (column = 0;;) {
981558Srgrimes		switch (ch = getchar()) {
991558Srgrimes		case EOF:
1001558Srgrimes			check(stdin);
1011558Srgrimes			break;
1021558Srgrimes		case '\b':
1031558Srgrimes			if (column)
1041558Srgrimes				--column;
1051558Srgrimes			break;
1061558Srgrimes		case '\n':
1071558Srgrimes			column = 0;
1081558Srgrimes			break;
1091558Srgrimes		case '\t':
1101558Srgrimes			column = (column + TAB) & ~(TAB - 1);
1111558Srgrimes			break;
1121558Srgrimes		default:
1131558Srgrimes			++column;
1141558Srgrimes			break;
1151558Srgrimes		}
1161558Srgrimes
1171558Srgrimes		if ((!start || column < start || stop && column > stop) &&
11892839Simp		    putchar(ch) == EOF)
1191558Srgrimes			check(stdout);
12098542Smckusick	}
12198542Smckusick}
12299827Sjmallett
1231558Srgrimesvoid
12499826Sjmallettcheck(stream)
12598542Smckusick	FILE *stream;
12623673Speter{
127101688Sjmallett	if (feof(stream))
128101688Sjmallett		exit(0);
12998542Smckusick	if (ferror(stream))
13098542Smckusick		err("%s: %s",
13198542Smckusick		    stream == stdin ? "stdin" : "stdout", strerror(errno));
13298542Smckusick}
13398542Smckusick
13498542Smckusickvoid
13598542Smckusickusage()
13698542Smckusick{
137101688Sjmallett	(void)fprintf(stderr, "usage: colrm [start [stop]]\n");
138101688Sjmallett	exit(1);
13998542Smckusick}
14098542Smckusick
14198542Smckusick#if __STDC__
14298542Smckusick#include <stdarg.h>
14398542Smckusick#else
14498542Smckusick#include <varargs.h>
145101688Sjmallett#endif
146101688Sjmallett
147101688Sjmallettvoid
14898542Smckusick#if __STDC__
1491558Srgrimeserr(const char *fmt, ...)
1501558Srgrimes#else
1511558Srgrimeserr(fmt, va_alist)
1521558Srgrimes	char *fmt;
1531558Srgrimes        va_dcl
1541558Srgrimes#endif
15598542Smckusick{
1561558Srgrimes	va_list ap;
15798542Smckusick#if __STDC__
158101688Sjmallett	va_start(ap, fmt);
159101688Sjmallett#else
16098542Smckusick	va_start(ap);
16198542Smckusick#endif
16298542Smckusick	(void)fprintf(stderr, "colrm: ");
16398542Smckusick	(void)vfprintf(stderr, fmt, ap);
16498542Smckusick	va_end(ap);
16598542Smckusick	(void)fprintf(stderr, "\n");
16698542Smckusick	exit(1);
16798542Smckusick	/* NOTREACHED */
16898542Smckusick}
16998542Smckusick