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 static unsigned long flags;
22 #define FLAG_COUNT_BYTES 1
23 #define FLAG_BREAK_SPACES 2
26 /* Assuming the current column is COLUMN, return the column that
27 printing C will move the cursor to.
28 The first column is 0. */
30 static int adjust_column(int column, char c)
32 if (!(flags & FLAG_COUNT_BYTES)) {
39 column = column + 8 - column % 8;
40 else /* if (isprint (c)) */
47 extern int fold_main(int argc, char **argv)
54 if(!ENABLE_DEBUG_YANK_SUSv2) {
55 /* Turn any numeric options into -w options. */
56 for (i = 1; i < argc; i++) {
57 char const *a = argv[i];
60 if (*a == '-' && !a[1])
63 argv[i] = bb_xasprintf("-w%s", a);
69 flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt);
70 if (flags & FLAG_WIDTH)
71 width = bb_xgetlarg(w_opt, 10, 1, 10000);
79 FILE *istream = bb_wfopen_input(*argv);
81 int column = 0; /* Screen column where next char will go. */
82 int offset_out = 0; /* Index in `line_out' for next char. */
83 static char *line_out = NULL;
84 static int allocated_out = 0;
86 if (istream == NULL) {
91 while ((c = getc(istream)) != EOF) {
92 if (offset_out + 1 >= allocated_out) {
93 allocated_out += 1024;
94 line_out = xrealloc(line_out, allocated_out);
98 line_out[offset_out++] = c;
99 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
100 column = offset_out = 0;
105 column = adjust_column(column, c);
107 if (column > width) {
108 /* This character would make the line too long.
109 Print the line plus a newline, and make this character
110 start the next line. */
111 if (flags & FLAG_BREAK_SPACES) {
112 /* Look for the last blank. */
115 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
116 if (isblank(line_out[logical_end])) {
120 if (logical_end >= 0) {
121 /* Found a blank. Don't output the part after it. */
123 fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
125 /* Move the remainder to the beginning of the next line.
126 The areas being copied here might overlap. */
127 memmove(line_out, line_out + logical_end, offset_out - logical_end);
128 offset_out -= logical_end;
129 for (column = i = 0; i < offset_out; i++) {
130 column = adjust_column(column, line_out[i]);
135 if (offset_out == 0) {
136 line_out[offset_out++] = c;
140 line_out[offset_out++] = '\n';
141 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
142 column = offset_out = 0;
146 line_out[offset_out++] = c;
150 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
153 if (ferror(istream) || bb_fclose_nonstdin(istream)) {
154 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
155 errs |= EXIT_FAILURE;
159 bb_fflush_stdout_and_exit(errs);
161 /* vi: set sw=4 ts=4: */