blob: 68f24e61a6d80901c3e72e07b6e7d6fb36b3f026 [file] [log] [blame]
Glenn L McGrathf01b8052003-04-27 06:02:14 +00001/* 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
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00007 Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrathf01b8052003-04-27 06:02:14 +00008
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)
12 any later version.
13
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.
18
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.
22*/
23
24#include <ctype.h>
25#include <errno.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <getopt.h>
30#include <sys/types.h>
31
32#include "busybox.h"
33
34/* If nonzero, count bytes, not column positions. */
35static int count_bytes;
36
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. */
40
41static int adjust_column(int column, char c)
42{
43 if (!count_bytes) {
44 if (c == '\b') {
45 if (column > 0)
46 column--;
47 } else if (c == '\r')
48 column = 0;
49 else if (c == '\t')
50 column = column + 8 - column % 8;
51 else /* if (isprint (c)) */
52 column++;
53 } else
54 column++;
55 return column;
56}
57
58extern int fold_main(int argc, char **argv)
59{
60 /* If nonzero, try to break on whitespace. */
61 int break_spaces;
62
63 /* If nonzero, at least one of the files we read was standard input. */
64 int have_read_stdin;
65
66 int width = 80;
67 int i;
68 int optc;
69 int errs = 0;
70
71 break_spaces = count_bytes = have_read_stdin = 0;
72
73 /* Turn any numeric options into -w options. */
74 for (i = 1; i < argc; i++) {
75 char const *a = argv[i];
76
77 if (a[0] == '-') {
78 if (a[1] == '-' && !a[2])
79 break;
80 if (isdigit(a[1])) {
81 char *s = xmalloc(strlen(a) + 2);
82
83 s[0] = '-';
84 s[1] = 'w';
85 strcpy(s + 2, a + 1);
86 argv[i] = s;
87 }
88 }
89 }
90
91 while ((optc = getopt(argc, argv, "bsw:")) > 0) {
92 switch (optc) {
93 case 'b': /* Count bytes rather than columns. */
94 count_bytes = 1;
95 break;
96 case 's': /* Break at word boundaries. */
97 break_spaces = 1;
98 break;
99 case 'w': { /* Line width. */
Glenn L McGrath7a543602003-04-27 10:05:35 +0000100 width = bb_xgetlarg(optarg, 10, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000101 break;
102 }
103 default:
104 bb_show_usage();
105 }
106 }
107
108 argv += optind;
109 if (!*argv) {
110 *--argv = "-";
111 }
112
113 do {
114 FILE *istream = bb_wfopen_input(*argv);
115 if (istream != NULL) {
116 int c;
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;
121
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);
126 }
127
128 if (c == '\n') {
129 line_out[offset_out++] = c;
130 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
131 column = offset_out = 0;
132 continue;
133 }
134
135rescan:
136 column = adjust_column(column, c);
137
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. */
142 if (break_spaces) {
143 /* Look for the last blank. */
144 int logical_end;
145
146 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
147 if (isblank(line_out[logical_end])) {
148 break;
149 }
150 }
151 if (logical_end >= 0) {
152 /* Found a blank. Don't output the part after it. */
153 logical_end++;
154 fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
155 putchar('\n');
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]);
162 }
163 goto rescan;
164 }
165 } else {
166 if (offset_out == 0) {
167 line_out[offset_out++] = c;
168 continue;
169 }
170 }
171 line_out[offset_out++] = '\n';
172 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
173 column = offset_out = 0;
174 goto rescan;
175 }
176
177 line_out[offset_out++] = c;
178 }
179
180 if (offset_out) {
181 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
182 }
183
184 if (ferror(istream) || bb_fclose_nonstdin(istream)) {
185 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
186 errs |= EXIT_FAILURE;
187 }
188 } else {
189 errs |= EXIT_FAILURE;
190 }
191 } while (*++argv);
192
193 bb_fflush_stdout_and_exit(errs);
194}