Annotation of src/usr.bin/unexpand/unexpand.c, Revision 1.7
1.7 ! millert 1: /* $OpenBSD: unexpand.c,v 1.6 2003/04/05 20:30:45 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: unexpand.c,v 1.5 1994/12/24 17:08:05 cgd Exp $ */
3:
4: /*-
5: * Copyright (c) 1980, 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: #ifndef lint
34: static char copyright[] =
35: "@(#) Copyright (c) 1980, 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93";
42: #endif
1.7 ! millert 43: static char rcsid[] = "$OpenBSD: unexpand.c,v 1.6 2003/04/05 20:30:45 deraadt Exp $";
1.1 deraadt 44: #endif /* not lint */
45:
46: /*
47: * unexpand - put tabs into a file replacing blanks
48: */
49: #include <stdio.h>
50: #include <string.h>
51:
52: char genbuf[BUFSIZ];
53: char linebuf[BUFSIZ];
54: int all;
55:
1.5 millert 56: void tabify(char);
1.1 deraadt 57:
58: int
59: main(argc, argv)
60: int argc;
61: char *argv[];
62: {
1.4 mpech 63: char *cp;
1.1 deraadt 64:
65: argc--, argv++;
66: if (argc > 0 && argv[0][0] == '-') {
67: if (strcmp(argv[0], "-a") != 0) {
1.3 deraadt 68: fprintf(stderr, "usage: unexpand [-a] [file ...]\n");
1.1 deraadt 69: exit(1);
70: }
71: all++;
72: argc--, argv++;
73: }
74: do {
75: if (argc > 0) {
76: if (freopen(argv[0], "r", stdin) == NULL) {
77: perror(argv[0]);
78: exit(1);
79: }
80: argc--, argv++;
81: }
82: while (fgets(genbuf, BUFSIZ, stdin) != NULL) {
83: for (cp = linebuf; *cp; cp++)
84: continue;
85: if (cp > linebuf)
86: cp[-1] = 0;
87: tabify(all);
88: printf("%s", linebuf);
89: }
90: } while (argc > 0);
91: exit(0);
92: }
93:
94: void
95: tabify(c)
96: char c;
97: {
1.4 mpech 98: char *cp, *dp;
99: int dcol;
1.1 deraadt 100: int ocol;
1.6 deraadt 101: size_t len;
1.1 deraadt 102:
103: ocol = 0;
104: dcol = 0;
1.6 deraadt 105: cp = genbuf;
106: dp = linebuf;
107: len = sizeof linebuf;
108:
1.1 deraadt 109: for (;;) {
110: switch (*cp) {
111:
112: case ' ':
113: dcol++;
114: break;
115:
116: case '\t':
117: dcol += 8;
118: dcol &= ~07;
119: break;
120:
121: default:
122: while (((ocol + 8) &~ 07) <= dcol) {
123: if (ocol + 1 == dcol)
124: break;
1.6 deraadt 125: if (len > 1) {
126: *dp++ = '\t';
127: len--;
128: }
1.1 deraadt 129: ocol += 8;
130: ocol &= ~07;
131: }
132: while (ocol < dcol) {
1.6 deraadt 133: if (len > 1) {
134: *dp++ = ' ';
135: len--;
136: }
1.1 deraadt 137: ocol++;
138: }
139: if (*cp == 0 || c == 0) {
1.6 deraadt 140: strlcpy(dp, cp, len);
1.1 deraadt 141: return;
142: }
143: *dp++ = *cp;
1.6 deraadt 144: len--;
145: ocol++;
146: dcol++;
1.1 deraadt 147: }
148: cp++;
149: }
150: }