1 /* vi: set sw=4 ts=4: */
3 * fold -- wrap each input line to fit in specified width.
5 * Written by David MacKenzie, djm@gnu.ai.mit.edu.
6 * Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
8 * Modified for busybox based on coreutils v 5.0
9 * Copyright (C) 2003 Glenn McGrath
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
14 //config: bool "fold (4.8 kb)"
17 //config: Wrap text to fit a specific width.
19 //applet:IF_FOLD(APPLET_NOEXEC(fold, fold, BB_DIR_USR_BIN, BB_SUID_DROP, fold))
21 //kbuild:lib-$(CONFIG_FOLD) += fold.o
23 //usage:#define fold_trivial_usage
24 //usage: "[-bs] [-w WIDTH] [FILE]..."
25 //usage:#define fold_full_usage "\n\n"
26 //usage: "Wrap input lines in FILEs (or stdin), writing to stdout\n"
27 //usage: "\n -b Count bytes rather than columns"
28 //usage: "\n -s Break at spaces"
29 //usage: "\n -w Use WIDTH columns instead of 80"
34 /* This is a NOEXEC applet. Be very careful! */
36 /* Must match getopt32 call */
37 #define FLAG_COUNT_BYTES 1
38 #define FLAG_BREAK_SPACES 2
41 /* Assuming the current column is COLUMN, return the column that
42 printing C will move the cursor to.
43 The first column is 0. */
44 static int adjust_column(unsigned column
, char c
)
46 if (option_mask32
& FLAG_COUNT_BYTES
)
50 return column
+ 8 - column
% 8;
53 if ((int)--column
< 0)
58 else { /* just a printable char */
59 if (unicode_status
!= UNICODE_ON
/* every byte is a new char */
60 || (c
& 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
68 /* Note that this function can write NULs, unlike fputs etc. */
69 static void write2stdout(const void *buf
, unsigned size
)
71 fwrite(buf
, 1, size
, stdout
);
74 int fold_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
75 int fold_main(int argc UNUSED_PARAM
, char **argv
)
77 char *line_out
= NULL
;
78 const char *w_opt
= "80";
80 exitcode_t exitcode
= EXIT_SUCCESS
;
84 if (ENABLE_INCLUDE_SUSv2
) {
85 /* Turn any numeric options into -w options. */
87 for (i
= 1; argv
[i
]; i
++) {
88 const char *a
= argv
[i
];
91 if (*a
== '-' && !a
[1]) /* "--" */
94 argv
[i
] = xasprintf("-w%s", a
);
99 getopt32(argv
, "bsw:", &w_opt
);
100 width
= xatou_range(w_opt
, 1, 10000);
104 *--argv
= (char*)"-";
107 FILE *istream
= fopen_or_warn_stdin(*argv
);
109 unsigned column
= 0; /* Screen column where next char will go */
110 unsigned offset_out
= 0; /* Index in 'line_out' for next char */
112 if (istream
== NULL
) {
113 exitcode
= EXIT_FAILURE
;
117 while ((c
= getc(istream
)) != EOF
) {
118 /* We grow line_out in chunks of 0x1000 bytes */
119 if ((offset_out
& 0xfff) == 0) {
120 line_out
= xrealloc(line_out
, offset_out
+ 0x1000);
123 line_out
[offset_out
] = c
;
125 write2stdout(line_out
, offset_out
+ 1);
126 column
= offset_out
= 0;
129 column
= adjust_column(column
, c
);
130 if (column
<= width
|| offset_out
== 0) {
131 /* offset_out == 0 case happens
132 * with small width (say, 1) and tabs.
133 * The very first tab already goes to column 8,
134 * but we must not wrap it */
139 /* This character would make the line too long.
140 * Print the line plus a newline, and make this character
141 * start the next line */
142 if (option_mask32
& FLAG_BREAK_SPACES
) {
144 unsigned logical_end
;
146 /* Look for the last blank. */
147 for (logical_end
= offset_out
- 1; (int)logical_end
>= 0; logical_end
--) {
148 if (!isblank(line_out
[logical_end
]))
151 /* Found a space or tab.
152 * Output up to and including it, and start a new line */
154 /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
155 write2stdout(line_out
, logical_end
);
157 /* Move the remainder to the beginning of the next line.
158 * The areas being copied here might overlap. */
159 memmove(line_out
, line_out
+ logical_end
, offset_out
- logical_end
);
160 offset_out
-= logical_end
;
161 for (column
= i
= 0; i
< offset_out
; i
++) {
162 column
= adjust_column(column
, line_out
[i
]);
166 /* No blank found, wrap will split the overlong word */
168 /* Output what we accumulated up to now, and start a new line */
169 line_out
[offset_out
] = '\n';
170 write2stdout(line_out
, offset_out
+ 1);
171 column
= offset_out
= 0;
173 } /* while (not EOF) */
176 write2stdout(line_out
, offset_out
);
179 if (fclose_if_not_stdin(istream
)) {
180 bb_simple_perror_msg(*argv
);
181 exitcode
= EXIT_FAILURE
;
185 fflush_stdout_and_exit(exitcode
);