1 /* fold -- wrap each input line to fit in specified width.
3 Written by David MacKenzie, djm@gnu.ai.mit.edu.
4 Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
6 Modified for busybox based on coreutils v 5.0
7 Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
9 Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
17 #include <sys/types.h>
21 /* If nonzero, count bytes, not column positions. */
22 static unsigned long flags;
23 #define FLAG_COUNT_BYTES 1
24 #define FLAG_BREAK_SPACES 2
27 /* Assuming the current column is COLUMN, return the column that
28 printing C will move the cursor to.
29 The first column is 0. */
31 static int adjust_column(int column, char c)
33 if (!(flags & FLAG_COUNT_BYTES)) {
40 column = column + 8 - column % 8;
41 else /* if (isprint (c)) */
48 extern int fold_main(int argc, char **argv)
56 #ifdef CONFIG_FEATURE_SUSv2_OBSOLETE
57 /* Turn any numeric options into -w options. */
58 for (i = 1; i < argc; i++) {
59 char const *a = argv[i];
62 if (a[1] == '-' && !a[2])
65 char *s = xmalloc(strlen(a) + 2);
76 flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt);
77 if (flags & FLAG_WIDTH)
78 width = bb_xgetlarg(w_opt, 10, 1, 10000);
86 FILE *istream = bb_wfopen_input(*argv);
87 if (istream != NULL) {
89 int column = 0; /* Screen column where next char will go. */
90 int offset_out = 0; /* Index in `line_out' for next char. */
91 static char *line_out = NULL;
92 static int allocated_out = 0;
94 while ((c = getc(istream)) != EOF) {
95 if (offset_out + 1 >= allocated_out) {
96 allocated_out += 1024;
97 line_out = xrealloc(line_out, allocated_out);
101 line_out[offset_out++] = c;
102 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
103 column = offset_out = 0;
108 column = adjust_column(column, c);
110 if (column > width) {
111 /* This character would make the line too long.
112 Print the line plus a newline, and make this character
113 start the next line. */
114 if (flags & FLAG_BREAK_SPACES) {
115 /* Look for the last blank. */
118 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
119 if (isblank(line_out[logical_end])) {
123 if (logical_end >= 0) {
124 /* Found a blank. Don't output the part after it. */
126 fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
128 /* Move the remainder to the beginning of the next line.
129 The areas being copied here might overlap. */
130 memmove(line_out, line_out + logical_end, offset_out - logical_end);
131 offset_out -= logical_end;
132 for (column = i = 0; i < offset_out; i++) {
133 column = adjust_column(column, line_out[i]);
138 if (offset_out == 0) {
139 line_out[offset_out++] = c;
143 line_out[offset_out++] = '\n';
144 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
145 column = offset_out = 0;
149 line_out[offset_out++] = c;
153 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
156 if (ferror(istream) || bb_fclose_nonstdin(istream)) {
157 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
158 errs |= EXIT_FAILURE;
161 errs |= EXIT_FAILURE;
165 bb_fflush_stdout_and_exit(errs);
167 /* vi: set sw=4 ts=4: */