colrm.c revision 87628
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
3541568Sarchiestatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1991, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3887244Smarkm#endif
391590Srgrimes
4087628Sdwmalone#if 0
411590Srgrimes#ifndef lint
4287628Sdwmalonestatic char sccsid[] = "@(#)colrm.c	8.2 (Berkeley) 5/4/95";
4387244Smarkm#endif
4487628Sdwmalone#endif
451590Srgrimes
4687628Sdwmalone#include <sys/cdefs.h>
4787628Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/colrm/colrm.c 87628 2001-12-10 21:13:08Z dwmalone $");
4887628Sdwmalone
491590Srgrimes#include <sys/types.h>
5026960Scharnier#include <err.h>
511590Srgrimes#include <errno.h>
5287244Smarkm#include <limits.h>
531590Srgrimes#include <stdio.h>
541590Srgrimes#include <stdlib.h>
551590Srgrimes#include <string.h>
5623690Speter#include <unistd.h>
571590Srgrimes
581590Srgrimes#define	TAB	8
591590Srgrimes
601590Srgrimesvoid check __P((FILE *));
6126960Scharnierstatic void usage __P((void));
621590Srgrimes
631590Srgrimesint
641590Srgrimesmain(argc, argv)
651590Srgrimes	int argc;
661590Srgrimes	char *argv[];
671590Srgrimes{
6887244Smarkm	u_long column, start, stop;
6987244Smarkm	int ch;
701590Srgrimes	char *p;
711590Srgrimes
7224360Simp	while ((ch = getopt(argc, argv, "")) != -1)
731590Srgrimes		switch(ch) {
741590Srgrimes		case '?':
751590Srgrimes		default:
761590Srgrimes			usage();
771590Srgrimes		}
781590Srgrimes	argc -= optind;
791590Srgrimes	argv += optind;
801590Srgrimes
811590Srgrimes	start = stop = 0;
821590Srgrimes	switch(argc) {
831590Srgrimes	case 2:
841590Srgrimes		stop = strtol(argv[1], &p, 10);
851590Srgrimes		if (stop <= 0 || *p)
8626960Scharnier			errx(1, "illegal column -- %s", argv[1]);
871590Srgrimes		/* FALLTHROUGH */
881590Srgrimes	case 1:
891590Srgrimes		start = strtol(argv[0], &p, 10);
901590Srgrimes		if (start <= 0 || *p)
9126960Scharnier			errx(1, "illegal column -- %s", argv[0]);
921590Srgrimes		break;
931590Srgrimes	case 0:
941590Srgrimes		break;
951590Srgrimes	default:
961590Srgrimes		usage();
971590Srgrimes	}
981590Srgrimes
991590Srgrimes	if (stop && start > stop)
10026960Scharnier		errx(1, "illegal start and stop columns");
1011590Srgrimes
1021590Srgrimes	for (column = 0;;) {
1031590Srgrimes		switch (ch = getchar()) {
1041590Srgrimes		case EOF:
1051590Srgrimes			check(stdin);
1061590Srgrimes			break;
1071590Srgrimes		case '\b':
1081590Srgrimes			if (column)
1091590Srgrimes				--column;
1101590Srgrimes			break;
1111590Srgrimes		case '\n':
1121590Srgrimes			column = 0;
1131590Srgrimes			break;
1141590Srgrimes		case '\t':
1151590Srgrimes			column = (column + TAB) & ~(TAB - 1);
1161590Srgrimes			break;
1171590Srgrimes		default:
1181590Srgrimes			++column;
1191590Srgrimes			break;
1201590Srgrimes		}
1211590Srgrimes
12232069Salex		if ((!start || column < start || (stop && column > stop)) &&
1231590Srgrimes		    putchar(ch) == EOF)
1241590Srgrimes			check(stdout);
1251590Srgrimes	}
1261590Srgrimes}
1271590Srgrimes
1281590Srgrimesvoid
1291590Srgrimescheck(stream)
1301590Srgrimes	FILE *stream;
1311590Srgrimes{
1321590Srgrimes	if (feof(stream))
1331590Srgrimes		exit(0);
1341590Srgrimes	if (ferror(stream))
13526960Scharnier		err(1, "%s", stream == stdin ? "stdin" : "stdout");
1361590Srgrimes}
1371590Srgrimes
1381590Srgrimesvoid
1391590Srgrimesusage()
1401590Srgrimes{
1411590Srgrimes	(void)fprintf(stderr, "usage: colrm [start [stop]]\n");
1421590Srgrimes	exit(1);
1431590Srgrimes}
1441590Srgrimes
145