colrm.c revision 27107
10Sstevel@tonic-gate/*- 20Sstevel@tonic-gate * Copyright (c) 1991, 1993 30Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 60Sstevel@tonic-gate * modification, are permitted provided that the following conditions 70Sstevel@tonic-gate * are met: 80Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 90Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 100Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 110Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 120Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 130Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 140Sstevel@tonic-gate * must display the following acknowledgement: 150Sstevel@tonic-gate * This product includes software developed by the University of 160Sstevel@tonic-gate * California, Berkeley and its contributors. 170Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 180Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 190Sstevel@tonic-gate * without specific prior written permission. 200Sstevel@tonic-gate * 210Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 220Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 230Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 240Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 250Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 260Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 270Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 280Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 290Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 300Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 310Sstevel@tonic-gate * SUCH DAMAGE. 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * $Id: colrm.c,v 1.3 1997/06/26 11:26:20 charnier Exp $ 340Sstevel@tonic-gate */ 350Sstevel@tonic-gate 360Sstevel@tonic-gate#ifndef lint 370Sstevel@tonic-gatestatic char copyright[] = 380Sstevel@tonic-gate"@(#) Copyright (c) 1991, 1993\n\ 390Sstevel@tonic-gate The Regents of the University of California. All rights reserved.\n"; 400Sstevel@tonic-gate#endif /* not lint */ 410Sstevel@tonic-gate 420Sstevel@tonic-gate#ifndef lint 430Sstevel@tonic-gatestatic char sccsid[] = "@(#)colrm.c 8.2 (Berkeley) 5/4/95"; 440Sstevel@tonic-gate#endif /* not lint */ 450Sstevel@tonic-gate 460Sstevel@tonic-gate#include <sys/types.h> 470Sstevel@tonic-gate#include <limits.h> 480Sstevel@tonic-gate#include <err.h> 490Sstevel@tonic-gate#include <errno.h> 500Sstevel@tonic-gate#include <stdio.h> 510Sstevel@tonic-gate#include <stdlib.h> 520Sstevel@tonic-gate#include <string.h> 530Sstevel@tonic-gate#include <unistd.h> 540Sstevel@tonic-gate 550Sstevel@tonic-gate#define TAB 8 560Sstevel@tonic-gate 570Sstevel@tonic-gatevoid check __P((FILE *)); 580Sstevel@tonic-gatestatic void usage __P((void)); 590Sstevel@tonic-gate 600Sstevel@tonic-gateint 610Sstevel@tonic-gatemain(argc, argv) 620Sstevel@tonic-gate int argc; 630Sstevel@tonic-gate char *argv[]; 640Sstevel@tonic-gate{ 650Sstevel@tonic-gate register u_long column, start, stop; 660Sstevel@tonic-gate register int ch; 670Sstevel@tonic-gate char *p; 680Sstevel@tonic-gate 690Sstevel@tonic-gate while ((ch = getopt(argc, argv, "")) != -1) 700Sstevel@tonic-gate switch(ch) { 710Sstevel@tonic-gate case '?': 720Sstevel@tonic-gate default: 730Sstevel@tonic-gate usage(); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate argc -= optind; 760Sstevel@tonic-gate argv += optind; 770Sstevel@tonic-gate 780Sstevel@tonic-gate start = stop = 0; 790Sstevel@tonic-gate switch(argc) { 800Sstevel@tonic-gate case 2: 810Sstevel@tonic-gate stop = strtol(argv[1], &p, 10); 820Sstevel@tonic-gate if (stop <= 0 || *p) 830Sstevel@tonic-gate errx(1, "illegal column -- %s", argv[1]); 840Sstevel@tonic-gate /* FALLTHROUGH */ 850Sstevel@tonic-gate case 1: 860Sstevel@tonic-gate start = strtol(argv[0], &p, 10); 870Sstevel@tonic-gate if (start <= 0 || *p) 880Sstevel@tonic-gate errx(1, "illegal column -- %s", argv[0]); 890Sstevel@tonic-gate break; 900Sstevel@tonic-gate case 0: 910Sstevel@tonic-gate break; 920Sstevel@tonic-gate default: 930Sstevel@tonic-gate usage(); 940Sstevel@tonic-gate } 950Sstevel@tonic-gate 960Sstevel@tonic-gate if (stop && start > stop) 970Sstevel@tonic-gate errx(1, "illegal start and stop columns"); 980Sstevel@tonic-gate 990Sstevel@tonic-gate for (column = 0;;) { 1000Sstevel@tonic-gate switch (ch = getchar()) { 1010Sstevel@tonic-gate case EOF: 1020Sstevel@tonic-gate check(stdin); 1030Sstevel@tonic-gate break; 104 case '\b': 105 if (column) 106 --column; 107 break; 108 case '\n': 109 column = 0; 110 break; 111 case '\t': 112 column = (column + TAB) & ~(TAB - 1); 113 break; 114 default: 115 ++column; 116 break; 117 } 118 119 if ((!start || column < start || stop && column > stop) && 120 putchar(ch) == EOF) 121 check(stdout); 122 } 123} 124 125void 126check(stream) 127 FILE *stream; 128{ 129 if (feof(stream)) 130 exit(0); 131 if (ferror(stream)) 132 err(1, "%s", stream == stdin ? "stdin" : "stdout"); 133} 134 135void 136usage() 137{ 138 (void)fprintf(stderr, "usage: colrm [start [stop]]\n"); 139 exit(1); 140} 141 142