fold.c revision 1.10 1 /* $NetBSD: fold.c,v 1.10 2000/09/08 12:55:36 mjl Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kevin Ruddy.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
42 The Regents of the University of California. All rights reserved.\n");
43 #endif /* not lint */
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93";
48 #endif
49 __RCSID("$NetBSD: fold.c,v 1.10 2000/09/08 12:55:36 mjl Exp $");
50 #endif /* not lint */
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <err.h>
57
58 #define DEFLINEWIDTH 80
59
60 int main __P((int, char **));
61 static void fold __P((int));
62 static int new_column_position __P((int, int));
63 static void usage(void);
64
65 int count_bytes = 0;
66 int split_words = 0;
67
68 int
69 main(argc, argv)
70 int argc;
71 char **argv;
72 {
73 int ch;
74 int width;
75 char *p;
76
77 width = -1;
78 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
79 switch (ch) {
80 case 'b':
81 count_bytes = 1;
82 break;
83 case 's':
84 split_words = 1;
85 break;
86 case 'w':
87 if ((width = atoi(optarg)) <= 0)
88 errx(1, "illegal width value");
89 break;
90 case '0': case '1': case '2': case '3': case '4':
91 case '5': case '6': case '7': case '8': case '9':
92 if (width == -1) {
93 p = argv[optind - 1];
94 if (p[0] == '-' && p[1] == ch && !p[2])
95 width = atoi(++p);
96 else
97 width = atoi(argv[optind] + 1);
98 }
99 break;
100 default:
101 usage();
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)) {
113 err (1, "%s", *argv);
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)
133 int width;
134 {
135 static char *buf = NULL;
136 static int buf_max = 0;
137 int ch, col;
138 int indx;
139
140 col = indx = 0;
141 while ((ch = getchar()) != EOF) {
142 if (ch == '\n') {
143 if (indx != 0)
144 fwrite (buf, 1, indx, stdout);
145 putchar('\n');
146 col = indx = 0;
147 continue;
148 }
149
150 col = new_column_position (col, ch);
151 if (col > width) {
152 int i, last_space;
153
154 #ifdef __GNUC__
155 last_space = 0; /* XXX gcc */
156 #endif
157 if (split_words) {
158 for (i = 0, last_space = -1; i < indx; i++)
159 if (buf[i] == ' ')
160 last_space = i;
161 }
162
163 if (split_words && last_space != -1) {
164 last_space++;
165
166 fwrite (buf, 1, last_space, stdout);
167 memmove (buf, buf+last_space, indx-last_space);
168
169 indx -= last_space;
170 col = 0;
171 for (i = 0; i < indx; i++) {
172 col = new_column_position (col, buf[i]);
173 }
174 } else {
175 fwrite (buf, 1, indx, stdout);
176 col = indx = 0;
177 }
178 putchar('\n');
179
180 /* calculate the column position for the next line. */
181 col = new_column_position (col, ch);
182 }
183
184 if (indx + 1 > buf_max) {
185 /* Allocate buffer in LINE_MAX increments */
186 buf_max += 2048;
187 if((buf = realloc (buf, buf_max)) == NULL) {
188 err (1, "realloc");
189 /* NOTREACHED */
190 }
191 }
192 buf[indx++] = ch;
193 }
194
195 if (indx != 0)
196 fwrite (buf, 1, indx, stdout);
197 }
198
199 /*
200 * calculate the column position
201 */
202 static int
203 new_column_position (col, ch)
204 int col;
205 int ch;
206 {
207 if (!count_bytes) {
208 switch (ch) {
209 case '\b':
210 if (col > 0)
211 --col;
212 break;
213 case '\r':
214 col = 0;
215 break;
216 case '\t':
217 col = (col + 8) & ~7;
218 break;
219 default:
220 ++col;
221 break;
222 }
223 } else {
224 ++col;
225 }
226
227 return col;
228 }
229
230 static void usage(void)
231 {
232 (void)fprintf(stderr,
233 "usage: fold [-bs] [-w width] [file ...]\n");
234 exit(1);
235 }
236
237