Annotation of src/usr.bin/fold/fold.c, Revision 1.6
1.6 ! deraadt 1: /* $OpenBSD: fold.c,v 1.6 2002/06/17 06:56:18 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: fold.c,v 1.6 1995/09/01 01:42:44 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1990, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Kevin Ruddy.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the University of
22: * California, Berkeley and its contributors.
23: * 4. Neither the name of the University nor the names of its contributors
24: * may be used to endorse or promote products derived from this software
25: * without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37: * SUCH DAMAGE.
38: */
39:
40: #ifndef lint
41: static char copyright[] =
42: "@(#) Copyright (c) 1990, 1993\n\
43: The Regents of the University of California. All rights reserved.\n";
44: #endif /* not lint */
45:
46: #ifndef lint
47: #if 0
48: static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93";
49: #endif
1.6 ! deraadt 50: static char rcsid[] = "$OpenBSD: fold.c,v 1.6 2002/06/17 06:56:18 deraadt Exp $";
1.1 deraadt 51: #endif /* not lint */
52:
53: #include <stdio.h>
54: #include <stdlib.h>
55: #include <string.h>
56: #include <unistd.h>
57: #include <err.h>
58:
59: #define DEFLINEWIDTH 80
60:
61: static void fold ();
62: static int new_column_position ();
63: int count_bytes = 0;
64: int split_words = 0;
65:
66: int
67: main(argc, argv)
68: int argc;
69: char **argv;
70: {
1.5 mpech 71: int ch;
1.1 deraadt 72: int width;
73: char *p;
74:
75: width = -1;
76: while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
77: switch (ch) {
78: case 'b':
79: count_bytes = 1;
80: break;
81: case 's':
82: split_words = 1;
83: break;
84: case 'w':
1.4 pjanzen 85: if ((width = atoi(optarg)) <= 0)
86: errx(1, "illegal width value.");
1.1 deraadt 87: break;
88: case '0': case '1': case '2': case '3': case '4':
89: case '5': case '6': case '7': case '8': case '9':
90: if (width == -1) {
91: p = argv[optind - 1];
92: if (p[0] == '-' && p[1] == ch && !p[2])
93: width = atoi(++p);
94: else
95: width = atoi(argv[optind] + 1);
96: }
97: break;
98: default:
99: (void)fprintf(stderr,
100: "usage: fold [-bs] [-w width] [file ...]\n");
101: exit(1);
102: }
103: argv += optind;
104: argc -= optind;
105:
106: if (width == -1)
107: width = DEFLINEWIDTH;
108:
109: if (!*argv)
110: fold(width);
111: else for (; *argv; ++argv)
112: if (!freopen(*argv, "r", stdin)) {
1.6 ! deraadt 113: err(1, "%s", *argv);
1.1 deraadt 114: /* NOTREACHED */
115: } else
116: fold(width);
117: exit(0);
118: }
119:
120: /*
121: * Fold the contents of standard input to fit within WIDTH columns
122: * (or bytes) and write to standard output.
123: *
124: * If split_words is set, split the line at the last space character
125: * on the line. This flag necessitates storing the line in a buffer
126: * until the current column > width, or a newline or EOF is read.
127: *
128: * The buffer can grow larger than WIDTH due to backspaces and carriage
129: * returns embedded in the input stream.
130: */
131: static void
132: fold(width)
1.5 mpech 133: int width;
1.1 deraadt 134: {
135: static char *buf = NULL;
136: static int buf_max = 0;
1.5 mpech 137: int ch, col;
138: int indx;
1.1 deraadt 139:
140: col = indx = 0;
141: while ((ch = getchar()) != EOF) {
142: if (ch == '\n') {
143: if (indx != 0)
1.6 ! deraadt 144: fwrite(buf, 1, indx, stdout);
1.1 deraadt 145: putchar('\n');
146: col = indx = 0;
147: continue;
148: }
149:
1.6 ! deraadt 150: col = new_column_position(col, ch);
1.1 deraadt 151: if (col > width) {
152: int i, last_space;
153:
154: if (split_words) {
155: for (i = 0, last_space = -1; i < indx; i++)
1.6 ! deraadt 156: if(buf[i] == ' ')
! 157: last_space = i;
1.1 deraadt 158: }
159:
160: if (split_words && last_space != -1) {
161: last_space++;
162:
1.6 ! deraadt 163: fwrite(buf, 1, last_space, stdout);
! 164: memmove(buf, buf+last_space, indx-last_space);
1.1 deraadt 165:
166: indx -= last_space;
167: col = 0;
168: for (i = 0; i < indx; i++) {
1.6 ! deraadt 169: col = new_column_position(col, buf[i]);
1.1 deraadt 170: }
171: } else {
1.6 ! deraadt 172: fwrite(buf, 1, indx, stdout);
1.1 deraadt 173: col = indx = 0;
174: }
175: putchar('\n');
176:
177: /* calculate the column position for the next line. */
1.6 ! deraadt 178: col = new_column_position(col, ch);
1.1 deraadt 179: }
180:
181: if (indx + 1 > buf_max) {
182: /* Allocate buffer in LINE_MAX increments */
183: buf_max += 2048;
1.6 ! deraadt 184: if((buf = realloc(buf, buf_max)) == NULL) {
! 185: err(1, NULL);
1.1 deraadt 186: /* NOTREACHED */
187: }
188: }
189: buf[indx++] = ch;
190: }
191:
192: if (indx != 0)
1.6 ! deraadt 193: fwrite(buf, 1, indx, stdout);
1.1 deraadt 194: }
195:
196: /*
197: * calculate the column position
198: */
199: static int
1.6 ! deraadt 200: new_column_position(col, ch)
1.1 deraadt 201: int col;
202: int ch;
203: {
204: if (!count_bytes) {
205: switch (ch) {
206: case '\b':
207: if (col > 0)
208: --col;
209: break;
210: case '\r':
211: col = 0;
212: break;
213: case '\t':
214: col = (col + 8) & ~7;
215: break;
216: default:
217: ++col;
218: break;
219: }
220: } else {
221: ++col;
222: }
223:
224: return col;
225: }