colrm.c revision 32069
1207618Srdivacky/*- 2207618Srdivacky * Copyright (c) 1991, 1993 3207618Srdivacky * The Regents of the University of California. All rights reserved. 4207618Srdivacky * 5207618Srdivacky * Redistribution and use in source and binary forms, with or without 6207618Srdivacky * modification, are permitted provided that the following conditions 7207618Srdivacky * are met: 8207618Srdivacky * 1. Redistributions of source code must retain the above copyright 9207618Srdivacky * notice, this list of conditions and the following disclaimer. 10207618Srdivacky * 2. Redistributions in binary form must reproduce the above copyright 11207618Srdivacky * notice, this list of conditions and the following disclaimer in the 12207618Srdivacky * documentation and/or other materials provided with the distribution. 13207618Srdivacky * 3. All advertising materials mentioning features or use of this software 14207618Srdivacky * must display the following acknowledgement: 15207618Srdivacky * This product includes software developed by the University of 16207618Srdivacky * California, Berkeley and its contributors. 17207618Srdivacky * 4. Neither the name of the University nor the names of its contributors 18207618Srdivacky * may be used to endorse or promote products derived from this software 19207618Srdivacky * without specific prior written permission. 20207618Srdivacky * 21207618Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22207618Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23207618Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24207618Srdivacky * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25207618Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26207618Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27207618Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28207618Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29207618Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30207618Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31207618Srdivacky * SUCH DAMAGE. 32207618Srdivacky * 33207618Srdivacky * $Id: colrm.c,v 1.4 1997/06/30 11:05:42 charnier Exp $ 34207618Srdivacky */ 35207618Srdivacky 36207618Srdivacky#ifndef lint 37207618Srdivackystatic char copyright[] = 38207618Srdivacky"@(#) Copyright (c) 1991, 1993\n\ 39207618Srdivacky The Regents of the University of California. All rights reserved.\n"; 40239462Sdim#endif /* not lint */ 41239462Sdim 42207618Srdivacky#ifndef lint 43207618Srdivackystatic char sccsid[] = "@(#)colrm.c 8.2 (Berkeley) 5/4/95"; 44239462Sdim#endif /* not lint */ 45207618Srdivacky 46207618Srdivacky#include <sys/types.h> 47239462Sdim#include <limits.h> 48239462Sdim#include <err.h> 49239462Sdim#include <errno.h> 50207618Srdivacky#include <stdio.h> 51207618Srdivacky#include <stdlib.h> 52239462Sdim#include <string.h> 53239462Sdim#include <unistd.h> 54239462Sdim 55239462Sdim#define TAB 8 56239462Sdim 57207618Srdivackyvoid check __P((FILE *)); 58207618Srdivackystatic void usage __P((void)); 59239462Sdim 60234353Sdimint 61207618Srdivackymain(argc, argv) 62207618Srdivacky int argc; 63207618Srdivacky char *argv[]; 64207618Srdivacky{ 65239462Sdim register u_long column, start, stop; 66207618Srdivacky register int ch; 67207618Srdivacky char *p; 68239462Sdim 69239462Sdim while ((ch = getopt(argc, argv, "")) != -1) 70239462Sdim switch(ch) { 71239462Sdim case '?': 72239462Sdim default: 73207618Srdivacky usage(); 74207618Srdivacky } 75288943Sdim argc -= optind; 76288943Sdim argv += optind; 77288943Sdim 78288943Sdim start = stop = 0; 79288943Sdim switch(argc) { 80288943Sdim case 2: 81288943Sdim stop = strtol(argv[1], &p, 10); 82280031Sdim if (stop <= 0 || *p) 83276479Sdim errx(1, "illegal column -- %s", argv[1]); 84276479Sdim /* FALLTHROUGH */ 85276479Sdim case 1: 86276479Sdim start = strtol(argv[0], &p, 10); 87276479Sdim if (start <= 0 || *p) 88239462Sdim errx(1, "illegal column -- %s", argv[0]); 89239462Sdim break; 90239462Sdim case 0: 91207618Srdivacky break; 92207618Srdivacky default: 93309124Sdim usage(); 94309124Sdim } 95309124Sdim 96309124Sdim if (stop && start > stop) 97309124Sdim errx(1, "illegal start and stop columns"); 98239462Sdim 99239462Sdim for (column = 0;;) { 100239462Sdim switch (ch = getchar()) { 101239462Sdim case EOF: 102207618Srdivacky check(stdin); 103207618Srdivacky break; 104239462Sdim case '\b': 105239462Sdim if (column) 106239462Sdim --column; 107239462Sdim break; 108208599Srdivacky case '\n': 109208599Srdivacky column = 0; 110239462Sdim break; 111239462Sdim case '\t': 112239462Sdim column = (column + TAB) & ~(TAB - 1); 113239462Sdim break; 114208599Srdivacky default: 115208599Srdivacky ++column; 116296417Sdim break; 117296417Sdim } 118296417Sdim 119296417Sdim if ((!start || column < start || (stop && column > stop)) && 120239462Sdim putchar(ch) == EOF) 121239462Sdim check(stdout); 122239462Sdim } 123239462Sdim} 124239462Sdim 125207618Srdivackyvoid 126207618Srdivackycheck(stream) 127207618Srdivacky FILE *stream; 128239462Sdim{ 129239462Sdim if (feof(stream)) 130239462Sdim exit(0); 131207618Srdivacky if (ferror(stream)) 132207618Srdivacky err(1, "%s", stream == stdin ? "stdin" : "stdout"); 133207618Srdivacky} 134207618Srdivacky 135207618Srdivackyvoid 136207618Srdivackyusage() 137207618Srdivacky{ 138207618Srdivacky (void)fprintf(stderr, "usage: colrm [start [stop]]\n"); 139288943Sdim exit(1); 140288943Sdim} 141239462Sdim 142239462Sdim