Commit | Line | Data |
---|---|---|
7774eca1 SL |
1 | #ifndef lint |
2 | static char sccsid[] = "@(#)uuencode.c 5.1 (Berkeley) %G%"; | |
3 | #endif | |
4 | ||
5 | /* | |
6 | * uuencode [input] output | |
7 | * | |
8 | * Encode a file so it can be mailed to a remote system. | |
9 | */ | |
10 | #include <stdio.h> | |
11 | #include <sys/types.h> | |
12 | #include <sys/stat.h> | |
13 | ||
14 | /* ENC is the basic 1 character encoding function to make a char printing */ | |
15 | #define ENC(c) (((c) & 077) + ' ') | |
16 | ||
17 | main(argc, argv) | |
18 | char **argv; | |
19 | { | |
20 | FILE *in; | |
21 | struct stat sbuf; | |
22 | int mode; | |
23 | ||
24 | /* optional 1st argument */ | |
25 | if (argc > 2) { | |
26 | if ((in = fopen(argv[1], "r")) == NULL) { | |
27 | perror(argv[1]); | |
28 | exit(1); | |
29 | } | |
30 | argv++; argc--; | |
31 | } else | |
32 | in = stdin; | |
33 | ||
34 | if (argc != 2) { | |
35 | printf("Usage: uuencode [infile] remotefile\n"); | |
36 | exit(2); | |
37 | } | |
38 | ||
39 | /* figure out the input file mode */ | |
40 | fstat(fileno(in), &sbuf); | |
41 | mode = sbuf.st_mode & 0777; | |
42 | printf("begin %o %s\n", mode, argv[1]); | |
43 | ||
44 | encode(in, stdout); | |
45 | ||
46 | printf("end\n"); | |
47 | exit(0); | |
48 | } | |
49 | ||
50 | /* | |
51 | * copy from in to out, encoding as you go along. | |
52 | */ | |
53 | encode(in, out) | |
54 | FILE *in; | |
55 | FILE *out; | |
56 | { | |
57 | char buf[80]; | |
58 | int i, n; | |
59 | ||
60 | for (;;) { | |
61 | /* 1 (up to) 45 character line */ | |
62 | n = fr(in, buf, 45); | |
63 | putc(ENC(n), out); | |
64 | ||
65 | for (i=0; i<n; i += 3) | |
66 | outdec(&buf[i], out); | |
67 | ||
68 | putc('\n', out); | |
69 | if (n <= 0) | |
70 | break; | |
71 | } | |
72 | } | |
73 | ||
74 | /* | |
75 | * output one group of 3 bytes, pointed at by p, on file f. | |
76 | */ | |
77 | outdec(p, f) | |
78 | char *p; | |
79 | FILE *f; | |
80 | { | |
81 | int c1, c2, c3, c4; | |
82 | ||
83 | c1 = *p >> 2; | |
84 | c2 = (*p << 4) & 060 | (p[1] >> 4) & 017; | |
85 | c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03; | |
86 | c4 = p[2] & 077; | |
87 | putc(ENC(c1), f); | |
88 | putc(ENC(c2), f); | |
89 | putc(ENC(c3), f); | |
90 | putc(ENC(c4), f); | |
91 | } | |
92 | ||
93 | /* fr: like read but stdio */ | |
94 | int | |
95 | fr(fd, buf, cnt) | |
96 | FILE *fd; | |
97 | char *buf; | |
98 | int cnt; | |
99 | { | |
100 | int c, i; | |
101 | ||
102 | for (i=0; i<cnt; i++) { | |
103 | c = getc(fd); | |
104 | if (c == EOF) | |
105 | return(i); | |
106 | buf[i] = c; | |
107 | } | |
108 | return (cnt); | |
109 | } |