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@optushome.com.au>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <sys/types.h>
34 /* If nonzero, count bytes, not column positions. */
35 static int count_bytes;
37 /* Assuming the current column is COLUMN, return the column that
38 printing C will move the cursor to.
39 The first column is 0. */
41 static int adjust_column(int column, char c)
50 column = column + 8 - column % 8;
51 else /* if (isprint (c)) */
58 extern int fold_main(int argc, char **argv)
60 /* If nonzero, try to break on whitespace. */
63 /* If nonzero, at least one of the files we read was standard input. */
71 break_spaces = count_bytes = have_read_stdin = 0;
73 /* Turn any numeric options into -w options. */
74 for (i = 1; i < argc; i++) {
75 char const *a = argv[i];
78 if (a[1] == '-' && !a[2])
81 char *s = xmalloc(strlen(a) + 2);
91 while ((optc = getopt(argc, argv, "bsw:")) > 0) {
93 case 'b': /* Count bytes rather than columns. */
96 case 's': /* Break at word boundaries. */
99 case 'w': { /* Line width. */
100 width = bb_xgetlarg(optarg, 10, 1, 10000);
114 FILE *istream = bb_wfopen_input(*argv);
115 if (istream != NULL) {
117 int column = 0; /* Screen column where next char will go. */
118 int offset_out = 0; /* Index in `line_out' for next char. */
119 static char *line_out = NULL;
120 static int allocated_out = 0;
122 while ((c = getc(istream)) != EOF) {
123 if (offset_out + 1 >= allocated_out) {
124 allocated_out += 1024;
125 line_out = xrealloc(line_out, allocated_out);
129 line_out[offset_out++] = c;
130 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
131 column = offset_out = 0;
136 column = adjust_column(column, c);
138 if (column > width) {
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. */
143 /* Look for the last blank. */
146 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
147 if (isblank(line_out[logical_end])) {
151 if (logical_end >= 0) {
152 /* Found a blank. Don't output the part after it. */
154 fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
156 /* Move the remainder to the beginning of the next line.
157 The areas being copied here might overlap. */
158 memmove(line_out, line_out + logical_end, offset_out - logical_end);
159 offset_out -= logical_end;
160 for (column = i = 0; i < offset_out; i++) {
161 column = adjust_column(column, line_out[i]);
166 if (offset_out == 0) {
167 line_out[offset_out++] = c;
171 line_out[offset_out++] = '\n';
172 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
173 column = offset_out = 0;
177 line_out[offset_out++] = c;
181 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
184 if (ferror(istream) || bb_fclose_nonstdin(istream)) {
185 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
186 errs |= EXIT_FAILURE;
189 errs |= EXIT_FAILURE;
193 bb_fflush_stdout_and_exit(errs);