Makefile.flags: restrict Wno-constant-logical-operand and Wno-string-plus-int options...
[oweals/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 //config:config UUENCODE
11 //config:       bool "uuencode (4.4 kb)"
12 //config:       default y
13 //config:       help
14 //config:       uuencode is used to uuencode a file.
15
16 //applet:IF_UUENCODE(APPLET(uuencode, BB_DIR_USR_BIN, BB_SUID_DROP))
17
18 //kbuild:lib-$(CONFIG_UUENCODE) += uuencode.o
19
20 //usage:#define uuencode_trivial_usage
21 //usage:       "[-m] [FILE] STORED_FILENAME"
22 //usage:#define uuencode_full_usage "\n\n"
23 //usage:       "Uuencode FILE (or stdin) to stdout\n"
24 //usage:     "\n        -m      Use base64 encoding per RFC1521"
25 //usage:
26 //usage:#define uuencode_example_usage
27 //usage:       "$ uuencode busybox busybox\n"
28 //usage:       "begin 755 busybox\n"
29 //usage:       "<encoded file snipped>\n"
30 //usage:       "$ uudecode busybox busybox > busybox.uu\n"
31 //usage:       "$\n"
32
33 #include "libbb.h"
34
35 enum {
36         SRC_BUF_SIZE = 15*3,  /* This *MUST* be a multiple of 3 */
37         DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
38 };
39
40 int uuencode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41 int uuencode_main(int argc UNUSED_PARAM, char **argv)
42 {
43         struct stat stat_buf;
44         int src_fd = STDIN_FILENO;
45         const char *tbl;
46         mode_t mode;
47         char src_buf[SRC_BUF_SIZE];
48         char dst_buf[DST_BUF_SIZE + 1];
49
50         tbl = bb_uuenc_tbl_std;
51         mode = 0666 & ~umask(0666);
52         if (getopt32(argv, "^" "m" "\0" "-1:?2"/*must have 1 or 2 args*/)) {
53                 tbl = bb_uuenc_tbl_base64;
54         }
55         argv += optind;
56         if (argv[1]) {
57                 src_fd = xopen(argv[0], O_RDONLY);
58                 fstat(src_fd, &stat_buf);
59                 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
60                 argv++;
61         }
62
63         printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, *argv);
64         while (1) {
65                 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
66                 if (!size)
67                         break;
68                 if ((ssize_t)size < 0)
69                         bb_simple_perror_msg_and_die(bb_msg_read_error);
70                 /* Encode the buffer we just read in */
71                 bb_uuencode(dst_buf, src_buf, size, tbl);
72                 bb_putchar('\n');
73                 if (tbl == bb_uuenc_tbl_std) {
74                         bb_putchar(tbl[size]);
75                 }
76                 fflush(stdout);
77                 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
78         }
79         printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n");
80
81         fflush_stdout_and_exit(EXIT_SUCCESS);
82 }