remove outdated and do not corected comment. Use bb_xasprintf instead xmalloc+set...
[oweals/busybox.git] / coreutils / fold.c
1 /* fold -- wrap each input line to fit in specified width.
2
3    Written by David MacKenzie, djm@gnu.ai.mit.edu.
4    Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
5
6    Modified for busybox based on coreutils v 5.0
7    Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
8
9    Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
10 */
11
12 #include <ctype.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include "busybox.h"
20
21 static unsigned long flags;
22 #define FLAG_COUNT_BYTES        1
23 #define FLAG_BREAK_SPACES       2
24 #define FLAG_WIDTH                      4
25
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. */
29
30 static int adjust_column(int column, char c)
31 {
32         if (!(flags & FLAG_COUNT_BYTES)) {
33                 if (c == '\b') {
34                         if (column > 0)
35                                 column--;
36                 } else if (c == '\r')
37                         column = 0;
38                 else if (c == '\t')
39                         column = column + 8 - column % 8;
40                 else                    /* if (isprint (c)) */
41                         column++;
42         } else
43                 column++;
44         return column;
45 }
46
47 extern int fold_main(int argc, char **argv)
48 {
49         char *w_opt;
50         int width = 80;
51         int i;
52         int errs = 0;
53
54
55         if(!ENABLE_DEBUG_YANK_SUSv2) {
56                 /* Turn any numeric options into -w options.  */
57                 for (i = 1; i < argc; i++) {
58                         char const *a = argv[i];
59
60                         if (*a++ == '-') {
61                                 if (*a == '-' && !a[1])
62                                         break;
63                                 if (isdigit(*a)) {
64                                         argv[i] = bb_xasprintf("-w%s", a);
65                                 }
66                         }
67                 }
68         }
69
70         flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt);
71         if (flags & FLAG_WIDTH)
72                 width = bb_xgetlarg(w_opt, 10, 1, 10000);
73
74         argv += optind;
75         if (!*argv) {
76                 *--argv = "-";
77         }
78
79         do {
80                 FILE *istream = bb_wfopen_input(*argv);
81                 if (istream != NULL) {
82                         int c;
83                         int column = 0;         /* Screen column where next char will go. */
84                         int offset_out = 0;     /* Index in `line_out' for next char. */
85                         static char *line_out = NULL;
86                         static int allocated_out = 0;
87
88                         while ((c = getc(istream)) != EOF) {
89                                 if (offset_out + 1 >= allocated_out) {
90                                         allocated_out += 1024;
91                                         line_out = xrealloc(line_out, allocated_out);
92                                 }
93
94                                 if (c == '\n') {
95                                         line_out[offset_out++] = c;
96                                         fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
97                                         column = offset_out = 0;
98                                         continue;
99                                 }
100
101 rescan:
102                                 column = adjust_column(column, c);
103
104                                 if (column > width) {
105                                         /* This character would make the line too long.
106                                            Print the line plus a newline, and make this character
107                                            start the next line. */
108                                         if (flags & FLAG_BREAK_SPACES) {
109                                                 /* Look for the last blank. */
110                                                 int logical_end;
111
112                                                 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
113                                                         if (isblank(line_out[logical_end])) {
114                                                                 break;
115                                                         }
116                                                 }
117                                                 if (logical_end >= 0) {
118                                                         /* Found a blank.  Don't output the part after it. */
119                                                         logical_end++;
120                                                         fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
121                                                         putchar('\n');
122                                                         /* Move the remainder to the beginning of the next line.
123                                                            The areas being copied here might overlap. */
124                                                         memmove(line_out, line_out + logical_end, offset_out - logical_end);
125                                                         offset_out -= logical_end;
126                                                         for (column = i = 0; i < offset_out; i++) {
127                                                                 column = adjust_column(column, line_out[i]);
128                                                         }
129                                                         goto rescan;
130                                                 }
131                                         } else {
132                                                 if (offset_out == 0) {
133                                                         line_out[offset_out++] = c;
134                                                         continue;
135                                                 }
136                                         }
137                                         line_out[offset_out++] = '\n';
138                                         fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
139                                         column = offset_out = 0;
140                                         goto rescan;
141                                 }
142
143                                 line_out[offset_out++] = c;
144                         }
145
146                         if (offset_out) {
147                                 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
148                         }
149
150                         if (ferror(istream) || bb_fclose_nonstdin(istream)) {
151                                 bb_perror_msg("%s", *argv);     /* Avoid multibyte problems. */
152                                 errs |= EXIT_FAILURE;
153                         }
154                 } else {
155                         errs |= EXIT_FAILURE;
156                 }
157         } while (*++argv);
158
159         bb_fflush_stdout_and_exit(errs);
160 }
161 /* vi: set sw=4 ts=4: */