colrm.c revision 100817
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 100817 2002-07-28 15:22:43Z 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 6092920Simpvoid check(FILE *); 6192920Simpstatic void usage(void); 621590Srgrimes 631590Srgrimesint 64100817Sdwmalonemain(int argc, char *argv[]) 651590Srgrimes{ 6687244Smarkm u_long column, start, stop; 6787244Smarkm int ch; 681590Srgrimes char *p; 691590Srgrimes 7024360Simp while ((ch = getopt(argc, argv, "")) != -1) 711590Srgrimes switch(ch) { 721590Srgrimes case '?': 731590Srgrimes default: 741590Srgrimes usage(); 751590Srgrimes } 761590Srgrimes argc -= optind; 771590Srgrimes argv += optind; 781590Srgrimes 791590Srgrimes start = stop = 0; 801590Srgrimes switch(argc) { 811590Srgrimes case 2: 821590Srgrimes stop = strtol(argv[1], &p, 10); 831590Srgrimes if (stop <= 0 || *p) 8426960Scharnier errx(1, "illegal column -- %s", argv[1]); 851590Srgrimes /* FALLTHROUGH */ 861590Srgrimes case 1: 871590Srgrimes start = strtol(argv[0], &p, 10); 881590Srgrimes if (start <= 0 || *p) 8926960Scharnier errx(1, "illegal column -- %s", argv[0]); 901590Srgrimes break; 911590Srgrimes case 0: 921590Srgrimes break; 931590Srgrimes default: 941590Srgrimes usage(); 951590Srgrimes } 961590Srgrimes 971590Srgrimes if (stop && start > stop) 9826960Scharnier errx(1, "illegal start and stop columns"); 991590Srgrimes 1001590Srgrimes for (column = 0;;) { 1011590Srgrimes switch (ch = getchar()) { 1021590Srgrimes case EOF: 1031590Srgrimes check(stdin); 1041590Srgrimes break; 1051590Srgrimes case '\b': 1061590Srgrimes if (column) 1071590Srgrimes --column; 1081590Srgrimes break; 1091590Srgrimes case '\n': 1101590Srgrimes column = 0; 1111590Srgrimes break; 1121590Srgrimes case '\t': 1131590Srgrimes column = (column + TAB) & ~(TAB - 1); 1141590Srgrimes break; 1151590Srgrimes default: 1161590Srgrimes ++column; 1171590Srgrimes break; 1181590Srgrimes } 1191590Srgrimes 12032069Salex if ((!start || column < start || (stop && column > stop)) && 1211590Srgrimes putchar(ch) == EOF) 1221590Srgrimes check(stdout); 1231590Srgrimes } 1241590Srgrimes} 1251590Srgrimes 1261590Srgrimesvoid 127100817Sdwmalonecheck(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 136100817Sdwmaloneusage(void) 1371590Srgrimes{ 1381590Srgrimes (void)fprintf(stderr, "usage: colrm [start [stop]]\n"); 1391590Srgrimes exit(1); 1401590Srgrimes} 1411590Srgrimes 142