ls: fix help text: -w N is optional
[platform/upstream/busybox.git] / coreutils / uuencode.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Copyright (C) 2000 by Glenn McGrath
4  *
5  *  based on the function base64_encode from http.c in wget v1.6
6  *  Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10
11 //usage:#define uuencode_trivial_usage
12 //usage:       "[-m] [INFILE] STORED_FILENAME"
13 //usage:#define uuencode_full_usage "\n\n"
14 //usage:       "Uuencode a file to stdout\n"
15 //usage:     "\nOptions:"
16 //usage:     "\n        -m      Use base64 encoding per RFC1521"
17 //usage:
18 //usage:#define uuencode_example_usage
19 //usage:       "$ uuencode busybox busybox\n"
20 //usage:       "begin 755 busybox\n"
21 //usage:       "<encoded file snipped>\n"
22 //usage:       "$ uudecode busybox busybox > busybox.uu\n"
23 //usage:       "$\n"
24
25 #include "libbb.h"
26
27 enum {
28         SRC_BUF_SIZE = 15*3,  /* This *MUST* be a multiple of 3 */
29         DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
30 };
31
32 int uuencode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
33 int uuencode_main(int argc UNUSED_PARAM, char **argv)
34 {
35         struct stat stat_buf;
36         int src_fd = STDIN_FILENO;
37         const char *tbl;
38         mode_t mode;
39         char src_buf[SRC_BUF_SIZE];
40         char dst_buf[DST_BUF_SIZE + 1];
41
42         tbl = bb_uuenc_tbl_std;
43         mode = 0666 & ~umask(0666);
44         opt_complementary = "-1:?2"; /* must have 1 or 2 args */
45         if (getopt32(argv, "m")) {
46                 tbl = bb_uuenc_tbl_base64;
47         }
48         argv += optind;
49         if (argv[1]) {
50                 src_fd = xopen(argv[0], O_RDONLY);
51                 fstat(src_fd, &stat_buf);
52                 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
53                 argv++;
54         }
55
56         printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, *argv);
57         while (1) {
58                 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
59                 if (!size)
60                         break;
61                 if ((ssize_t)size < 0)
62                         bb_perror_msg_and_die(bb_msg_read_error);
63                 /* Encode the buffer we just read in */
64                 bb_uuencode(dst_buf, src_buf, size, tbl);
65                 bb_putchar('\n');
66                 if (tbl == bb_uuenc_tbl_std) {
67                         bb_putchar(tbl[size]);
68                 }
69                 fflush(stdout);
70                 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
71         }
72         printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n");
73
74         fflush_stdout_and_exit(EXIT_SUCCESS);
75 }