xref: /csrg-svn/usr.bin/colrm/colrm.c (revision 21551)
1*21551Sdist /*
2*21551Sdist  * Copyright (c) 1980 Regents of the University of California.
3*21551Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21551Sdist  * specifies the terms and conditions for redistribution.
5*21551Sdist  */
6*21551Sdist 
7*21551Sdist #ifndef lint
8*21551Sdist char copyright[] =
9*21551Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*21551Sdist  All rights reserved.\n";
11*21551Sdist #endif not lint
12*21551Sdist 
13*21551Sdist #ifndef lint
14*21551Sdist static char sccsid[] = "@(#)colrm.c	5.1 (Berkeley) 05/31/85";
15*21551Sdist #endif not lint
16*21551Sdist 
17986Sbill #include <stdio.h>
18986Sbill /*
19986Sbill COLRM removes unwanted columns from a file
20986Sbill 	Jeff Schriebman  UC Berkeley 11-74
21986Sbill */
22986Sbill 
23986Sbill 
24986Sbill main(argc,argv)
25986Sbill char **argv;
26986Sbill {
2716659Sralph 	register c, ct, first, last;
28986Sbill 
2916659Sralph 	first = 0;
3016659Sralph 	last = 0;
3116659Sralph 	if (argc > 1)
32986Sbill 		first = getn(*++argv);
3316659Sralph 	if (argc > 2)
34986Sbill 		last = getn(*++argv);
35986Sbill 
36986Sbill start:
37986Sbill 	ct = 0;
38986Sbill loop1:
395791Sroot 	c = getc(stdin);
405791Sroot 	if (feof(stdin))
41986Sbill 		goto fin;
421358Sbill 	if (c == '\t')
4316659Sralph 		ct = (ct + 8) & ~7;
441358Sbill 	else if (c == '\b')
451358Sbill 		ct = ct ? ct - 1 : 0;
461358Sbill 	else
471358Sbill 		ct++;
4816659Sralph 	if (c == '\n') {
4916659Sralph 		putc(c, stdout);
50986Sbill 		goto start;
51986Sbill 	}
5216659Sralph 	if (!first || ct < first) {
5316659Sralph 		putc(c, stdout);
54986Sbill 		goto loop1;
55986Sbill 	}
56986Sbill 
57986Sbill /* Loop getting rid of characters */
5816659Sralph 	while (!last || ct < last) {
595791Sroot 		c = getc(stdin);
605791Sroot 		if (feof(stdin))
61986Sbill 			goto fin;
6216659Sralph 		if (c == '\n') {
6316659Sralph 			putc(c, stdout);
64986Sbill 			goto start;
65986Sbill 		}
6616659Sralph 		if (c == '\t')
6716659Sralph 			ct = (ct + 8) & ~7;
6816659Sralph 		else if (c == '\b')
6916659Sralph 			ct = ct ? ct - 1 : 0;
7016659Sralph 		else
7116659Sralph 			ct++;
72986Sbill 	}
73986Sbill 
74986Sbill /* Output last of the line */
755791Sroot 	for (;;) {
765791Sroot 		c = getc(stdin);
775791Sroot 		if (feof(stdin))
785791Sroot 			break;
7916659Sralph 		putc(c, stdout);
8016659Sralph 		if (c == '\n')
81986Sbill 			goto start;
82986Sbill 	}
83986Sbill fin:
84986Sbill 	fflush(stdout);
85986Sbill }
86986Sbill 
87986Sbill getn(ap)
88986Sbill char *ap;
89986Sbill {
90986Sbill 	register int n,c;
91986Sbill 	register char *p;
92986Sbill 
93986Sbill 	p = ap;
94986Sbill 	n = 0;
95986Sbill 	while ((c = *p++) >= '0' && c <= '9')
96986Sbill 		n = n*10 + c - '0';
97986Sbill 	return(n);
98986Sbill }
99