xref: /csrg-svn/usr.bin/colrm/colrm.c (revision 32749)
121551Sdist /*
221551Sdist  * Copyright (c) 1980 Regents of the University of California.
3*32749Sbostic  * All rights reserved.
4*32749Sbostic  *
5*32749Sbostic  * Redistribution and use in source and binary forms are permitted
6*32749Sbostic  * provided that this notice is preserved and that due credit is given
7*32749Sbostic  * to the University of California at Berkeley. The name of the University
8*32749Sbostic  * may not be used to endorse or promote products derived from this
9*32749Sbostic  * software without specific written prior permission. This software
10*32749Sbostic  * is provided ``as is'' without express or implied warranty.
1121551Sdist  */
1221551Sdist 
1321551Sdist #ifndef lint
1421551Sdist char copyright[] =
1521551Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1621551Sdist  All rights reserved.\n";
17*32749Sbostic #endif /* not lint */
1821551Sdist 
1921551Sdist #ifndef lint
20*32749Sbostic static char sccsid[] = "@(#)colrm.c	5.2 (Berkeley) 12/02/87";
21*32749Sbostic #endif /* not lint */
2221551Sdist 
23986Sbill #include <stdio.h>
24986Sbill /*
25986Sbill COLRM removes unwanted columns from a file
26986Sbill 	Jeff Schriebman  UC Berkeley 11-74
27986Sbill */
28986Sbill 
29986Sbill 
30986Sbill main(argc,argv)
31986Sbill char **argv;
32986Sbill {
3316659Sralph 	register c, ct, first, last;
34986Sbill 
3516659Sralph 	first = 0;
3616659Sralph 	last = 0;
3716659Sralph 	if (argc > 1)
38986Sbill 		first = getn(*++argv);
3916659Sralph 	if (argc > 2)
40986Sbill 		last = getn(*++argv);
41986Sbill 
42986Sbill start:
43986Sbill 	ct = 0;
44986Sbill loop1:
455791Sroot 	c = getc(stdin);
465791Sroot 	if (feof(stdin))
47986Sbill 		goto fin;
481358Sbill 	if (c == '\t')
4916659Sralph 		ct = (ct + 8) & ~7;
501358Sbill 	else if (c == '\b')
511358Sbill 		ct = ct ? ct - 1 : 0;
521358Sbill 	else
531358Sbill 		ct++;
5416659Sralph 	if (c == '\n') {
5516659Sralph 		putc(c, stdout);
56986Sbill 		goto start;
57986Sbill 	}
5816659Sralph 	if (!first || ct < first) {
5916659Sralph 		putc(c, stdout);
60986Sbill 		goto loop1;
61986Sbill 	}
62986Sbill 
63986Sbill /* Loop getting rid of characters */
6416659Sralph 	while (!last || ct < last) {
655791Sroot 		c = getc(stdin);
665791Sroot 		if (feof(stdin))
67986Sbill 			goto fin;
6816659Sralph 		if (c == '\n') {
6916659Sralph 			putc(c, stdout);
70986Sbill 			goto start;
71986Sbill 		}
7216659Sralph 		if (c == '\t')
7316659Sralph 			ct = (ct + 8) & ~7;
7416659Sralph 		else if (c == '\b')
7516659Sralph 			ct = ct ? ct - 1 : 0;
7616659Sralph 		else
7716659Sralph 			ct++;
78986Sbill 	}
79986Sbill 
80986Sbill /* Output last of the line */
815791Sroot 	for (;;) {
825791Sroot 		c = getc(stdin);
835791Sroot 		if (feof(stdin))
845791Sroot 			break;
8516659Sralph 		putc(c, stdout);
8616659Sralph 		if (c == '\n')
87986Sbill 			goto start;
88986Sbill 	}
89986Sbill fin:
90986Sbill 	fflush(stdout);
91*32749Sbostic 	exit(0);
92986Sbill }
93986Sbill 
94986Sbill getn(ap)
95986Sbill char *ap;
96986Sbill {
97986Sbill 	register int n,c;
98986Sbill 	register char *p;
99986Sbill 
100986Sbill 	p = ap;
101986Sbill 	n = 0;
102986Sbill 	while ((c = *p++) >= '0' && c <= '9')
103986Sbill 		n = n*10 + c - '0';
104986Sbill 	return(n);
105986Sbill }
106