Teach libc5 what a sighandler_t is
[oweals/busybox.git] / uuencode.c
index 68d6f19a775fb8262aea1cca758ca1bdedcdb6e7..fc037403a33a66ea84a05897466d897ec490dd30 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,7 +82,7 @@ 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 = 600;   // 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);
@@ -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");