1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "libbb.h"
25
26enum {
27 SRC_BUF_SIZE = 15*3,
28 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
29};
30
31int uuencode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
32int uuencode_main(int argc UNUSED_PARAM, char **argv)
33{
34 struct stat stat_buf;
35 int src_fd = STDIN_FILENO;
36 const char *tbl;
37 mode_t mode;
38 char src_buf[SRC_BUF_SIZE];
39 char dst_buf[DST_BUF_SIZE + 1];
40
41 tbl = bb_uuenc_tbl_std;
42 mode = 0666 & ~umask(0666);
43 opt_complementary = "-1:?2";
44 if (getopt32(argv, "m")) {
45 tbl = bb_uuenc_tbl_base64;
46 }
47 argv += optind;
48 if (argv[1]) {
49 src_fd = xopen(argv[0], O_RDONLY);
50 fstat(src_fd, &stat_buf);
51 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
52 argv++;
53 }
54
55 printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, *argv);
56 while (1) {
57 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
58 if (!size)
59 break;
60 if ((ssize_t)size < 0)
61 bb_perror_msg_and_die(bb_msg_read_error);
62
63 bb_uuencode(dst_buf, src_buf, size, tbl);
64 bb_putchar('\n');
65 if (tbl == bb_uuenc_tbl_std) {
66 bb_putchar(tbl[size]);
67 }
68 fflush(stdout);
69 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
70 }
71 printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n");
72
73 fflush_stdout_and_exit(EXIT_SUCCESS);
74}
75