1 /* vi: set sw=4 ts=4: */
2 /* fold -- wrap each input line to fit in specified width.
4 Written by David MacKenzie, djm@gnu.ai.mit.edu.
5 Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
7 Modified for busybox based on coreutils v 5.0
8 Copyright (C) 2003 Glenn McGrath
10 Licensed under GPLv2 or later, see file LICENSE in this source tree.
13 //usage:#define fold_trivial_usage
14 //usage: "[-bs] [-w WIDTH] [FILE]..."
15 //usage:#define fold_full_usage "\n\n"
16 //usage: "Wrap input lines in each FILE (or stdin), writing to stdout\n"
18 //usage: "\n -b Count bytes rather than columns"
19 //usage: "\n -s Break at spaces"
20 //usage: "\n -w Use WIDTH columns instead of 80"
25 /* This is a NOEXEC applet. Be very careful! */
27 /* Must match getopt32 call */
28 #define FLAG_COUNT_BYTES 1
29 #define FLAG_BREAK_SPACES 2
32 /* Assuming the current column is COLUMN, return the column that
33 printing C will move the cursor to.
34 The first column is 0. */
35 static int adjust_column(unsigned column, char c)
37 if (option_mask32 & FLAG_COUNT_BYTES)
41 return column + 8 - column % 8;
44 if ((int)--column < 0)
49 else { /* just a printable char */
50 if (unicode_status != UNICODE_ON /* every byte is a new char */
51 || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
59 /* Note that this function can write NULs, unlike fputs etc. */
60 static void write2stdout(const void *buf, unsigned size)
62 fwrite(buf, 1, size, stdout);
65 int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
66 int fold_main(int argc UNUSED_PARAM, char **argv)
68 char *line_out = NULL;
69 const char *w_opt = "80";
71 smallint exitcode = EXIT_SUCCESS;
75 if (ENABLE_INCLUDE_SUSv2) {
76 /* Turn any numeric options into -w options. */
78 for (i = 1; argv[i]; i++) {
79 const char *a = argv[i];
82 if (*a == '-' && !a[1]) /* "--" */
85 argv[i] = xasprintf("-w%s", a);
90 getopt32(argv, "bsw:", &w_opt);
91 width = xatou_range(w_opt, 1, 10000);
98 FILE *istream = fopen_or_warn_stdin(*argv);
100 unsigned column = 0; /* Screen column where next char will go */
101 unsigned offset_out = 0; /* Index in 'line_out' for next char */
103 if (istream == NULL) {
104 exitcode = EXIT_FAILURE;
108 while ((c = getc(istream)) != EOF) {
109 /* We grow line_out in chunks of 0x1000 bytes */
110 if ((offset_out & 0xfff) == 0) {
111 line_out = xrealloc(line_out, offset_out + 0x1000);
114 line_out[offset_out] = c;
116 write2stdout(line_out, offset_out + 1);
117 column = offset_out = 0;
120 column = adjust_column(column, c);
121 if (column <= width || offset_out == 0) {
122 /* offset_out == 0 case happens
123 * with small width (say, 1) and tabs.
124 * The very first tab already goes to column 8,
125 * but we must not wrap it */
130 /* This character would make the line too long.
131 * Print the line plus a newline, and make this character
132 * start the next line */
133 if (option_mask32 & FLAG_BREAK_SPACES) {
135 unsigned logical_end;
137 /* Look for the last blank. */
138 for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
139 if (!isblank(line_out[logical_end]))
142 /* Found a space or tab.
143 * Output up to and including it, and start a new line */
145 /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
146 write2stdout(line_out, logical_end);
148 /* Move the remainder to the beginning of the next line.
149 * The areas being copied here might overlap. */
150 memmove(line_out, line_out + logical_end, offset_out - logical_end);
151 offset_out -= logical_end;
152 for (column = i = 0; i < offset_out; i++) {
153 column = adjust_column(column, line_out[i]);
157 /* No blank found, wrap will split the overlong word */
159 /* Output what we accumulated up to now, and start a new line */
160 line_out[offset_out] = '\n';
161 write2stdout(line_out, offset_out + 1);
162 column = offset_out = 0;
164 } /* while (not EOF) */
167 write2stdout(line_out, offset_out);
170 if (fclose_if_not_stdin(istream)) {
171 bb_simple_perror_msg(*argv);
172 exitcode = EXIT_FAILURE;
176 fflush_stdout_and_exit(exitcode);