colrm.c revision 23690
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1991, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
351590Srgrimesstatic char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1991, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4123690Speterstatic char sccsid[] = "@(#)colrm.c	8.2 (Berkeley) 5/4/95";
421590Srgrimes#endif /* not lint */
431590Srgrimes
441590Srgrimes#include <sys/types.h>
451590Srgrimes#include <limits.h>
461590Srgrimes#include <errno.h>
471590Srgrimes#include <stdio.h>
481590Srgrimes#include <stdlib.h>
491590Srgrimes#include <string.h>
5023690Speter#include <unistd.h>
511590Srgrimes
521590Srgrimes#define	TAB	8
531590Srgrimes
541590Srgrimesvoid err __P((const char *, ...));
551590Srgrimesvoid check __P((FILE *));
561590Srgrimesvoid usage __P((void));
571590Srgrimes
581590Srgrimesint
591590Srgrimesmain(argc, argv)
601590Srgrimes	int argc;
611590Srgrimes	char *argv[];
621590Srgrimes{
631590Srgrimes	register u_long column, start, stop;
641590Srgrimes	register int ch;
651590Srgrimes	char *p;
661590Srgrimes
671590Srgrimes	while ((ch = getopt(argc, argv, "")) != EOF)
681590Srgrimes		switch(ch) {
691590Srgrimes		case '?':
701590Srgrimes		default:
711590Srgrimes			usage();
721590Srgrimes		}
731590Srgrimes	argc -= optind;
741590Srgrimes	argv += optind;
751590Srgrimes
761590Srgrimes	start = stop = 0;
771590Srgrimes	switch(argc) {
781590Srgrimes	case 2:
791590Srgrimes		stop = strtol(argv[1], &p, 10);
801590Srgrimes		if (stop <= 0 || *p)
811590Srgrimes			err("illegal column -- %s", argv[1]);
821590Srgrimes		/* FALLTHROUGH */
831590Srgrimes	case 1:
841590Srgrimes		start = strtol(argv[0], &p, 10);
851590Srgrimes		if (start <= 0 || *p)
861590Srgrimes			err("illegal column -- %s", argv[0]);
871590Srgrimes		break;
881590Srgrimes	case 0:
891590Srgrimes		break;
901590Srgrimes	default:
911590Srgrimes		usage();
921590Srgrimes	}
931590Srgrimes
941590Srgrimes	if (stop && start > stop)
951590Srgrimes		err("illegal start and stop columns");
961590Srgrimes
971590Srgrimes	for (column = 0;;) {
981590Srgrimes		switch (ch = getchar()) {
991590Srgrimes		case EOF:
1001590Srgrimes			check(stdin);
1011590Srgrimes			break;
1021590Srgrimes		case '\b':
1031590Srgrimes			if (column)
1041590Srgrimes				--column;
1051590Srgrimes			break;
1061590Srgrimes		case '\n':
1071590Srgrimes			column = 0;
1081590Srgrimes			break;
1091590Srgrimes		case '\t':
1101590Srgrimes			column = (column + TAB) & ~(TAB - 1);
1111590Srgrimes			break;
1121590Srgrimes		default:
1131590Srgrimes			++column;
1141590Srgrimes			break;
1151590Srgrimes		}
1161590Srgrimes
1171590Srgrimes		if ((!start || column < start || stop && column > stop) &&
1181590Srgrimes		    putchar(ch) == EOF)
1191590Srgrimes			check(stdout);
1201590Srgrimes	}
1211590Srgrimes}
1221590Srgrimes
1231590Srgrimesvoid
1241590Srgrimescheck(stream)
1251590Srgrimes	FILE *stream;
1261590Srgrimes{
1271590Srgrimes	if (feof(stream))
1281590Srgrimes		exit(0);
1291590Srgrimes	if (ferror(stream))
1301590Srgrimes		err("%s: %s",
1311590Srgrimes		    stream == stdin ? "stdin" : "stdout", strerror(errno));
1321590Srgrimes}
1331590Srgrimes
1341590Srgrimesvoid
1351590Srgrimesusage()
1361590Srgrimes{
1371590Srgrimes	(void)fprintf(stderr, "usage: colrm [start [stop]]\n");
1381590Srgrimes	exit(1);
1391590Srgrimes}
1401590Srgrimes
1411590Srgrimes#if __STDC__
1421590Srgrimes#include <stdarg.h>
1431590Srgrimes#else
1441590Srgrimes#include <varargs.h>
1451590Srgrimes#endif
1461590Srgrimes
1471590Srgrimesvoid
1481590Srgrimes#if __STDC__
1491590Srgrimeserr(const char *fmt, ...)
1501590Srgrimes#else
1511590Srgrimeserr(fmt, va_alist)
1521590Srgrimes	char *fmt;
1531590Srgrimes        va_dcl
1541590Srgrimes#endif
1551590Srgrimes{
1561590Srgrimes	va_list ap;
1571590Srgrimes#if __STDC__
1581590Srgrimes	va_start(ap, fmt);
1591590Srgrimes#else
1601590Srgrimes	va_start(ap);
1611590Srgrimes#endif
1621590Srgrimes	(void)fprintf(stderr, "colrm: ");
1631590Srgrimes	(void)vfprintf(stderr, fmt, ap);
1641590Srgrimes	va_end(ap);
1651590Srgrimes	(void)fprintf(stderr, "\n");
1661590Srgrimes	exit(1);
1671590Srgrimes	/* NOTREACHED */
1681590Srgrimes}
169