xref: /original-bsd/usr.bin/colrm/colrm.c (revision d0e3910b)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific written prior permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 char copyright[] =
15 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
16  All rights reserved.\n";
17 #endif /* not lint */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)colrm.c	5.2 (Berkeley) 12/02/87";
21 #endif /* not lint */
22 
23 #include <stdio.h>
24 /*
25 COLRM removes unwanted columns from a file
26 	Jeff Schriebman  UC Berkeley 11-74
27 */
28 
29 
30 main(argc,argv)
31 char **argv;
32 {
33 	register c, ct, first, last;
34 
35 	first = 0;
36 	last = 0;
37 	if (argc > 1)
38 		first = getn(*++argv);
39 	if (argc > 2)
40 		last = getn(*++argv);
41 
42 start:
43 	ct = 0;
44 loop1:
45 	c = getc(stdin);
46 	if (feof(stdin))
47 		goto fin;
48 	if (c == '\t')
49 		ct = (ct + 8) & ~7;
50 	else if (c == '\b')
51 		ct = ct ? ct - 1 : 0;
52 	else
53 		ct++;
54 	if (c == '\n') {
55 		putc(c, stdout);
56 		goto start;
57 	}
58 	if (!first || ct < first) {
59 		putc(c, stdout);
60 		goto loop1;
61 	}
62 
63 /* Loop getting rid of characters */
64 	while (!last || ct < last) {
65 		c = getc(stdin);
66 		if (feof(stdin))
67 			goto fin;
68 		if (c == '\n') {
69 			putc(c, stdout);
70 			goto start;
71 		}
72 		if (c == '\t')
73 			ct = (ct + 8) & ~7;
74 		else if (c == '\b')
75 			ct = ct ? ct - 1 : 0;
76 		else
77 			ct++;
78 	}
79 
80 /* Output last of the line */
81 	for (;;) {
82 		c = getc(stdin);
83 		if (feof(stdin))
84 			break;
85 		putc(c, stdout);
86 		if (c == '\n')
87 			goto start;
88 	}
89 fin:
90 	fflush(stdout);
91 	exit(0);
92 }
93 
94 getn(ap)
95 char *ap;
96 {
97 	register int n,c;
98 	register char *p;
99 
100 	p = ap;
101 	n = 0;
102 	while ((c = *p++) >= '0' && c <= '9')
103 		n = n*10 + c - '0';
104 	return(n);
105 }
106