Use an int to hold the result of fgetc (bug noted by David Kimdon).
[oweals/busybox.git] / coreutils / uudecode.c
index 90bef9238f103c885ad85e446fa12ee54f761d5c..6e7705ed17e24b5d30cb115777531e89659e39b9 100644 (file)
  * along with this product; see the file COPYING.  If not, write to
  * the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  * 02111-1307, USA.
- */
-
-/* Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *       This product includes software developed by the University of
- *       California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
+ * Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93.
  *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ * Original copyright notice is retained at the end of this file.
  */
 
-/* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93.  */
 
-#include "internal.h"
 
 #include <stdio.h>
 #include <errno.h>
 #include <getopt.h>
-#include <pwd.h>
+#include <string.h>
+#include <stdlib.h>
+#include "busybox.h"
+#include "pwd.h"
+#include "grp.h"
 
 /*struct passwd *getpwnam();*/
 
@@ -72,7 +46,7 @@ static int read_stduu (const char *inname)
     char *p;
 
     if (fgets (buf, sizeof(buf), stdin) == NULL) {
-      errorMsg("%s: Short file\n", inname);
+      error_msg("%s: Short file", inname);
       return FALSE;
     }
     p = buf;
@@ -107,7 +81,7 @@ static int read_stduu (const char *inname)
 
   if (fgets (buf, sizeof(buf), stdin) == NULL
       || strcmp (buf, "end\n")) {
-    errorMsg("%s: No `end' line\n", inname);
+    error_msg("%s: No `end' line", inname);
     return FALSE;
   }
 
@@ -157,7 +131,7 @@ static int read_base64 (const char *inname)
     unsigned char *p;
 
     if (fgets (buf, sizeof(buf), stdin) == NULL) {
-      errorMsg("%s: Short file\n", inname);
+      error_msg("%s: Short file", inname);
       return FALSE;
     }
     p = buf;
@@ -165,7 +139,7 @@ static int read_base64 (const char *inname)
     if (memcmp (buf, "====", 4) == 0)
       break;
     if (last_data != 0) {
-      errorMsg("%s: data following `=' padding character\n", inname);
+      error_msg("%s: data following `=' padding character", inname);
       return FALSE;
     }
 
@@ -187,14 +161,14 @@ static int read_base64 (const char *inname)
 
       while ((b64_tab[*p] & '\100') != 0)
         if (*p == '\n' || *p++ == '=') {
-          errorMsg("%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') {
-          errorMsg("%s: illegal line\n", inname);
+          error_msg("%s: illegal line", inname);
           return FALSE;
         }
       if (*p == '=') {
@@ -206,7 +180,7 @@ static int read_base64 (const char *inname)
 
       while (b64_tab[*p] == '\177')
         if (*p++ == '\n') {
-          errorMsg("%s: illegal line\n", inname);
+          error_msg("%s: illegal line", inname);
           return FALSE;
         }
       putchar (c1 << 2 | c2 >> 4);
@@ -227,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) {
-      errorMsg("%s: No `begin' line\n", inname);
+      error_msg("%s: No `begin' line", inname);
       return FALSE;
     }
 
@@ -252,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 {
@@ -263,21 +239,17 @@ static int decode (const char *inname,
       while (*p != '/')
         ++p;
       if (*p == '\0') {
-        errorMsg("%s: Illegal ~user\n", inname);
+        error_msg("%s: Illegal ~user", inname);
         return FALSE;
       }
       *p++ = '\0';
       pw = getpwnam (buf + 1);
       if (pw == NULL) {
-        errorMsg("%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;
     }
   }
 
@@ -286,7 +258,9 @@ static int decode (const char *inname,
       && (freopen (outname, "w", stdout) == NULL
          || chmod (outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO))
          )) {
-    errorMsg("%s: %s %s\n", outname, inname, strerror(errno)); /* */
+    perror_msg("%s", outname); /* */
+    if (dofre)
+       free(outname);
     return FALSE;
   }
 
@@ -295,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,
@@ -318,21 +295,21 @@ int uudecode_main (int argc,
       break;
 
      default:
-      usage(uudecode_usage);
+      show_usage();
     }
   }
 
   if (optind == argc)
-    exit_status = decode ("stdin", outname) == 0 ? TRUE : FALSE;
+    exit_status = decode ("stdin", outname) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
   else {
-    exit_status = TRUE;
+    exit_status = EXIT_SUCCESS;
     do {
       if (freopen (argv[optind], "r", stdin) != NULL) {
         if (decode (argv[optind], outname) != 0)
           exit_status = FALSE;
       } else {
-        errorMsg("%s: %s\n", argv[optind], strerror(errno));
-        exit_status = FALSE;
+        perror_msg("%s", argv[optind]);
+        exit_status = EXIT_FAILURE;
       }
       optind++;
     }
@@ -340,3 +317,37 @@ int uudecode_main (int argc,
   }
   return(exit_status);
 }
+
+/* Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change 
+ *             ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> 
+ *
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+