colrm.c revision 32069
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. 3226960Scharnier * 3332069Salex * $Id: colrm.c,v 1.4 1997/06/30 11:05:42 charnier Exp $ 341590Srgrimes */ 351590Srgrimes 361590Srgrimes#ifndef lint 371590Srgrimesstatic char copyright[] = 381590Srgrimes"@(#) Copyright (c) 1991, 1993\n\ 391590Srgrimes The Regents of the University of California. All rights reserved.\n"; 401590Srgrimes#endif /* not lint */ 411590Srgrimes 421590Srgrimes#ifndef lint 4323690Speterstatic char sccsid[] = "@(#)colrm.c 8.2 (Berkeley) 5/4/95"; 441590Srgrimes#endif /* not lint */ 451590Srgrimes 461590Srgrimes#include <sys/types.h> 471590Srgrimes#include <limits.h> 4826960Scharnier#include <err.h> 491590Srgrimes#include <errno.h> 501590Srgrimes#include <stdio.h> 511590Srgrimes#include <stdlib.h> 521590Srgrimes#include <string.h> 5323690Speter#include <unistd.h> 541590Srgrimes 551590Srgrimes#define TAB 8 561590Srgrimes 571590Srgrimesvoid check __P((FILE *)); 5826960Scharnierstatic void usage __P((void)); 591590Srgrimes 601590Srgrimesint 611590Srgrimesmain(argc, argv) 621590Srgrimes int argc; 631590Srgrimes char *argv[]; 641590Srgrimes{ 651590Srgrimes register u_long column, start, stop; 661590Srgrimes register int ch; 671590Srgrimes char *p; 681590Srgrimes 6924360Simp while ((ch = getopt(argc, argv, "")) != -1) 701590Srgrimes switch(ch) { 711590Srgrimes case '?': 721590Srgrimes default: 731590Srgrimes usage(); 741590Srgrimes } 751590Srgrimes argc -= optind; 761590Srgrimes argv += optind; 771590Srgrimes 781590Srgrimes start = stop = 0; 791590Srgrimes switch(argc) { 801590Srgrimes case 2: 811590Srgrimes stop = strtol(argv[1], &p, 10); 821590Srgrimes if (stop <= 0 || *p) 8326960Scharnier errx(1, "illegal column -- %s", argv[1]); 841590Srgrimes /* FALLTHROUGH */ 851590Srgrimes case 1: 861590Srgrimes start = strtol(argv[0], &p, 10); 871590Srgrimes if (start <= 0 || *p) 8826960Scharnier errx(1, "illegal column -- %s", argv[0]); 891590Srgrimes break; 901590Srgrimes case 0: 911590Srgrimes break; 921590Srgrimes default: 931590Srgrimes usage(); 941590Srgrimes } 951590Srgrimes 961590Srgrimes if (stop && start > stop) 9726960Scharnier errx(1, "illegal start and stop columns"); 981590Srgrimes 991590Srgrimes for (column = 0;;) { 1001590Srgrimes switch (ch = getchar()) { 1011590Srgrimes case EOF: 1021590Srgrimes check(stdin); 1031590Srgrimes break; 1041590Srgrimes case '\b': 1051590Srgrimes if (column) 1061590Srgrimes --column; 1071590Srgrimes break; 1081590Srgrimes case '\n': 1091590Srgrimes column = 0; 1101590Srgrimes break; 1111590Srgrimes case '\t': 1121590Srgrimes column = (column + TAB) & ~(TAB - 1); 1131590Srgrimes break; 1141590Srgrimes default: 1151590Srgrimes ++column; 1161590Srgrimes break; 1171590Srgrimes } 1181590Srgrimes 11932069Salex if ((!start || column < start || (stop && column > stop)) && 1201590Srgrimes putchar(ch) == EOF) 1211590Srgrimes check(stdout); 1221590Srgrimes } 1231590Srgrimes} 1241590Srgrimes 1251590Srgrimesvoid 1261590Srgrimescheck(stream) 1271590Srgrimes FILE *stream; 1281590Srgrimes{ 1291590Srgrimes if (feof(stream)) 1301590Srgrimes exit(0); 1311590Srgrimes if (ferror(stream)) 13226960Scharnier err(1, "%s", stream == stdin ? "stdin" : "stdout"); 1331590Srgrimes} 1341590Srgrimes 1351590Srgrimesvoid 1361590Srgrimesusage() 1371590Srgrimes{ 1381590Srgrimes (void)fprintf(stderr, "usage: colrm [start [stop]]\n"); 1391590Srgrimes exit(1); 1401590Srgrimes} 1411590Srgrimes 142