152142Sbostic /*-
261948Sbostic * Copyright (c) 1991, 1993
361948Sbostic * The Regents of the University of California. All rights reserved.
432749Sbostic *
542723Sbostic * %sccs.include.redist.c%
621551Sdist */
721551Sdist
821551Sdist #ifndef lint
961948Sbostic static char copyright[] =
1061948Sbostic "@(#) Copyright (c) 1991, 1993\n\
1161948Sbostic The Regents of the University of California. All rights reserved.\n";
1232749Sbostic #endif /* not lint */
1321551Sdist
1421551Sdist #ifndef lint
15*69229Sbostic static char sccsid[] = "@(#)colrm.c 8.2 (Berkeley) 05/04/95";
1632749Sbostic #endif /* not lint */
1721551Sdist
1852142Sbostic #include <sys/types.h>
1952142Sbostic #include <limits.h>
2052142Sbostic #include <errno.h>
21986Sbill #include <stdio.h>
2252142Sbostic #include <stdlib.h>
2352142Sbostic #include <string.h>
24*69229Sbostic #include <unistd.h>
25986Sbill
2652142Sbostic #define TAB 8
27986Sbill
2852142Sbostic void err __P((const char *, ...));
2952142Sbostic void check __P((FILE *));
3052142Sbostic void usage __P((void));
3152142Sbostic
3252142Sbostic int
main(argc,argv)3352142Sbostic main(argc, argv)
3452142Sbostic int argc;
3552142Sbostic char *argv[];
36986Sbill {
3752142Sbostic register u_long column, start, stop;
3852142Sbostic register int ch;
3952142Sbostic char *p;
40986Sbill
4152142Sbostic while ((ch = getopt(argc, argv, "")) != EOF)
4252142Sbostic switch(ch) {
4352142Sbostic case '?':
4452142Sbostic default:
4552142Sbostic usage();
4652142Sbostic }
4752142Sbostic argc -= optind;
4852142Sbostic argv += optind;
49986Sbill
5052142Sbostic start = stop = 0;
5152142Sbostic switch(argc) {
5252142Sbostic case 2:
5352142Sbostic stop = strtol(argv[1], &p, 10);
5452142Sbostic if (stop <= 0 || *p)
5552142Sbostic err("illegal column -- %s", argv[1]);
5652142Sbostic /* FALLTHROUGH */
5752142Sbostic case 1:
5852142Sbostic start = strtol(argv[0], &p, 10);
5952142Sbostic if (start <= 0 || *p)
6052142Sbostic err("illegal column -- %s", argv[0]);
6152142Sbostic break;
6252142Sbostic case 0:
6352142Sbostic break;
6452142Sbostic default:
6552142Sbostic usage();
66986Sbill }
67986Sbill
6852142Sbostic if (stop && start > stop)
6952142Sbostic err("illegal start and stop columns");
7052142Sbostic
7152142Sbostic for (column = 0;;) {
7252142Sbostic switch (ch = getchar()) {
7352142Sbostic case EOF:
7452142Sbostic check(stdin);
7552142Sbostic break;
7652142Sbostic case '\b':
7752142Sbostic if (column)
7852142Sbostic --column;
7952142Sbostic break;
8052142Sbostic case '\n':
8152142Sbostic column = 0;
8252142Sbostic break;
8352142Sbostic case '\t':
8452142Sbostic column = (column + TAB) & ~(TAB - 1);
8552142Sbostic break;
8652142Sbostic default:
8752142Sbostic ++column;
8852142Sbostic break;
89986Sbill }
90986Sbill
9152142Sbostic if ((!start || column < start || stop && column > stop) &&
9252142Sbostic putchar(ch) == EOF)
9352142Sbostic check(stdout);
94986Sbill }
95986Sbill }
96986Sbill
9752142Sbostic void
check(stream)9852142Sbostic check(stream)
9952142Sbostic FILE *stream;
100986Sbill {
10152142Sbostic if (feof(stream))
10252142Sbostic exit(0);
10352142Sbostic if (ferror(stream))
10452142Sbostic err("%s: %s",
10552142Sbostic stream == stdin ? "stdin" : "stdout", strerror(errno));
10652142Sbostic }
107986Sbill
10852142Sbostic void
usage()10952142Sbostic usage()
11052142Sbostic {
11152142Sbostic (void)fprintf(stderr, "usage: colrm [start [stop]]\n");
11252142Sbostic exit(1);
113986Sbill }
11452142Sbostic
11552142Sbostic #if __STDC__
11652142Sbostic #include <stdarg.h>
11752142Sbostic #else
11852142Sbostic #include <varargs.h>
11952142Sbostic #endif
12052142Sbostic
12152142Sbostic void
12252142Sbostic #if __STDC__
err(const char * fmt,...)12352142Sbostic err(const char *fmt, ...)
12452142Sbostic #else
12552142Sbostic err(fmt, va_alist)
12652142Sbostic char *fmt;
12752142Sbostic va_dcl
12852142Sbostic #endif
12952142Sbostic {
13052142Sbostic va_list ap;
13152142Sbostic #if __STDC__
13252142Sbostic va_start(ap, fmt);
13352142Sbostic #else
13452142Sbostic va_start(ap);
13552142Sbostic #endif
13652142Sbostic (void)fprintf(stderr, "colrm: ");
13752142Sbostic (void)vfprintf(stderr, fmt, ap);
13852142Sbostic va_end(ap);
13952142Sbostic (void)fprintf(stderr, "\n");
14052142Sbostic exit(1);
14152142Sbostic /* NOTREACHED */
14252142Sbostic }
143