usage.c: remove reference to busybox.h
[oweals/busybox.git] / coreutils / uudecode.c
index 60bf7d8c1dce4414157ae5d9c4563abbc3877aa2..779710edda07e6e6ad2c6edc0d6997e914f15e89 100644 (file)
@@ -1,44 +1,29 @@
+/* vi: set sw=4 ts=4: */
 /*
- *  GPLv2
  *  Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License version 2 as published
- *  by the Free Software Foundation; either version 2 of the License.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU Library General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *  Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
  *  Based on specification from
  *  http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
  *
- *  Bugs: the spec doesnt mention anything about "`\n`\n" prior to the "end" line
+ *  Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
+ *        "end" line
  */
 
 
-#include <stdio.h>
-#include <errno.h>
-#include <getopt.h> /* optind */
-#include <string.h>
-#include <stdlib.h>
-#include "busybox.h"
+#include "libbb.h"
 
-static int read_stduu(FILE *src_stream, FILE *dst_stream)
+static void read_stduu(FILE *src_stream, FILE *dst_stream)
 {
        char *line;
 
-       while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
+       while ((line = xmalloc_getline(src_stream)) != NULL) {
                int length;
                char *line_ptr = line;
 
                if (strcmp(line, "end") == 0) {
-                       return(EXIT_SUCCESS);
+                       return;
                }
                length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
 
@@ -47,18 +32,18 @@ static int read_stduu(FILE *src_stream, FILE *dst_stream)
                        continue;
                }
                if (length > 60) {
-                       bb_error_msg_and_die("Line too long");
+                       bb_error_msg_and_die("line too long");
                }
 
                line_ptr++;
-               /* Tolerate an overly long line to acomadate a possible exta '`' */
-               if (strlen(line_ptr) < length) {
-                       bb_error_msg_and_die("Short file");
+               /* Tolerate an overly long line to accomodate a possible exta '`' */
+               if (strlen(line_ptr) < (size_t)length) {
+                       bb_error_msg_and_die("short file");
                }
 
                while (length > 0) {
                        /* Merge four 6 bit chars to three 8 bit chars */
-                   fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
+                       fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
                        line_ptr++;
                        length--;
                        if (length == 0) {
@@ -78,14 +63,14 @@ static int read_stduu(FILE *src_stream, FILE *dst_stream)
                }
                free(line);
        }
-       bb_error_msg_and_die("Short file");
+       bb_error_msg_and_die("short file");
 }
 
-static int read_base64(FILE *src_stream, FILE *dst_stream)
+static void read_base64(FILE *src_stream, FILE *dst_stream)
 {
-       const char *base64_table =
+       static const char base64_table[] =
                "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
-       char term_count = 0;
+       int term_count = 0;
 
        while (1) {
                char translated[4];
@@ -99,7 +84,7 @@ static int read_base64(FILE *src_stream, FILE *dst_stream)
                        do {
                                ch = fgetc(src_stream);
                                if (ch == EOF) {
-                                       bb_error_msg_and_die("Short file");
+                                       bb_error_msg_and_die("short file");
                                }
                        } while ((table_ptr = strchr(base64_table, ch)) == NULL);
 
@@ -116,7 +101,7 @@ static int read_base64(FILE *src_stream, FILE *dst_stream)
                        else if (*table_ptr == '\n') {
                                /* Check for terminating line */
                                if (term_count == 5) {
-                                       return(EXIT_SUCCESS);
+                                       return;
                                }
                                term_count = 1;
                                continue;
@@ -138,63 +123,59 @@ static int read_base64(FILE *src_stream, FILE *dst_stream)
        }
 }
 
-extern int uudecode_main(int argc, char **argv)
+int uudecode_main(int argc, char **argv);
+int uudecode_main(int argc, char **argv)
 {
-       int (*decode_fn_ptr) (FILE * src, FILE * dst);
        FILE *src_stream;
        char *outname = NULL;
        char *line;
-       int opt;
 
-       opt = bb_getopt_ulflags(argc, argv, "o:", &outname);
+       getopt32(argc, argv, "o:", &outname);
 
        if (optind == argc) {
                src_stream = stdin;
        } else if (optind + 1 == argc) {
-               src_stream = bb_xfopen(argv[optind], "r");
+               src_stream = xfopen(argv[optind], "r");
        } else {
                bb_show_usage();
        }
 
        /* Search for the start of the encoding */
-       while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
-               char *line_ptr = NULL;
+       while ((line = xmalloc_getline(src_stream)) != NULL) {
+               void (*decode_fn_ptr)(FILE * src, FILE * dst);
+               char *line_ptr;
+               FILE *dst_stream;
+               int mode;
 
-               if (line == NULL) {
-                       break;
-               } else if (strncmp(line, "begin-base64 ", 13) == 0) {
+               if (strncmp(line, "begin-base64 ", 13) == 0) {
                        line_ptr = line + 13;
                        decode_fn_ptr = read_base64;
                } else if (strncmp(line, "begin ", 6) == 0) {
                        line_ptr = line + 6;
                        decode_fn_ptr = read_stduu;
+               } else {
+                       free(line);
+                       continue;
                }
 
-               if (line_ptr) {
-                       FILE *dst_stream;
-                       int mode;
-                       int ret;
-
-                       mode = strtoul(line_ptr, NULL, 8);
-                       if (outname == NULL) {
-                               outname = strchr(line_ptr, ' ');
-                               if ((outname == NULL) || (*outname == '\0')) {
-                                       break;
-                               }
-                               outname++;
-                       }
-                       if (strcmp(outname, "-") == 0) {
-                               dst_stream = stdout;
-                       } else {
-                               dst_stream = bb_xfopen(outname, "w");
-                               chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
+               mode = strtoul(line_ptr, NULL, 8);
+               if (outname == NULL) {
+                       outname = strchr(line_ptr, ' ');
+                       if ((outname == NULL) || (*outname == '\0')) {
+                               break;
                        }
-                       free(line);
-                       ret = decode_fn_ptr(src_stream, dst_stream);
-                       bb_fclose_nonstdin(src_stream);
-                       return(ret);
+                       outname++;
+               }
+               if (LONE_DASH(outname)) {
+                       dst_stream = stdout;
+               } else {
+                       dst_stream = xfopen(outname, "w");
+                       chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
                }
                free(line);
+               decode_fn_ptr(src_stream, dst_stream);
+               fclose_if_not_stdin(src_stream);
+               return EXIT_SUCCESS;
        }
-       bb_error_msg_and_die("No `begin' line");
+       bb_error_msg_and_die("no 'begin' line");
 }