aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/catv.c5
-rw-r--r--coreutils/cksum.c5
-rw-r--r--coreutils/date.c7
-rw-r--r--coreutils/dos2unix.c51
-rw-r--r--coreutils/split.c2
-rw-r--r--coreutils/tr.c87
6 files changed, 73 insertions, 84 deletions
diff --git a/coreutils/catv.c b/coreutils/catv.c
index 2d2229f7f..cc61233e1 100644
--- a/coreutils/catv.c
+++ b/coreutils/catv.c
@@ -33,13 +33,14 @@ int catv_main(int argc, char **argv)
33 else for (;;) { 33 else for (;;) {
34 int i, res; 34 int i, res;
35 35
36 res = read(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1)); 36#define read_buf bb_common_bufsiz1
37 res = read(fd, read_buf, COMMON_BUFSIZE);
37 if (res < 0) 38 if (res < 0)
38 retval = EXIT_FAILURE; 39 retval = EXIT_FAILURE;
39 if (res < 1) 40 if (res < 1)
40 break; 41 break;
41 for (i = 0; i < res; i++) { 42 for (i = 0; i < res; i++) {
42 char c = bb_common_bufsiz1[i]; 43 char c = read_buf[i];
43 44
44 if (c > 126 && (flags & CATV_OPT_v)) { 45 if (c > 126 && (flags & CATV_OPT_v)) {
45 if (c == 127) { 46 if (c == 127) {
diff --git a/coreutils/cksum.c b/coreutils/cksum.c
index 865bea0ee..987f5f32c 100644
--- a/coreutils/cksum.c
+++ b/coreutils/cksum.c
@@ -27,8 +27,9 @@ int cksum_main(int argc, char **argv)
27 crc = 0; 27 crc = 0;
28 length = 0; 28 length = 0;
29 29
30 while ((bytes_read = fread(bb_common_bufsiz1, 1, BUFSIZ, fp)) > 0) { 30#define read_buf bb_common_bufsiz1
31 cp = bb_common_bufsiz1; 31 while ((bytes_read = fread(read_buf, 1, BUFSIZ, fp)) > 0) {
32 cp = read_buf;
32 length += bytes_read; 33 length += bytes_read;
33 while (bytes_read--) 34 while (bytes_read--)
34 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL]; 35 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL];
diff --git a/coreutils/date.c b/coreutils/date.c
index 57c826a3f..cec8854ff 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -222,9 +222,10 @@ int date_main(int argc, char **argv)
222 date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y"; 222 date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y";
223 } 223 }
224 224
225#define date_buf bb_common_bufsiz1
225 if (*date_fmt == '\0') { 226 if (*date_fmt == '\0') {
226 /* With no format string, just print a blank line */ 227 /* With no format string, just print a blank line */
227 *bb_common_bufsiz1 = 0; 228 date_buf[0] = '\0';
228 } else { 229 } else {
229 /* Handle special conversions */ 230 /* Handle special conversions */
230 231
@@ -233,9 +234,9 @@ int date_main(int argc, char **argv)
233 } 234 }
234 235
235 /* Generate output string */ 236 /* Generate output string */
236 strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time); 237 strftime(date_buf, sizeof(date_buf), date_fmt, &tm_time);
237 } 238 }
238 puts(bb_common_bufsiz1); 239 puts(date_buf);
239 240
240 return EXIT_SUCCESS; 241 return EXIT_SUCCESS;
241} 242}
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index b053a0cbf..86adcd91f 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -14,17 +14,20 @@
14 14
15#include "libbb.h" 15#include "libbb.h"
16 16
17enum ConvType { 17enum {
18 CT_UNIX2DOS = 1, 18 CT_UNIX2DOS = 1,
19 CT_DOS2UNIX 19 CT_DOS2UNIX
20} ConvType; 20};
21 21
22/* if fn is NULL then input is stdin and output is stdout */ 22/* if fn is NULL then input is stdin and output is stdout */
23static int convert(char *fn) 23static int convert(char *fn, int ConvType)
24{ 24{
25 FILE *in, *out; 25 FILE *in, *out;
26 int i; 26 int i;
27#define name_buf bb_common_bufsiz1
27 28
29 in = stdin;
30 out = stdout;
28 if (fn != NULL) { 31 if (fn != NULL) {
29 in = xfopen(fn, "rw"); 32 in = xfopen(fn, "rw");
30 /* 33 /*
@@ -32,24 +35,17 @@ static int convert(char *fn)
32 permissions 0666 for glibc 2.0.6 and earlier or 35 permissions 0666 for glibc 2.0.6 and earlier or
33 0600 for glibc 2.0.7 and later. 36 0600 for glibc 2.0.7 and later.
34 */ 37 */
35 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn); 38 snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
36 /* 39 i = mkstemp(&name_buf[0]);
37 sizeof bb_common_bufsiz1 is 4096, so it should be big enough to 40 if (i == -1 || chmod(name_buf, 0600) == -1) {
38 hold the full path. However if the output is truncated the
39 subsequent call to mkstemp would fail.
40 */
41 i = mkstemp(&bb_common_bufsiz1[0]);
42 if (i == -1 || chmod(bb_common_bufsiz1, 0600) == -1) {
43 bb_perror_nomsg_and_die(); 41 bb_perror_nomsg_and_die();
44 } 42 }
45 out = fdopen(i, "w+"); 43 out = fdopen(i, "w+");
46 if (!out) { 44 if (!out) {
47 close(i); 45 close(i);
48 remove(bb_common_bufsiz1); 46 remove(name_buf);
47 return -2;
49 } 48 }
50 } else {
51 in = stdin;
52 out = stdout;
53 } 49 }
54 50
55 while ((i = fgetc(in)) != EOF) { 51 while ((i = fgetc(in)) != EOF) {
@@ -67,14 +63,14 @@ static int convert(char *fn)
67 if (fn != NULL) { 63 if (fn != NULL) {
68 if (fclose(in) < 0 || fclose(out) < 0) { 64 if (fclose(in) < 0 || fclose(out) < 0) {
69 bb_perror_nomsg(); 65 bb_perror_nomsg();
70 remove(bb_common_bufsiz1); 66 remove(name_buf);
71 return -2; 67 return -2;
72 } 68 }
73 /* Assume they are both on the same filesystem (which 69 /* Assume they are both on the same filesystem (which
74 * should be true since we put them into the same directory 70 * should be true since we put them into the same directory
75 * so we _should_ be ok, but you never know... */ 71 * so we _should_ be ok, but you never know... */
76 if (rename(bb_common_bufsiz1, fn) < 0) { 72 if (rename(name_buf, fn) < 0) {
77 bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn); 73 bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
78 return -1; 74 return -1;
79 } 75 }
80 } 76 }
@@ -85,13 +81,13 @@ static int convert(char *fn)
85int dos2unix_main(int argc, char **argv); 81int dos2unix_main(int argc, char **argv);
86int dos2unix_main(int argc, char **argv) 82int dos2unix_main(int argc, char **argv)
87{ 83{
88 int o; 84 int o, ConvType;
89 85
90 /* See if we are supposed to be doing dos2unix or unix2dos */ 86 /* See if we are supposed to be doing dos2unix or unix2dos */
91 if (applet_name[0] == 'd') { 87 if (applet_name[0] == 'd') {
92 ConvType = CT_DOS2UNIX; /*2 */ 88 ConvType = CT_DOS2UNIX; /* 2 */
93 } else { 89 } else {
94 ConvType = CT_UNIX2DOS; /*1 */ 90 ConvType = CT_UNIX2DOS; /* 1 */
95 } 91 }
96 /* -u and -d are mutally exclusive */ 92 /* -u and -d are mutally exclusive */
97 opt_complementary = "?:u--d:d--u"; 93 opt_complementary = "?:u--d:d--u";
@@ -105,12 +101,13 @@ int dos2unix_main(int argc, char **argv)
105 if (o) 101 if (o)
106 ConvType = o; 102 ConvType = o;
107 103
108 if (optind < argc) { 104 do {
109 while (optind < argc) 105 /* might be convert(NULL) if there is no filename given */
110 if ((o = convert(argv[optind++])) < 0) 106 o = convert(argv[optind], ConvType);
111 break; 107 if (o < 0)
112 } else 108 break;
113 o = convert(NULL); 109 optind++;
110 } while (optind < argc);
114 111
115 return o; 112 return o;
116} 113}
diff --git a/coreutils/split.c b/coreutils/split.c
index 27f9cfdd6..7b4f8c2c0 100644
--- a/coreutils/split.c
+++ b/coreutils/split.c
@@ -49,7 +49,7 @@ static char *next_file(char *old, unsigned suffix_len)
49} 49}
50 50
51#define read_buffer bb_common_bufsiz1 51#define read_buffer bb_common_bufsiz1
52enum { READ_BUFFER_SIZE = sizeof(bb_common_bufsiz1) - 1 }; 52enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
53 53
54#define SPLIT_OPT_l (1<<0) 54#define SPLIT_OPT_l (1<<0)
55#define SPLIT_OPT_b (1<<1) 55#define SPLIT_OPT_b (1<<1)
diff --git a/coreutils/tr.c b/coreutils/tr.c
index 7e89e9a80..c0d0dfacb 100644
--- a/coreutils/tr.c
+++ b/coreutils/tr.c
@@ -25,41 +25,9 @@
25#define TR_OPT_complement (1<<0) 25#define TR_OPT_complement (1<<0)
26#define TR_OPT_delete (1<<1) 26#define TR_OPT_delete (1<<1)
27#define TR_OPT_squeeze_reps (1<<2) 27#define TR_OPT_squeeze_reps (1<<2)
28/* some "globals" shared across this file */
29/* these last are pointers to static buffers declared in tr_main */
30static char *poutput, *pvector, *pinvec, *poutvec;
31 28
32static void ATTRIBUTE_NORETURN convert(const smalluint flags) 29static void map(char *pvector,
33{ 30 unsigned char *string1, unsigned int string1_len,
34 size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
35
36 for (;;) {
37 /* If we're out of input, flush output and read more input. */
38 if (in_index == read_chars) {
39 if (out_index) {
40 xwrite(STDOUT_FILENO, (char *)poutput, out_index);
41 out_index = 0;
42 }
43 if ((read_chars = read(STDIN_FILENO, bb_common_bufsiz1, BUFSIZ)) <= 0) {
44 if (write(STDOUT_FILENO, (char *)poutput, out_index) != out_index)
45 bb_perror_msg(bb_msg_write_error);
46 exit(EXIT_SUCCESS);
47 }
48 in_index = 0;
49 }
50 c = bb_common_bufsiz1[in_index++];
51 coded = pvector[c];
52 if ((flags & TR_OPT_delete) && pinvec[c])
53 continue;
54 if ((flags & TR_OPT_squeeze_reps) && last == coded &&
55 (pinvec[c] || poutvec[coded]))
56 continue;
57 poutput[out_index++] = last = coded;
58 }
59 /* NOTREACHED */
60}
61
62static void map(unsigned char *string1, unsigned int string1_len,
63 unsigned char *string2, unsigned int string2_len) 31 unsigned char *string2, unsigned int string2_len)
64{ 32{
65 char last = '0'; 33 char last = '0';
@@ -121,9 +89,9 @@ static unsigned int expand(const char *arg, char *buffer)
121 if (ENABLE_FEATURE_TR_CLASSES && i == ':') { 89 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
122 smalluint j; 90 smalluint j;
123 { /* not really pretty.. */ 91 { /* not really pretty.. */
124 char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7 92 char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7
125 j = index_in_str_array(classes, tmp) + 1; 93 j = index_in_str_array(classes, tmp) + 1;
126 free(tmp); 94 free(tmp);
127 } 95 }
128 if (j == CLASS_alnum || j == CLASS_digit) { 96 if (j == CLASS_alnum || j == CLASS_digit) {
129 for (i = '0'; i <= '9'; i++) 97 for (i = '0'; i <= '9'; i++)
@@ -183,7 +151,7 @@ static unsigned int expand(const char *arg, char *buffer)
183 151
184static int complement(char *buffer, int buffer_len) 152static int complement(char *buffer, int buffer_len)
185{ 153{
186 short i, j, ix; 154 int i, j, ix;
187 char conv[ASCII + 2]; 155 char conv[ASCII + 2];
188 156
189 ix = 0; 157 ix = 0;
@@ -206,17 +174,12 @@ int tr_main(int argc, char **argv)
206 int idx = 1; 174 int idx = 1;
207 int i; 175 int i;
208 smalluint flags = 0; 176 smalluint flags = 0;
177 size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
209 RESERVE_CONFIG_UBUFFER(output, BUFSIZ); 178 RESERVE_CONFIG_UBUFFER(output, BUFSIZ);
210 RESERVE_CONFIG_BUFFER(vector, ASCII+1); 179 RESERVE_CONFIG_BUFFER(vector, ASCII+1);
211 RESERVE_CONFIG_BUFFER(invec, ASCII+1); 180 RESERVE_CONFIG_BUFFER(invec, ASCII+1);
212 RESERVE_CONFIG_BUFFER(outvec, ASCII+1); 181 RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
213 182
214 /* ... but make them available globally */
215 poutput = output;
216 pvector = vector;
217 pinvec = invec;
218 poutvec = outvec;
219
220 if (argc > 1 && argv[idx][0] == '-') { 183 if (argc > 1 && argv[idx][0] == '-') {
221 for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) { 184 for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
222 if (*ptr == 'c') 185 if (*ptr == 'c')
@@ -235,21 +198,47 @@ int tr_main(int argc, char **argv)
235 invec[i] = outvec[i] = FALSE; 198 invec[i] = outvec[i] = FALSE;
236 } 199 }
237 200
201#define tr_buf bb_common_bufsiz1
238 if (argv[idx] != NULL) { 202 if (argv[idx] != NULL) {
239 input_length = expand(argv[idx++], bb_common_bufsiz1); 203 input_length = expand(argv[idx++], tr_buf);
240 if (flags & TR_OPT_complement) 204 if (flags & TR_OPT_complement)
241 input_length = complement(bb_common_bufsiz1, input_length); 205 input_length = complement(tr_buf, input_length);
242 if (argv[idx] != NULL) { 206 if (argv[idx] != NULL) {
243 if (*argv[idx] == '\0') 207 if (*argv[idx] == '\0')
244 bb_error_msg_and_die("STRING2 cannot be empty"); 208 bb_error_msg_and_die("STRING2 cannot be empty");
245 output_length = expand(argv[idx], output); 209 output_length = expand(argv[idx], output);
246 map(bb_common_bufsiz1, input_length, output, output_length); 210 map(vector, tr_buf, input_length, output, output_length);
247 } 211 }
248 for (i = 0; i < input_length; i++) 212 for (i = 0; i < input_length; i++)
249 invec[(unsigned char)bb_common_bufsiz1[i]] = TRUE; 213 invec[(unsigned char)tr_buf[i]] = TRUE;
250 for (i = 0; i < output_length; i++) 214 for (i = 0; i < output_length; i++)
251 outvec[output[i]] = TRUE; 215 outvec[output[i]] = TRUE;
252 } 216 }
253 convert(flags); 217
218 for (;;) {
219 /* If we're out of input, flush output and read more input. */
220 if (in_index == read_chars) {
221 if (out_index) {
222 xwrite(STDOUT_FILENO, (char *)output, out_index);
223 out_index = 0;
224 }
225 read_chars = read(STDIN_FILENO, tr_buf, BUFSIZ);
226 if (read_chars <= 0) {
227 if (write(STDOUT_FILENO, (char *)output, out_index) != out_index)
228 bb_perror_msg(bb_msg_write_error);
229 exit(EXIT_SUCCESS);
230 }
231 in_index = 0;
232 }
233 c = tr_buf[in_index++];
234 coded = vector[c];
235 if ((flags & TR_OPT_delete) && invec[c])
236 continue;
237 if ((flags & TR_OPT_squeeze_reps) && last == coded &&
238 (invec[c] || outvec[coded]))
239 continue;
240 output[out_index++] = last = coded;
241 }
242 /* NOTREACHED */
254 return EXIT_SUCCESS; 243 return EXIT_SUCCESS;
255} 244}