aboutsummaryrefslogtreecommitdiff
path: root/coreutils/fold.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/fold.c')
-rw-r--r--coreutils/fold.c29
1 files changed, 13 insertions, 16 deletions
diff --git a/coreutils/fold.c b/coreutils/fold.c
index ed484edf0..e2a30d5d4 100644
--- a/coreutils/fold.c
+++ b/coreutils/fold.c
@@ -12,18 +12,17 @@
12 12
13#include "libbb.h" 13#include "libbb.h"
14 14
15static unsigned long flags; 15/* Must match getopt32 call */
16#define FLAG_COUNT_BYTES 1 16#define FLAG_COUNT_BYTES 1
17#define FLAG_BREAK_SPACES 2 17#define FLAG_BREAK_SPACES 2
18#define FLAG_WIDTH 4 18#define FLAG_WIDTH 4
19 19
20/* Assuming the current column is COLUMN, return the column that 20/* Assuming the current column is COLUMN, return the column that
21 printing C will move the cursor to. 21 printing C will move the cursor to.
22 The first column is 0. */ 22 The first column is 0. */
23
24static int adjust_column(int column, char c) 23static int adjust_column(int column, char c)
25{ 24{
26 if (!(flags & FLAG_COUNT_BYTES)) { 25 if (!(option_mask32 & FLAG_COUNT_BYTES)) {
27 if (c == '\b') { 26 if (c == '\b') {
28 if (column > 0) 27 if (column > 0)
29 column--; 28 column--;
@@ -54,29 +53,27 @@ int fold_main(int argc, char **argv)
54 char const *a = argv[i]; 53 char const *a = argv[i];
55 54
56 if (*a++ == '-') { 55 if (*a++ == '-') {
57 if (*a == '-' && !a[1]) 56 if (*a == '-' && !a[1]) /* "--" */
58 break; 57 break;
59 if (isdigit(*a)) { 58 if (isdigit(*a))
60 argv[i] = xasprintf("-w%s", a); 59 argv[i] = xasprintf("-w%s", a);
61 }
62 } 60 }
63 } 61 }
64 } 62 }
65 63
66 flags = getopt32(argv, "bsw:", &w_opt); 64 getopt32(argv, "bsw:", &w_opt);
67 if (flags & FLAG_WIDTH) 65 if (option_mask32 & FLAG_WIDTH)
68 width = xatoul_range(w_opt, 1, 10000); 66 width = xatoul_range(w_opt, 1, 10000);
69 67
70 argv += optind; 68 argv += optind;
71 if (!*argv) { 69 if (!*argv)
72 *--argv = (char*)"-"; 70 *--argv = (char*)"-";
73 }
74 71
75 do { 72 do {
76 FILE *istream = fopen_or_warn_stdin(*argv); 73 FILE *istream = fopen_or_warn_stdin(*argv);
77 int c; 74 int c;
78 int column = 0; /* Screen column where next char will go. */ 75 int column = 0; /* Screen column where next char will go. */
79 int offset_out = 0; /* Index in `line_out' for next char. */ 76 int offset_out = 0; /* Index in 'line_out' for next char. */
80 77
81 if (istream == NULL) { 78 if (istream == NULL) {
82 errs |= EXIT_FAILURE; 79 errs |= EXIT_FAILURE;
@@ -102,7 +99,7 @@ int fold_main(int argc, char **argv)
102 /* This character would make the line too long. 99 /* This character would make the line too long.
103 Print the line plus a newline, and make this character 100 Print the line plus a newline, and make this character
104 start the next line. */ 101 start the next line. */
105 if (flags & FLAG_BREAK_SPACES) { 102 if (option_mask32 & FLAG_BREAK_SPACES) {
106 /* Look for the last blank. */ 103 /* Look for the last blank. */
107 int logical_end; 104 int logical_end;
108 105
@@ -144,7 +141,7 @@ int fold_main(int argc, char **argv)
144 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); 141 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
145 } 142 }
146 143
147 if (ferror(istream) || fclose_if_not_stdin(istream)) { 144 if (fclose_if_not_stdin(istream)) {
148 bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */ 145 bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */
149 errs |= EXIT_FAILURE; 146 errs |= EXIT_FAILURE;
150 } 147 }