aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-12-02 17:55:45 +0000
committerlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-12-02 17:55:45 +0000
commit9ee06328d65784688a00d1c671ece1a74fae39fa (patch)
treed1e17ea93956a566cf9b6212b92ef6322d1f791c
parentdc34b0ae52622b698aeb7893f28c3715afd52e11 (diff)
downloadbusybox-w32-9ee06328d65784688a00d1c671ece1a74fae39fa.tar.gz
busybox-w32-9ee06328d65784688a00d1c671ece1a74fae39fa.tar.bz2
busybox-w32-9ee06328d65784688a00d1c671ece1a74fae39fa.zip
Another cleanup patch that's been in my tree for a while. Again I think it's
originally from Bernhard Fischer... git-svn-id: svn://busybox.net/trunk/busybox@12645 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--coreutils/fold.c54
1 files changed, 14 insertions, 40 deletions
diff --git a/coreutils/fold.c b/coreutils/fold.c
index 68f24e61a..31412da07 100644
--- a/coreutils/fold.c
+++ b/coreutils/fold.c
@@ -6,19 +6,7 @@
6 Modified for busybox based on coreutils v 5.0 6 Modified for busybox based on coreutils v 5.0
7 Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au> 7 Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
8 8
9 This program is free software; you can redistribute it and/or modify 9 Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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*/ 10*/
23 11
24#include <ctype.h> 12#include <ctype.h>
@@ -26,13 +14,15 @@
26#include <stdio.h> 14#include <stdio.h>
27#include <stdlib.h> 15#include <stdlib.h>
28#include <string.h> 16#include <string.h>
29#include <getopt.h>
30#include <sys/types.h> 17#include <sys/types.h>
31 18#include <unistd.h>
32#include "busybox.h" 19#include "busybox.h"
33 20
34/* If nonzero, count bytes, not column positions. */ 21/* If nonzero, count bytes, not column positions. */
35static int count_bytes; 22static unsigned long flags;
23#define FLAG_COUNT_BYTES 1
24#define FLAG_BREAK_SPACES 2
25#define FLAG_WIDTH 4
36 26
37/* Assuming the current column is COLUMN, return the column that 27/* Assuming the current column is COLUMN, return the column that
38 printing C will move the cursor to. 28 printing C will move the cursor to.
@@ -40,7 +30,7 @@ static int count_bytes;
40 30
41static int adjust_column(int column, char c) 31static int adjust_column(int column, char c)
42{ 32{
43 if (!count_bytes) { 33 if (!(flags & FLAG_COUNT_BYTES)) {
44 if (c == '\b') { 34 if (c == '\b') {
45 if (column > 0) 35 if (column > 0)
46 column--; 36 column--;
@@ -57,18 +47,14 @@ static int adjust_column(int column, char c)
57 47
58extern int fold_main(int argc, char **argv) 48extern int fold_main(int argc, char **argv)
59{ 49{
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. */ 50 /* If nonzero, at least one of the files we read was standard input. */
64 int have_read_stdin; 51 int have_read_stdin = 0;
65 52
66 int width = 80; 53 int width = 80;
67 int i; 54 int i;
68 int optc;
69 int errs = 0; 55 int errs = 0;
70 56
71 break_spaces = count_bytes = have_read_stdin = 0; 57 have_read_stdin = 0;
72 58
73 /* Turn any numeric options into -w options. */ 59 /* Turn any numeric options into -w options. */
74 for (i = 1; i < argc; i++) { 60 for (i = 1; i < argc; i++) {
@@ -88,22 +74,10 @@ extern int fold_main(int argc, char **argv)
88 } 74 }
89 } 75 }
90 76
91 while ((optc = getopt(argc, argv, "bsw:")) > 0) { 77 char *w_opt;
92 switch (optc) { 78 flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt);
93 case 'b': /* Count bytes rather than columns. */ 79 if (flags & 4)
94 count_bytes = 1; 80 width = bb_xgetlarg(w_opt, 10, 1, 10000);
95 break;
96 case 's': /* Break at word boundaries. */
97 break_spaces = 1;
98 break;
99 case 'w': { /* Line width. */
100 width = bb_xgetlarg(optarg, 10, 1, 10000);
101 break;
102 }
103 default:
104 bb_show_usage();
105 }
106 }
107 81
108 argv += optind; 82 argv += optind;
109 if (!*argv) { 83 if (!*argv) {
@@ -139,7 +113,7 @@ rescan:
139 /* This character would make the line too long. 113 /* This character would make the line too long.
140 Print the line plus a newline, and make this character 114 Print the line plus a newline, and make this character
141 start the next line. */ 115 start the next line. */
142 if (break_spaces) { 116 if (flags & FLAG_BREAK_SPACES) {
143 /* Look for the last blank. */ 117 /* Look for the last blank. */
144 int logical_end; 118 int logical_end;
145 119