xref: /csrg-svn/usr.bin/colrm/colrm.c (revision 1358)
1*1358Sbill static	char *Sccsid = "@(#)colrm.c	4.2 (Berkeley) 10/10/80";
2986Sbill #include <stdio.h>
3986Sbill /*
4986Sbill COLRM removes unwanted columns from a file
5986Sbill 	Jeff Schriebman  UC Berkeley 11-74
6986Sbill */
7986Sbill 
8986Sbill 
9986Sbill main(argc,argv)
10986Sbill char **argv;
11986Sbill {
12986Sbill 	int first;
13986Sbill 	register ct,last;
14986Sbill 	register char c;
15986Sbill 	char buffer[BUFSIZ];
16986Sbill 
17986Sbill 	setbuf(stdout, buffer);
18986Sbill 	first = 20000;
19986Sbill 	last  = -1;
20986Sbill 	if (argc>1) {
21986Sbill 		first = getn(*++argv);
22986Sbill 		last = 20000;
23986Sbill 	}
24986Sbill 	if (argc>2)
25986Sbill 		last = getn(*++argv);
26986Sbill 
27986Sbill start:
28986Sbill 	ct = 0;
29986Sbill loop1:
30986Sbill 	if ((c=getc(stdin))<0)
31986Sbill 		goto fin;
32*1358Sbill 	if (c == '\t')
33*1358Sbill 		ct = (ct + 8) &~ 7;
34*1358Sbill 	else if (c == '\b')
35*1358Sbill 		ct = ct ? ct - 1 : 0;
36*1358Sbill 	else
37*1358Sbill 		ct++;
38986Sbill 	if (c=='\n') {
39986Sbill 		putc(c,stdout);
40986Sbill 		goto start;
41986Sbill 	}
42986Sbill 	if (ct<first) {
43986Sbill 		putc(c,stdout);
44986Sbill 		goto loop1;
45986Sbill 	}
46986Sbill 
47986Sbill /* Loop getting rid of characters */
48986Sbill 	for (;ct<last;ct++) {
49986Sbill 		if ((c=getc(stdin))<0)
50986Sbill 			goto fin;
51986Sbill 		if (c=='\n') {
52986Sbill 			putc(c,stdout);
53986Sbill 			goto start;
54986Sbill 		}
55986Sbill 	}
56986Sbill 
57986Sbill /* Output last of the line */
58986Sbill 	while ((c=getc(stdin))>0) {
59986Sbill 		putc(c,stdout);
60986Sbill 		if (c=='\n')
61986Sbill 			goto start;
62986Sbill 	}
63986Sbill fin:
64986Sbill 	fflush(stdout);
65986Sbill }
66986Sbill 
67986Sbill getn(ap)
68986Sbill char *ap;
69986Sbill {
70986Sbill 	register int n,c;
71986Sbill 	register char *p;
72986Sbill 
73986Sbill 	p = ap;
74986Sbill 	n = 0;
75986Sbill 	while ((c = *p++) >= '0' && c <= '9')
76986Sbill 		n = n*10 + c - '0';
77986Sbill 	return(n);
78986Sbill }
79