hush: rename hush-redir/redir3.tests (ash has redir3.tests which id different)
[oweals/busybox.git] / coreutils / uudecode.c
index 6ecfe6ceffa076e028fe1dcae30848533bb74df7..ddce2548b87b7b77dd546ef602e46c7b365e3e1d 100644 (file)
  * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
  * "end" line
  */
+//config:config UUDECODE
+//config:      bool "uudecode"
+//config:      default y
+//config:      help
+//config:        uudecode is used to decode a uuencoded file.
+
+//applet:IF_UUDECODE(APPLET(uudecode, BB_DIR_USR_BIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_UUDECODE) += uudecode.o
 
 //usage:#define uudecode_trivial_usage
 //usage:       "[-o OUTFILE] [INFILE]"
 //usage:#define uudecode_full_usage "\n\n"
 //usage:       "Uudecode a file\n"
-//usage:       "Finds outfile name in uuencoded source unless -o is given"
+//usage:       "Finds OUTFILE in uuencoded source unless -o is given"
 //usage:
 //usage:#define uudecode_example_usage
 //usage:       "$ uudecode -o busybox busybox.uu\n"
@@ -29,9 +38,19 @@ static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags U
 {
        char *line;
 
-       while ((line = xmalloc_fgetline(src_stream)) != NULL) {
+       for (;;) {
                int encoded_len, str_len;
                char *line_ptr, *dst;
+               size_t line_len;
+
+               line_len = 64 * 1024;
+               line = xmalloc_fgets_str_len(src_stream, "\n", &line_len);
+               if (!line)
+                       break;
+               /* Handle both Unix and MSDOS text, and stray trailing spaces */
+               str_len = line_len;
+               while (--str_len >= 0 && isspace(line[str_len]))
+                       line[str_len] = '\0';
 
                if (strcmp(line, "end") == 0) {
                        return; /* the only non-error exit */
@@ -46,7 +65,7 @@ static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags U
 
                encoded_len = line[0] * 4 / 3;
                /* Check that line is not too short. (we tolerate
-                * overly _long_ line to accomodate possible extra '`').
+                * overly _long_ line to accommodate possible extra '`').
                 * Empty line case is also caught here. */
                if (str_len <= encoded_len) {
                        break; /* go to bb_error_msg_and_die("short file"); */
@@ -110,10 +129,10 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
                FILE *dst_stream;
                int mode;
 
-               if (strncmp(line, "begin-base64 ", 13) == 0) {
+               if (is_prefixed_with(line, "begin-base64 ")) {
                        line_ptr = line + 13;
                        decode_fn_ptr = read_base64;
-               } else if (strncmp(line, "begin ", 6) == 0) {
+               } else if (is_prefixed_with(line, "begin ")) {
                        line_ptr = line + 6;
                        decode_fn_ptr = read_stduu;
                } else {
@@ -125,10 +144,12 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
                mode = bb_strtou(line_ptr, NULL, 8);
                if (outname == NULL) {
                        outname = strchr(line_ptr, ' ');
-                       if ((outname == NULL) || (*outname == '\0')) {
+                       if (!outname)
                                break;
-                       }
                        outname++;
+                       trim(outname); /* remove trailing space (and '\r' for DOS text) */
+                       if (!outname[0])
+                               break;
                }
                dst_stream = stdout;
                if (NOT_LONE_DASH(outname)) {