1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include "libbb.h"
34
35enum {
36 SRC_BUF_SIZE = 15*3,
37 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
38};
39
40int uuencode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41int 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")) {
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_perror_msg_and_die(bb_msg_read_error);
70
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}
83