aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-04-27 06:02:14 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-04-27 06:02:14 +0000
commitf01b805d9142bad42ebd8d0b4f6770bf06fb2cc3 (patch)
tree3a0adfb6330858de103e96a5b42d3f76418502af
parentc91ce5709998976d8b6614b3ac6201692d20eb94 (diff)
downloadbusybox-w32-f01b805d9142bad42ebd8d0b4f6770bf06fb2cc3.tar.gz
busybox-w32-f01b805d9142bad42ebd8d0b4f6770bf06fb2cc3.tar.bz2
busybox-w32-f01b805d9142bad42ebd8d0b4f6770bf06fb2cc3.zip
New applet, fold
-rw-r--r--coreutils/Config.in6
-rw-r--r--coreutils/Makefile.in1
-rw-r--r--coreutils/fold.c202
-rw-r--r--include/applets.h3
-rw-r--r--include/usage.h10
5 files changed, 222 insertions, 0 deletions
diff --git a/coreutils/Config.in b/coreutils/Config.in
index 7a4b9be4c..0d5eff3c9 100644
--- a/coreutils/Config.in
+++ b/coreutils/Config.in
@@ -165,6 +165,12 @@ if !CONFIG_HUSH && !CONFIG_LASH && !CONFIG_MSH
165 Please submit a patch to add help text for this item. 165 Please submit a patch to add help text for this item.
166endif 166endif
167 167
168config CONFIG_FOLD
169 bool "fold"
170 default n
171 help
172 Wrap text to fit a specific width.
173
168config CONFIG_HEAD 174config CONFIG_HEAD
169 bool "head" 175 bool "head"
170 default n 176 default n
diff --git a/coreutils/Makefile.in b/coreutils/Makefile.in
index be34934ab..881d8c0d0 100644
--- a/coreutils/Makefile.in
+++ b/coreutils/Makefile.in
@@ -43,6 +43,7 @@ COREUTILS-$(CONFIG_ECHO) += echo.o
43COREUTILS-$(CONFIG_ENV) += env.o 43COREUTILS-$(CONFIG_ENV) += env.o
44COREUTILS-$(CONFIG_EXPR) += expr.o 44COREUTILS-$(CONFIG_EXPR) += expr.o
45COREUTILS-$(CONFIG_FALSE) += false.o 45COREUTILS-$(CONFIG_FALSE) += false.o
46COREUTILS-$(CONFIG_FOLD) += fold.o
46COREUTILS-$(CONFIG_HEAD) += head.o 47COREUTILS-$(CONFIG_HEAD) += head.o
47COREUTILS-$(CONFIG_HOSTID) += hostid.o 48COREUTILS-$(CONFIG_HOSTID) += hostid.o
48COREUTILS-$(CONFIG_ID) += id.o 49COREUTILS-$(CONFIG_ID) += id.o
diff --git a/coreutils/fold.c b/coreutils/fold.c
new file mode 100644
index 000000000..f9ae526df
--- /dev/null
+++ b/coreutils/fold.c
@@ -0,0 +1,202 @@
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@optushome.com.au>
8
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. */
100 long int tmp_long;
101 char *end_ptr;
102
103 errno = 0;
104 tmp_long = strtol(optarg, &end_ptr, 10);
105 if (!end_ptr || errno || tmp_long < 1) {
106 bb_error_msg_and_die("invalid number of columns: `%s'", optarg);
107 }
108 width = (int) tmp_long;
109 break;
110 }
111 default:
112 bb_show_usage();
113 }
114 }
115
116 argv += optind;
117 if (!*argv) {
118 *--argv = "-";
119 }
120
121 do {
122 FILE *istream = bb_wfopen_input(*argv);
123 if (istream != NULL) {
124 int c;
125 int column = 0; /* Screen column where next char will go. */
126 int offset_out = 0; /* Index in `line_out' for next char. */
127 static char *line_out = NULL;
128 static int allocated_out = 0;
129
130 while ((c = getc(istream)) != EOF) {
131 if (offset_out + 1 >= allocated_out) {
132 allocated_out += 1024;
133 line_out = xrealloc(line_out, allocated_out);
134 }
135
136 if (c == '\n') {
137 line_out[offset_out++] = c;
138 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
139 column = offset_out = 0;
140 continue;
141 }
142
143rescan:
144 column = adjust_column(column, c);
145
146 if (column > width) {
147 /* This character would make the line too long.
148 Print the line plus a newline, and make this character
149 start the next line. */
150 if (break_spaces) {
151 /* Look for the last blank. */
152 int logical_end;
153
154 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
155 if (isblank(line_out[logical_end])) {
156 break;
157 }
158 }
159 if (logical_end >= 0) {
160 /* Found a blank. Don't output the part after it. */
161 logical_end++;
162 fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
163 putchar('\n');
164 /* Move the remainder to the beginning of the next line.
165 The areas being copied here might overlap. */
166 memmove(line_out, line_out + logical_end, offset_out - logical_end);
167 offset_out -= logical_end;
168 for (column = i = 0; i < offset_out; i++) {
169 column = adjust_column(column, line_out[i]);
170 }
171 goto rescan;
172 }
173 } else {
174 if (offset_out == 0) {
175 line_out[offset_out++] = c;
176 continue;
177 }
178 }
179 line_out[offset_out++] = '\n';
180 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
181 column = offset_out = 0;
182 goto rescan;
183 }
184
185 line_out[offset_out++] = c;
186 }
187
188 if (offset_out) {
189 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
190 }
191
192 if (ferror(istream) || bb_fclose_nonstdin(istream)) {
193 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
194 errs |= EXIT_FAILURE;
195 }
196 } else {
197 errs |= EXIT_FAILURE;
198 }
199 } while (*++argv);
200
201 bb_fflush_stdout_and_exit(errs);
202}
diff --git a/include/applets.h b/include/applets.h
index 7267b8248..668c84914 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -202,6 +202,9 @@
202#ifdef CONFIG_FIND 202#ifdef CONFIG_FIND
203 APPLET(find, find_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) 203 APPLET(find, find_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
204#endif 204#endif
205#ifdef CONFIG_FOLD
206 APPLET(fold, fold_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
207#endif
205#ifdef CONFIG_FREE 208#ifdef CONFIG_FREE
206 APPLET(free, free_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) 209 APPLET(free, free_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
207#endif 210#endif
diff --git a/include/usage.h b/include/usage.h
index 6848549f7..e98e8b97c 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -627,6 +627,16 @@
627 "$ find / -name /etc/passwd\n" \ 627 "$ find / -name /etc/passwd\n" \
628 "/etc/passwd\n" 628 "/etc/passwd\n"
629 629
630#define fold_trivial_usage \
631 "[bsw] [FILE]"
632#define fold_full_usage \
633 "Wrap input lines in each FILE (standard input by default), writing to\n" \
634 "standard output.\n\n" \
635 "Options:\n" \
636 "\t-b\tcount bytes rather than columns\n" \
637 "\t-s\tbreak at spaces\n" \
638 "\t-w\tuse WIDTH columns instead of 80\n"
639
630#define free_trivial_usage \ 640#define free_trivial_usage \
631 "" 641 ""
632#define free_full_usage \ 642#define free_full_usage \