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
45 #define DEFLINEWIDTH 80
48 static int newpos(int, wint_t);
49 static void usage(void) __dead2;
51 static int bflag; /* Count bytes, not columns */
52 static int sflag; /* Split on word boundaries */
55 main(int argc, char **argv)
60 (void) setlocale(LC_CTYPE, "");
64 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) {
73 if ((width = atoi(optarg)) <= 0) {
74 errx(1, "illegal width value");
77 case '0': case '1': case '2': case '3': case '4':
78 case '5': case '6': case '7': case '8': case '9':
79 /* Accept a width as eg. -30. Note that a width
80 * specified using the -w option is always used prior
81 * to this undocumented option. */
82 switch (previous_ch) {
83 case '0': case '1': case '2': case '3': case '4':
84 case '5': case '6': case '7': case '8': case '9':
85 /* The width is a number with multiple digits:
86 * add the last one. */
87 width = width * 10 + (ch - '0');
90 /* Set the width, unless it was previously
91 * set. For instance, the following options
92 * would all give a width of 5 and not 10:
110 width = DEFLINEWIDTH;
114 else for (; *argv; ++argv)
115 if (!freopen(*argv, "r", stdin)) {
126 (void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n");
131 * Fold the contents of standard input to fit within WIDTH columns (or bytes)
132 * and write to standard output.
134 * If sflag is set, split the line at the last space character on the line.
135 * This flag necessitates storing the line in a buffer until the current
136 * column > width, or a newline or EOF is read.
138 * The buffer can grow larger than WIDTH due to backspaces and carriage
139 * returns embedded in the input stream.
146 int col, i, indx, space;
150 while ((ch = getwchar()) != WEOF) {
152 wprintf(L"%.*ls\n", indx, buf);
156 if ((col = newpos(col, ch)) > width) {
159 while (--i >= 0 && !iswblank(buf[i]))
163 if (sflag && space != -1) {
165 wprintf(L"%.*ls\n", space, buf);
166 wmemmove(buf, buf + space, indx - space);
169 for (i = 0; i < indx; i++)
170 col = newpos(col, buf[i]);
172 wprintf(L"%.*ls\n", indx, buf);
175 col = newpos(col, ch);
177 if (indx + 1 > buf_max) {
179 buf = realloc(buf, sizeof(*buf) * buf_max);
187 wprintf(L"%.*ls", indx, buf);
191 * Update the current column position for a character.
194 newpos(int col, wint_t ch)
196 char buf[MB_LEN_MAX];
201 len = wcrtomb(buf, ch, NULL);
213 col = (col + 8) & ~7;
216 if ((w = wcwidth(ch)) > 0)