1 /* $NetBSD: fold.c,v 1.15 2008/10/29 01:31:09 ahoka 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.15 2008/10/29 01:31:09 ahoka Exp $");
56 #define DEFLINEWIDTH 80
58 int main(int, char **);
59 static void fold(int);
60 static int new_column_position(int, wint_t);
61 static void usage(void);
67 main(int argc
, char **argv
)
73 setlocale(LC_CTYPE
, "");
77 while ((ch
= getopt(argc
, argv
, "0123456789bsw:")) != -1)
86 if ((width
= atoi(optarg
)) <= 0)
87 errx(1, "illegal width value");
89 case '0': case '1': case '2': case '3': case '4':
90 case '5': case '6': case '7': case '8': case '9':
93 if (p
[0] == '-' && p
[1] == ch
&& !p
[2])
96 width
= atoi(argv
[optind
] + 1);
106 width
= DEFLINEWIDTH
;
110 else for (; *argv
; ++argv
)
111 if (!freopen(*argv
, "r", stdin
)) {
112 err (1, "%s", *argv
);
120 * Fold the contents of standard input to fit within WIDTH columns
121 * (or bytes) and write to standard output.
123 * If split_words is set, split the line at the last space character
124 * on the line. This flag necessitates storing the line in a buffer
125 * until the current column > width, or a newline or EOF is read.
127 * The buffer can grow larger than WIDTH due to backspaces and carriage
128 * returns embedded in the input stream.
133 static wchar_t *buf
= NULL
;
135 static int buf_max
= 0;
140 while ((ch
= getwchar()) != WEOF
) {
143 for (i
= 0; i
< indx
; i
++)
151 col
= new_column_position (col
, ch
);
156 last_space
= 0; /* XXX gcc */
159 for (i
= 0, last_space
= -1; i
< indx
; i
++)
164 if (split_words
&& last_space
!= -1) {
165 for (i
= 0; i
< last_space
; i
++)
168 /* increase last_space here, so we skip trailing whitespace */
170 wmemmove (buf
, buf
+last_space
, indx
-last_space
);
174 for (i
= 0; i
< indx
; i
++) {
175 col
= new_column_position (col
, buf
[i
]);
178 for (i
= 0; i
< indx
; i
++)
184 /* calculate the column position for the next line. */
185 col
= new_column_position (col
, ch
);
188 if (indx
+ 1 > buf_max
) {
189 /* Allocate buffer in LINE_MAX increments */
190 if ((nbuf
= realloc (buf
, buf_max
+ 2048)) == NULL
) {
201 for (i
= 0; i
< indx
; i
++)
207 * calculate the column position
210 new_column_position (int col
, wint_t ch
)
224 col
= (col
+ 8) & ~7;
233 char dummy
[MB_LEN_MAX
];
235 /* XXX: we assume stateless encoding */
236 col
+= wcrtomb(dummy
, ch
, NULL
);
245 (void)fprintf(stderr
,
246 "usage: %s [-bs] [-w width] [file ...]\n", getprogname());