aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2006-01-30 17:41:06 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2006-01-30 17:41:06 +0000
commit4bf31272ff074e5383b61f43aa270b50908991f8 (patch)
tree894471e59ff71c616720eefeb3031ce8d4300ee1
parent76b24270d459f574a972467ff525ed7a793e8bdc (diff)
downloadbusybox-w32-4bf31272ff074e5383b61f43aa270b50908991f8.tar.gz
busybox-w32-4bf31272ff074e5383b61f43aa270b50908991f8.tar.bz2
busybox-w32-4bf31272ff074e5383b61f43aa270b50908991f8.zip
- very minor tweak: save 1 byte.
statistics: text data bss dec hex filename 867 0 12 879 36f fold.o.oorig.gcc-3.3 843 0 12 855 357 fold.o.oorig.gcc-3.4 780 0 12 792 318 fold.o.oorig.gcc-4.0 761 0 12 773 305 fold.o.oorig.gcc-4.1-HEAD 761 0 12 773 305 fold.o.oorig.gcc-4.2-HEAD 866 0 12 878 36e fold.o.gcc-3.3 842 0 12 854 356 fold.o.gcc-3.4 779 0 12 791 317 fold.o.gcc-4.0 760 0 12 772 304 fold.o.gcc-4.1-HEAD 760 0 12 772 304 fold.o.gcc-4.2-HEAD
-rw-r--r--coreutils/fold.c130
1 files changed, 65 insertions, 65 deletions
diff --git a/coreutils/fold.c b/coreutils/fold.c
index 85b15f4d8..f1ab1bdc0 100644
--- a/coreutils/fold.c
+++ b/coreutils/fold.c
@@ -51,7 +51,6 @@ extern int fold_main(int argc, char **argv)
51 int i; 51 int i;
52 int errs = 0; 52 int errs = 0;
53 53
54
55 if(!ENABLE_DEBUG_YANK_SUSv2) { 54 if(!ENABLE_DEBUG_YANK_SUSv2) {
56 /* Turn any numeric options into -w options. */ 55 /* Turn any numeric options into -w options. */
57 for (i = 1; i < argc; i++) { 56 for (i = 1; i < argc; i++) {
@@ -78,80 +77,81 @@ extern int fold_main(int argc, char **argv)
78 77
79 do { 78 do {
80 FILE *istream = bb_wfopen_input(*argv); 79 FILE *istream = bb_wfopen_input(*argv);
81 if (istream != NULL) { 80 int c;
82 int c; 81 int column = 0; /* Screen column where next char will go. */
83 int column = 0; /* Screen column where next char will go. */ 82 int offset_out = 0; /* Index in `line_out' for next char. */
84 int offset_out = 0; /* Index in `line_out' for next char. */ 83 static char *line_out = NULL;
85 static char *line_out = NULL; 84 static int allocated_out = 0;
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 85
94 if (c == '\n') { 86 if (istream == NULL) {
95 line_out[offset_out++] = c; 87 errs |= EXIT_FAILURE;
96 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); 88 continue;
97 column = offset_out = 0; 89 }
98 continue; 90
99 } 91 while ((c = getc(istream)) != EOF) {
92 if (offset_out + 1 >= allocated_out) {
93 allocated_out += 1024;
94 line_out = xrealloc(line_out, allocated_out);
95 }
96
97 if (c == '\n') {
98 line_out[offset_out++] = c;
99 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
100 column = offset_out = 0;
101 continue;
102 }
100 103
101rescan: 104rescan:
102 column = adjust_column(column, c); 105 column = adjust_column(column, c);
103 106
104 if (column > width) { 107 if (column > width) {
105 /* This character would make the line too long. 108 /* This character would make the line too long.
106 Print the line plus a newline, and make this character 109 Print the line plus a newline, and make this character
107 start the next line. */ 110 start the next line. */
108 if (flags & FLAG_BREAK_SPACES) { 111 if (flags & FLAG_BREAK_SPACES) {
109 /* Look for the last blank. */ 112 /* Look for the last blank. */
110 int logical_end; 113 int logical_end;
111 114
112 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) { 115 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
113 if (isblank(line_out[logical_end])) { 116 if (isblank(line_out[logical_end])) {
114 break; 117 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 } 118 }
131 } else { 119 }
132 if (offset_out == 0) { 120 if (logical_end >= 0) {
133 line_out[offset_out++] = c; 121 /* Found a blank. Don't output the part after it. */
134 continue; 122 logical_end++;
123 fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
124 putchar('\n');
125 /* Move the remainder to the beginning of the next line.
126 The areas being copied here might overlap. */
127 memmove(line_out, line_out + logical_end, offset_out - logical_end);
128 offset_out -= logical_end;
129 for (column = i = 0; i < offset_out; i++) {
130 column = adjust_column(column, line_out[i]);
135 } 131 }
132 goto rescan;
133 }
134 } else {
135 if (offset_out == 0) {
136 line_out[offset_out++] = c;
137 continue;
136 } 138 }
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 } 139 }
142 140 line_out[offset_out++] = '\n';
143 line_out[offset_out++] = c;
144 }
145
146 if (offset_out) {
147 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); 141 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
142 column = offset_out = 0;
143 goto rescan;
148 } 144 }
149 145
150 if (ferror(istream) || bb_fclose_nonstdin(istream)) { 146 line_out[offset_out++] = c;
151 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ 147 }
152 errs |= EXIT_FAILURE; 148
153 } 149 if (offset_out) {
154 } else { 150 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
151 }
152
153 if (ferror(istream) || bb_fclose_nonstdin(istream)) {
154 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
155 errs |= EXIT_FAILURE; 155 errs |= EXIT_FAILURE;
156 } 156 }
157 } while (*++argv); 157 } while (*++argv);