* archival/bunzip2.c (bunzip2_main): Do not remove files if writing to standard
[oweals/busybox.git] / coreutils / uuencode.c
index d46fb1588f8c219f08c33158f36cf56e8feb7d7a..0a362a2622f5ed3d22df906d7151097f531a9b4d 100644 (file)
@@ -56,7 +56,7 @@ static char tbl_std[64] = {
  * buffer of at least 1+BASE64_LENGTH(length) bytes.
  * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3))
  */
-static void base64_encode (const char *s, const char *store, const int length, const char *tbl)
+static void uuencode (const char *s, const char *store, const int length, const char *tbl)
 {
        int i;
        unsigned char *p = (unsigned char *)store;
@@ -82,10 +82,10 @@ static void base64_encode (const char *s, const char *store, const int length, c
 
 int uuencode_main(int argc, char **argv)
 {
-       const int src_buf_size = BUFSIZ;        // This *MUST* be a multiple of 3
+       const int src_buf_size = 60;    // This *MUST* be a multiple of 3
        const int dst_buf_size = 4 * ((src_buf_size + 2) / 3);
-       RESERVE_BB_BUFFER(src_buf, src_buf_size + 1);
-       RESERVE_BB_BUFFER(dst_buf, dst_buf_size + 1);
+       RESERVE_CONFIG_BUFFER(src_buf, src_buf_size + 1);
+       RESERVE_CONFIG_BUFFER(dst_buf, dst_buf_size + 1);
        struct stat stat_buf;
        FILE *src_stream = stdin;
        char *tbl = tbl_std;
@@ -93,7 +93,7 @@ int uuencode_main(int argc, char **argv)
        mode_t mode;
        int opt;
        int column = 0;
-       int write_size;
+       int write_size = 0;
        int remaining;
        int buffer_offset = 0;
 
@@ -117,7 +117,7 @@ int uuencode_main(int argc, char **argv)
                        }
                        break;
                case 1:
-                       mode = umask(0666);
+                       mode = 0666 & ~umask(0666);
                        break;
                default:
                        show_usage();
@@ -127,7 +127,7 @@ int uuencode_main(int argc, char **argv)
 
        while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) {
                /* Encode the buffer we just read in */
-               base64_encode(src_buf, dst_buf, size, tbl);
+               uuencode(src_buf, dst_buf, size, tbl);
 
                /* Write the buffer to stdout, wrapping at 60 chars.
                 * This looks overly complex, but it gets tricky as
@@ -140,19 +140,20 @@ int uuencode_main(int argc, char **argv)
                /* Initialise values for the new buffer */
                remaining = 4 * ((size + 2) / 3);
                buffer_offset = 0;
-               if (remaining > (60 - column)) {
-                       write_size = 60 - column;
-               }
-               else if (remaining < 60) {
-                       write_size = remaining;
-               } else {
-                       write_size = 60;
-               }
 
                /* Write the buffer to stdout, wrapping at 60 chars
                 * starting from the column the last buffer ran out
                 */
                do {
+                       if (remaining > (60 - column)) {
+                               write_size = 60 - column;
+                       }
+                       else if (remaining < 60) {
+                               write_size = remaining;
+                       } else {
+                               write_size = 60;
+                       }
+
                        /* Setup a new row if required */
                        if (column == 0) {
                                putchar('\n');
@@ -171,15 +172,6 @@ int uuencode_main(int argc, char **argv)
                        if (column % 60 == 0) {
                                column = 0;
                        }
-
-                       /* working next amount to write */
-                       write_size = (60 - column) % 60;
-                       if (write_size < remaining) {
-                               write_size = remaining;
-                       }
-                       if (write_size == 0) {
-                               write_size = 60;
-                       }
                } while (remaining > 0);
        }
        printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n");