xref: /csrg-svn/usr.bin/colrm/colrm.c (revision 16659)
1*16659Sralph static	char *Sccsid = "@(#)colrm.c	4.4 (Berkeley) 07/02/84";
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 {
12*16659Sralph 	register c, ct, first, last;
13986Sbill 
14*16659Sralph 	first = 0;
15*16659Sralph 	last = 0;
16*16659Sralph 	if (argc > 1)
17986Sbill 		first = getn(*++argv);
18*16659Sralph 	if (argc > 2)
19986Sbill 		last = getn(*++argv);
20986Sbill 
21986Sbill start:
22986Sbill 	ct = 0;
23986Sbill loop1:
245791Sroot 	c = getc(stdin);
255791Sroot 	if (feof(stdin))
26986Sbill 		goto fin;
271358Sbill 	if (c == '\t')
28*16659Sralph 		ct = (ct + 8) & ~7;
291358Sbill 	else if (c == '\b')
301358Sbill 		ct = ct ? ct - 1 : 0;
311358Sbill 	else
321358Sbill 		ct++;
33*16659Sralph 	if (c == '\n') {
34*16659Sralph 		putc(c, stdout);
35986Sbill 		goto start;
36986Sbill 	}
37*16659Sralph 	if (!first || ct < first) {
38*16659Sralph 		putc(c, stdout);
39986Sbill 		goto loop1;
40986Sbill 	}
41986Sbill 
42986Sbill /* Loop getting rid of characters */
43*16659Sralph 	while (!last || ct < last) {
445791Sroot 		c = getc(stdin);
455791Sroot 		if (feof(stdin))
46986Sbill 			goto fin;
47*16659Sralph 		if (c == '\n') {
48*16659Sralph 			putc(c, stdout);
49986Sbill 			goto start;
50986Sbill 		}
51*16659Sralph 		if (c == '\t')
52*16659Sralph 			ct = (ct + 8) & ~7;
53*16659Sralph 		else if (c == '\b')
54*16659Sralph 			ct = ct ? ct - 1 : 0;
55*16659Sralph 		else
56*16659Sralph 			ct++;
57986Sbill 	}
58986Sbill 
59986Sbill /* Output last of the line */
605791Sroot 	for (;;) {
615791Sroot 		c = getc(stdin);
625791Sroot 		if (feof(stdin))
635791Sroot 			break;
64*16659Sralph 		putc(c, stdout);
65*16659Sralph 		if (c == '\n')
66986Sbill 			goto start;
67986Sbill 	}
68986Sbill fin:
69986Sbill 	fflush(stdout);
70986Sbill }
71986Sbill 
72986Sbill getn(ap)
73986Sbill char *ap;
74986Sbill {
75986Sbill 	register int n,c;
76986Sbill 	register char *p;
77986Sbill 
78986Sbill 	p = ap;
79986Sbill 	n = 0;
80986Sbill 	while ((c = *p++) >= '0' && c <= '9')
81986Sbill 		n = n*10 + c - '0';
82986Sbill 	return(n);
83986Sbill }
84