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)
else for (;;) {
int i, res;
- res = read(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1));
+#define read_buf bb_common_bufsiz1
+ res = read(fd, read_buf, COMMON_BUFSIZE);
if (res < 0)
retval = EXIT_FAILURE;
if (res < 1)
break;
for (i = 0; i < res; i++) {
- char c = bb_common_bufsiz1[i];
+ char c = read_buf[i];
if (c > 126 && (flags & CATV_OPT_v)) {
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)
crc = 0;
length = 0;
- while ((bytes_read = fread(bb_common_bufsiz1, 1, BUFSIZ, fp)) > 0) {
- cp = bb_common_bufsiz1;
+#define read_buf bb_common_bufsiz1
+ while ((bytes_read = fread(read_buf, 1, BUFSIZ, fp)) > 0) {
+ cp = read_buf;
length += bytes_read;
while (bytes_read--)
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)
date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y";
}
+#define date_buf bb_common_bufsiz1
if (*date_fmt == '\0') {
/* With no format string, just print a blank line */
- *bb_common_bufsiz1 = 0;
+ date_buf[0] = '\0';
} else {
/* Handle special conversions */
@@ -233,9 +234,9 @@ int date_main(int argc, char **argv)
}
/* Generate output string */
- strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time);
+ strftime(date_buf, sizeof(date_buf), date_fmt, &tm_time);
}
- puts(bb_common_bufsiz1);
+ puts(date_buf);
return EXIT_SUCCESS;
}
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 @@
#include "libbb.h"
-enum ConvType {
+enum {
CT_UNIX2DOS = 1,
CT_DOS2UNIX
-} ConvType;
+};
/* if fn is NULL then input is stdin and output is stdout */
-static int convert(char *fn)
+static int convert(char *fn, int ConvType)
{
FILE *in, *out;
int i;
+#define name_buf bb_common_bufsiz1
+ in = stdin;
+ out = stdout;
if (fn != NULL) {
in = xfopen(fn, "rw");
/*
@@ -32,24 +35,17 @@ static int convert(char *fn)
permissions 0666 for glibc 2.0.6 and earlier or
0600 for glibc 2.0.7 and later.
*/
- snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn);
- /*
- sizeof bb_common_bufsiz1 is 4096, so it should be big enough to
- hold the full path. However if the output is truncated the
- subsequent call to mkstemp would fail.
- */
- i = mkstemp(&bb_common_bufsiz1[0]);
- if (i == -1 || chmod(bb_common_bufsiz1, 0600) == -1) {
+ snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
+ i = mkstemp(&name_buf[0]);
+ if (i == -1 || chmod(name_buf, 0600) == -1) {
bb_perror_nomsg_and_die();
}
out = fdopen(i, "w+");
if (!out) {
close(i);
- remove(bb_common_bufsiz1);
+ remove(name_buf);
+ return -2;
}
- } else {
- in = stdin;
- out = stdout;
}
while ((i = fgetc(in)) != EOF) {
@@ -67,14 +63,14 @@ static int convert(char *fn)
if (fn != NULL) {
if (fclose(in) < 0 || fclose(out) < 0) {
bb_perror_nomsg();
- remove(bb_common_bufsiz1);
+ remove(name_buf);
return -2;
}
/* Assume they are both on the same filesystem (which
* should be true since we put them into the same directory
* so we _should_ be ok, but you never know... */
- if (rename(bb_common_bufsiz1, fn) < 0) {
- bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn);
+ if (rename(name_buf, fn) < 0) {
+ bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
return -1;
}
}
@@ -85,13 +81,13 @@ static int convert(char *fn)
int dos2unix_main(int argc, char **argv);
int dos2unix_main(int argc, char **argv)
{
- int o;
+ int o, ConvType;
/* See if we are supposed to be doing dos2unix or unix2dos */
if (applet_name[0] == 'd') {
- ConvType = CT_DOS2UNIX; /*2 */
+ ConvType = CT_DOS2UNIX; /* 2 */
} else {
- ConvType = CT_UNIX2DOS; /*1 */
+ ConvType = CT_UNIX2DOS; /* 1 */
}
/* -u and -d are mutally exclusive */
opt_complementary = "?:u--d:d--u";
@@ -105,12 +101,13 @@ int dos2unix_main(int argc, char **argv)
if (o)
ConvType = o;
- if (optind < argc) {
- while (optind < argc)
- if ((o = convert(argv[optind++])) < 0)
- break;
- } else
- o = convert(NULL);
+ do {
+ /* might be convert(NULL) if there is no filename given */
+ o = convert(argv[optind], ConvType);
+ if (o < 0)
+ break;
+ optind++;
+ } while (optind < argc);
return o;
}
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)
}
#define read_buffer bb_common_bufsiz1
-enum { READ_BUFFER_SIZE = sizeof(bb_common_bufsiz1) - 1 };
+enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
#define SPLIT_OPT_l (1<<0)
#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 @@
#define TR_OPT_complement (1<<0)
#define TR_OPT_delete (1<<1)
#define TR_OPT_squeeze_reps (1<<2)
-/* some "globals" shared across this file */
-/* these last are pointers to static buffers declared in tr_main */
-static char *poutput, *pvector, *pinvec, *poutvec;
-static void ATTRIBUTE_NORETURN convert(const smalluint flags)
-{
- size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
-
- for (;;) {
- /* If we're out of input, flush output and read more input. */
- if (in_index == read_chars) {
- if (out_index) {
- xwrite(STDOUT_FILENO, (char *)poutput, out_index);
- out_index = 0;
- }
- if ((read_chars = read(STDIN_FILENO, bb_common_bufsiz1, BUFSIZ)) <= 0) {
- if (write(STDOUT_FILENO, (char *)poutput, out_index) != out_index)
- bb_perror_msg(bb_msg_write_error);
- exit(EXIT_SUCCESS);
- }
- in_index = 0;
- }
- c = bb_common_bufsiz1[in_index++];
- coded = pvector[c];
- if ((flags & TR_OPT_delete) && pinvec[c])
- continue;
- if ((flags & TR_OPT_squeeze_reps) && last == coded &&
- (pinvec[c] || poutvec[coded]))
- continue;
- poutput[out_index++] = last = coded;
- }
- /* NOTREACHED */
-}
-
-static void map(unsigned char *string1, unsigned int string1_len,
+static void map(char *pvector,
+ unsigned char *string1, unsigned int string1_len,
unsigned char *string2, unsigned int string2_len)
{
char last = '0';
@@ -121,9 +89,9 @@ static unsigned int expand(const char *arg, char *buffer)
if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
smalluint j;
{ /* not really pretty.. */
- char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7
- j = index_in_str_array(classes, tmp) + 1;
- free(tmp);
+ char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7
+ j = index_in_str_array(classes, tmp) + 1;
+ free(tmp);
}
if (j == CLASS_alnum || j == CLASS_digit) {
for (i = '0'; i <= '9'; i++)
@@ -183,7 +151,7 @@ static unsigned int expand(const char *arg, char *buffer)
static int complement(char *buffer, int buffer_len)
{
- short i, j, ix;
+ int i, j, ix;
char conv[ASCII + 2];
ix = 0;
@@ -206,17 +174,12 @@ int tr_main(int argc, char **argv)
int idx = 1;
int i;
smalluint flags = 0;
+ size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
RESERVE_CONFIG_UBUFFER(output, BUFSIZ);
RESERVE_CONFIG_BUFFER(vector, ASCII+1);
RESERVE_CONFIG_BUFFER(invec, ASCII+1);
RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
- /* ... but make them available globally */
- poutput = output;
- pvector = vector;
- pinvec = invec;
- poutvec = outvec;
-
if (argc > 1 && argv[idx][0] == '-') {
for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
if (*ptr == 'c')
@@ -235,21 +198,47 @@ int tr_main(int argc, char **argv)
invec[i] = outvec[i] = FALSE;
}
+#define tr_buf bb_common_bufsiz1
if (argv[idx] != NULL) {
- input_length = expand(argv[idx++], bb_common_bufsiz1);
+ input_length = expand(argv[idx++], tr_buf);
if (flags & TR_OPT_complement)
- input_length = complement(bb_common_bufsiz1, input_length);
+ input_length = complement(tr_buf, input_length);
if (argv[idx] != NULL) {
if (*argv[idx] == '\0')
bb_error_msg_and_die("STRING2 cannot be empty");
output_length = expand(argv[idx], output);
- map(bb_common_bufsiz1, input_length, output, output_length);
+ map(vector, tr_buf, input_length, output, output_length);
}
for (i = 0; i < input_length; i++)
- invec[(unsigned char)bb_common_bufsiz1[i]] = TRUE;
+ invec[(unsigned char)tr_buf[i]] = TRUE;
for (i = 0; i < output_length; i++)
outvec[output[i]] = TRUE;
}
- convert(flags);
+
+ for (;;) {
+ /* If we're out of input, flush output and read more input. */
+ if (in_index == read_chars) {
+ if (out_index) {
+ xwrite(STDOUT_FILENO, (char *)output, out_index);
+ out_index = 0;
+ }
+ read_chars = read(STDIN_FILENO, tr_buf, BUFSIZ);
+ if (read_chars <= 0) {
+ if (write(STDOUT_FILENO, (char *)output, out_index) != out_index)
+ bb_perror_msg(bb_msg_write_error);
+ exit(EXIT_SUCCESS);
+ }
+ in_index = 0;
+ }
+ c = tr_buf[in_index++];
+ coded = vector[c];
+ if ((flags & TR_OPT_delete) && invec[c])
+ continue;
+ if ((flags & TR_OPT_squeeze_reps) && last == coded &&
+ (invec[c] || outvec[coded]))
+ continue;
+ output[out_index++] = last = coded;
+ }
+ /* NOTREACHED */
return EXIT_SUCCESS;
}