2 * SPDX-License-Identifier: BSD-3-Clause
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
36 static const char copyright[] =
37 "@(#) Copyright (c) 1990, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93";
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
60 #define DEFLINEWIDTH 80
63 static int newpos(int, wint_t);
64 static void usage(void);
66 static int bflag; /* Count bytes, not columns */
67 static int sflag; /* Split on word boundaries */
70 main(int argc, char **argv)
75 (void) setlocale(LC_CTYPE, "");
79 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) {
88 if ((width = atoi(optarg)) <= 0) {
89 errx(1, "illegal width value");
92 case '0': case '1': case '2': case '3': case '4':
93 case '5': case '6': case '7': case '8': case '9':
94 /* Accept a width as eg. -30. Note that a width
95 * specified using the -w option is always used prior
96 * to this undocumented option. */
97 switch (previous_ch) {
98 case '0': case '1': case '2': case '3': case '4':
99 case '5': case '6': case '7': case '8': case '9':
100 /* The width is a number with multiple digits:
101 * add the last one. */
102 width = width * 10 + (ch - '0');
105 /* Set the width, unless it was previously
106 * set. For instance, the following options
107 * would all give a width of 5 and not 10:
125 width = DEFLINEWIDTH;
129 else for (; *argv; ++argv)
130 if (!freopen(*argv, "r", stdin)) {
141 (void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n");
146 * Fold the contents of standard input to fit within WIDTH columns (or bytes)
147 * and write to standard output.
149 * If sflag is set, split the line at the last space character on the line.
150 * This flag necessitates storing the line in a buffer until the current
151 * column > width, or a newline or EOF is read.
153 * The buffer can grow larger than WIDTH due to backspaces and carriage
154 * returns embedded in the input stream.
161 int col, i, indx, space;
165 while ((ch = getwchar()) != WEOF) {
167 wprintf(L"%.*ls\n", indx, buf);
171 if ((col = newpos(col, ch)) > width) {
174 while (--i >= 0 && !iswblank(buf[i]))
178 if (sflag && space != -1) {
180 wprintf(L"%.*ls\n", space, buf);
181 wmemmove(buf, buf + space, indx - space);
184 for (i = 0; i < indx; i++)
185 col = newpos(col, buf[i]);
187 wprintf(L"%.*ls\n", indx, buf);
190 col = newpos(col, ch);
192 if (indx + 1 > buf_max) {
194 buf = realloc(buf, sizeof(*buf) * buf_max);
202 wprintf(L"%.*ls", indx, buf);
206 * Update the current column position for a character.
209 newpos(int col, wint_t ch)
211 char buf[MB_LEN_MAX];
216 len = wcrtomb(buf, ch, NULL);
228 col = (col + 8) & ~7;
231 if ((w = wcwidth(ch)) > 0)