Annotation of src/usr.bin/colrm/colrm.c, Revision 1.9
1.9 ! deraadt 1: /* $OpenBSD: colrm.c,v 1.8 2003/06/10 22:20:45 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: colrm.c,v 1.4 1995/09/02 05:51:37 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1991, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.7 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #include <sys/types.h>
34:
35: #include <err.h>
36: #include <errno.h>
37: #include <limits.h>
38: #include <stdio.h>
39: #include <stdlib.h>
40: #include <string.h>
41: #include <unistd.h>
42:
43: #define TAB 8
44:
1.6 millert 45: void check(FILE *);
46: void usage(void);
1.1 deraadt 47:
48: int
1.8 deraadt 49: main(int argc, char *argv[])
1.1 deraadt 50: {
1.5 mpech 51: u_long column, start, stop;
52: int ch;
1.1 deraadt 53: char *p;
54:
1.3 millert 55: while ((ch = getopt(argc, argv, "")) != -1)
1.1 deraadt 56: switch(ch) {
57: case '?':
58: default:
59: usage();
60: }
61: argc -= optind;
62: argv += optind;
63:
64: start = stop = 0;
65: switch(argc) {
66: case 2:
67: stop = strtol(argv[1], &p, 10);
68: if (stop <= 0 || *p)
69: errx(1, "illegal column -- %s", argv[1]);
70: /* FALLTHROUGH */
71: case 1:
72: start = strtol(argv[0], &p, 10);
73: if (start <= 0 || *p)
74: errx(1, "illegal column -- %s", argv[0]);
75: break;
76: case 0:
77: break;
78: default:
79: usage();
80: }
81:
82: if (stop && start > stop)
83: err(1, "illegal start and stop columns");
84:
85: for (column = 0;;) {
86: switch (ch = getchar()) {
87: case EOF:
88: check(stdin);
89: break;
90: case '\b':
91: if (column)
92: --column;
93: break;
94: case '\n':
95: column = 0;
96: break;
97: case '\t':
98: column = (column + TAB) & ~(TAB - 1);
99: break;
100: default:
101: ++column;
102: break;
103: }
104:
1.4 deraadt 105: if ((!start || column < start || (stop && column > stop)) &&
1.1 deraadt 106: putchar(ch) == EOF)
107: check(stdout);
108: }
109: }
110:
111: void
1.8 deraadt 112: check(FILE *stream)
1.1 deraadt 113: {
114: if (feof(stream))
115: exit(0);
116: if (ferror(stream))
117: err(1, "%s", stream == stdin ? "stdin" : "stdout");
118: }
119:
120: void
1.8 deraadt 121: usage(void)
1.1 deraadt 122: {
123: (void)fprintf(stderr, "usage: colrm [start [stop]]\n");
124: exit(1);
125: }