Oops. Forgot these....
[oweals/busybox.git] / coreutils / uudecode.c
index 7b26d2dad78b99545beb66495d7aaacf3e2d81a0..a4059ddfe9b78c34221a902fe9d68994a0dfceb2 100644 (file)
  */
 
 
-#include "busybox.h"
 
 #include <stdio.h>
 #include <errno.h>
 #include <getopt.h>
+#include <string.h>
+#include <stdlib.h>
+#include "busybox.h"
+#include "pwd_grp/pwd.h"
+#include "pwd_grp/grp.h"
 
 /*struct passwd *getpwnam();*/
 
@@ -42,7 +46,7 @@ static int read_stduu (const char *inname)
     char *p;
 
     if (fgets (buf, sizeof(buf), stdin) == NULL) {
-      error_msg("%s: Short file\n", inname);
+      error_msg("%s: Short file", inname);
       return FALSE;
     }
     p = buf;
@@ -77,7 +81,7 @@ static int read_stduu (const char *inname)
 
   if (fgets (buf, sizeof(buf), stdin) == NULL
       || strcmp (buf, "end\n")) {
-    error_msg("%s: No `end' line\n", inname);
+    error_msg("%s: No `end' line", inname);
     return FALSE;
   }
 
@@ -127,7 +131,7 @@ static int read_base64 (const char *inname)
     unsigned char *p;
 
     if (fgets (buf, sizeof(buf), stdin) == NULL) {
-      error_msg("%s: Short file\n", inname);
+      error_msg("%s: Short file", inname);
       return FALSE;
     }
     p = buf;
@@ -135,7 +139,7 @@ static int read_base64 (const char *inname)
     if (memcmp (buf, "====", 4) == 0)
       break;
     if (last_data != 0) {
-      error_msg("%s: data following `=' padding character\n", inname);
+      error_msg("%s: data following `=' padding character", inname);
       return FALSE;
     }
 
@@ -157,14 +161,14 @@ static int read_base64 (const char *inname)
 
       while ((b64_tab[*p] & '\100') != 0)
         if (*p == '\n' || *p++ == '=') {
-          error_msg("%s: illegal line\n", inname);
+          error_msg("%s: illegal line", inname);
           return FALSE;
         }
       c2 = b64_tab[*p++];
 
       while (b64_tab[*p] == '\177')
         if (*p++ == '\n') {
-          error_msg("%s: illegal line\n", inname);
+          error_msg("%s: illegal line", inname);
           return FALSE;
         }
       if (*p == '=') {
@@ -176,7 +180,7 @@ static int read_base64 (const char *inname)
 
       while (b64_tab[*p] == '\177')
         if (*p++ == '\n') {
-          error_msg("%s: illegal line\n", inname);
+          error_msg("%s: illegal line", inname);
           return FALSE;
         }
       putchar (c1 << 2 | c2 >> 4);
@@ -197,18 +201,19 @@ static int decode (const char *inname,
                    const char *forced_outname)
 {
   struct passwd *pw;
-  register int n;
   register char *p;
-  int mode, n1;
+  int mode;
   char buf[2 * BUFSIZ];
   char *outname;
   int do_base64 = 0;
+  int res;
+  int dofre;
 
   /* Search for header line.  */
 
   while (1) {
     if (fgets (buf, sizeof (buf), stdin) == NULL) {
-      error_msg("%s: No `begin' line\n", inname);
+      error_msg("%s: No `begin' line", inname);
       return FALSE;
     }
 
@@ -222,6 +227,7 @@ static int decode (const char *inname,
   }
 
   /* If the output file name is given on the command line this rules.  */
+  dofre = FALSE;
   if (forced_outname != NULL)
     outname = (char *) forced_outname;
   else {
@@ -233,21 +239,17 @@ static int decode (const char *inname,
       while (*p != '/')
         ++p;
       if (*p == '\0') {
-        error_msg("%s: Illegal ~user\n", inname);
+        error_msg("%s: Illegal ~user", inname);
         return FALSE;
       }
       *p++ = '\0';
       pw = getpwnam (buf + 1);
       if (pw == NULL) {
-        error_msg("%s: No user `%s'\n", inname, buf + 1);
+        error_msg("%s: No user `%s'", inname, buf + 1);
         return FALSE;
       }
-      n = strlen (pw->pw_dir);
-      n1 = strlen (p);
-      outname = (char *) alloca ((size_t) (n + n1 + 2));
-      memcpy (outname + n + 1, p, (size_t) (n1 + 1));
-      memcpy (outname, pw->pw_dir, (size_t) n);
-      outname[n] = '/';
+      outname = concat_path_file(pw->pw_dir, p);
+      dofre = TRUE;
     }
   }
 
@@ -257,6 +259,8 @@ static int decode (const char *inname,
          || chmod (outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO))
          )) {
     perror_msg("%s", outname); /* */
+    if (dofre)
+       free(outname);
     return FALSE;
   }
 
@@ -265,9 +269,12 @@ static int decode (const char *inname,
 
   /* For each input line:  */
   if (do_base64)
-    return read_base64 (inname);
+      res = read_base64 (inname);
   else
-    return read_stduu (inname);
+       res = read_stduu (inname);
+  if (dofre)
+      free(outname);
+  return res;
 }
 
 int uudecode_main (int argc,
@@ -288,7 +295,7 @@ int uudecode_main (int argc,
       break;
 
      default:
-      usage(uudecode_usage);
+      show_usage();
     }
   }