1 /* $NetBSD: fold.c,v 1.14 2008/07/21 14:19:22 lukem Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93";
45 __RCSID("$NetBSD: fold.c,v 1.14 2008/07/21 14:19:22 lukem Exp $");
54 #define DEFLINEWIDTH 80
56 int main(int, char **);
57 static void fold(int);
58 static int new_column_position(int, int);
59 static void usage(void);
65 main(int argc
, char **argv
)
72 while ((ch
= getopt(argc
, argv
, "0123456789bsw:")) != -1)
81 if ((width
= atoi(optarg
)) <= 0)
82 errx(1, "illegal width value");
84 case '0': case '1': case '2': case '3': case '4':
85 case '5': case '6': case '7': case '8': case '9':
88 if (p
[0] == '-' && p
[1] == ch
&& !p
[2])
91 width
= atoi(argv
[optind
] + 1);
101 width
= DEFLINEWIDTH
;
105 else for (; *argv
; ++argv
)
106 if (!freopen(*argv
, "r", stdin
)) {
107 err (1, "%s", *argv
);
115 * Fold the contents of standard input to fit within WIDTH columns
116 * (or bytes) and write to standard output.
118 * If split_words is set, split the line at the last space character
119 * on the line. This flag necessitates storing the line in a buffer
120 * until the current column > width, or a newline or EOF is read.
122 * The buffer can grow larger than WIDTH due to backspaces and carriage
123 * returns embedded in the input stream.
128 static char *buf
= NULL
;
130 static int buf_max
= 0;
135 while ((ch
= getchar()) != EOF
) {
138 fwrite (buf
, 1, indx
, stdout
);
144 col
= new_column_position (col
, ch
);
149 last_space
= 0; /* XXX gcc */
152 for (i
= 0, last_space
= -1; i
< indx
; i
++)
157 if (split_words
&& last_space
!= -1) {
158 fwrite (buf
, 1, last_space
, stdout
);
160 /* increase last_space here, so we skip trailing whitespace */
162 memmove (buf
, buf
+last_space
, indx
-last_space
);
166 for (i
= 0; i
< indx
; i
++) {
167 col
= new_column_position (col
, buf
[i
]);
170 fwrite (buf
, 1, indx
, stdout
);
175 /* calculate the column position for the next line. */
176 col
= new_column_position (col
, ch
);
179 if (indx
+ 1 > buf_max
) {
180 /* Allocate buffer in LINE_MAX increments */
181 if ((nbuf
= realloc (buf
, buf_max
+ 2048)) == NULL
) {
192 fwrite (buf
, 1, indx
, stdout
);
196 * calculate the column position
199 new_column_position (int col
, int ch
)
211 col
= (col
+ 8) & ~7;
227 (void)fprintf(stderr
,
228 "usage: fold [-bs] [-w width] [file ...]\n");