1 /* $NetBSD: fold.c,v 1.17 2011/09/04 20:24:59 joerg 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.17 2011/09/04 20:24:59 joerg Exp $");
56 #define DEFLINEWIDTH 80
58 static void fold(int);
59 static int new_column_position(int, wint_t);
60 __dead
static void usage(void);
62 static int count_bytes
= 0;
63 static int split_words
= 0;
66 main(int argc
, char **argv
)
72 setlocale(LC_CTYPE
, "");
76 while ((ch
= getopt(argc
, argv
, "0123456789bsw:")) != -1)
85 if ((width
= atoi(optarg
)) <= 0)
86 errx(1, "illegal width value");
88 case '0': case '1': case '2': case '3': case '4':
89 case '5': case '6': case '7': case '8': case '9':
92 if (p
[0] == '-' && p
[1] == ch
&& !p
[2])
95 width
= atoi(argv
[optind
] + 1);
105 width
= DEFLINEWIDTH
;
109 else for (; *argv
; ++argv
)
110 if (!freopen(*argv
, "r", stdin
)) {
111 err (1, "%s", *argv
);
119 * Fold the contents of standard input to fit within WIDTH columns
120 * (or bytes) and write to standard output.
122 * If split_words is set, split the line at the last space character
123 * on the line. This flag necessitates storing the line in a buffer
124 * until the current column > width, or a newline or EOF is read.
126 * The buffer can grow larger than WIDTH due to backspaces and carriage
127 * returns embedded in the input stream.
132 static wchar_t *buf
= NULL
;
134 static int buf_max
= 0;
139 while ((ch
= getwchar()) != WEOF
) {
142 for (i
= 0; i
< indx
; i
++)
150 col
= new_column_position (col
, ch
);
155 last_space
= 0; /* XXX gcc */
158 for (i
= 0, last_space
= -1; i
< indx
; i
++)
163 if (split_words
&& last_space
!= -1) {
164 for (i
= 0; i
< last_space
; i
++)
167 /* increase last_space here, so we skip trailing whitespace */
169 wmemmove (buf
, buf
+last_space
, indx
-last_space
);
173 for (i
= 0; i
< indx
; i
++) {
174 col
= new_column_position (col
, buf
[i
]);
177 for (i
= 0; i
< indx
; i
++)
183 /* calculate the column position for the next line. */
184 col
= new_column_position (col
, ch
);
187 if (indx
+ 1 > buf_max
) {
188 /* Allocate buffer in LINE_MAX increments */
189 if ((nbuf
= realloc (buf
, buf_max
+ 2048)) == NULL
) {
200 for (i
= 0; i
< indx
; i
++)
206 * calculate the column position
209 new_column_position (int col
, wint_t ch
)
223 col
= (col
+ 8) & ~7;
232 char dummy
[MB_LEN_MAX
];
234 /* XXX: we assume stateless encoding */
235 col
+= wcrtomb(dummy
, ch
, NULL
);
244 (void)fprintf(stderr
,
245 "usage: %s [-bs] [-w width] [file ...]\n", getprogname());