removed support for -r, -h, and rmail
[unix-history] / usr / src / usr.bin / uuencode / uuencode.c
CommitLineData
7774eca1 1#ifndef lint
ef49872e 2static char sccsid[] = "@(#)uuencode.c 5.5 (Berkeley) %G%";
7774eca1
SL
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 */
46b15d8a 15#define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
7774eca1
SL
16
17main(argc, argv)
18char **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) {
ef49872e 35 fprintf(stderr,"Usage: uuencode [infile] remotefile\n");
7774eca1
SL
36 exit(2);
37 }
38
39 /* figure out the input file mode */
612884c9
KB
40 if (fstat(fileno(in), &sbuf) < 0 || !isatty(fileno(in)))
41 mode = 0666 & ~umask(0666);
42 else
43 mode = sbuf.st_mode & 0777;
7774eca1
SL
44 printf("begin %o %s\n", mode, argv[1]);
45
46 encode(in, stdout);
47
48 printf("end\n");
49 exit(0);
50}
51
52/*
53 * copy from in to out, encoding as you go along.
54 */
55encode(in, out)
ef49872e
RA
56register FILE *in;
57register FILE *out;
7774eca1
SL
58{
59 char buf[80];
ef49872e 60 register int i, n;
7774eca1
SL
61
62 for (;;) {
63 /* 1 (up to) 45 character line */
ef49872e 64 n = fread(buf, 1, 45, in);
7774eca1
SL
65 putc(ENC(n), out);
66
67 for (i=0; i<n; i += 3)
68 outdec(&buf[i], out);
69
70 putc('\n', out);
71 if (n <= 0)
72 break;
73 }
74}
75
76/*
77 * output one group of 3 bytes, pointed at by p, on file f.
78 */
79outdec(p, f)
ef49872e
RA
80register char *p;
81register FILE *f;
7774eca1 82{
ef49872e 83 register int c1, c2, c3, c4;
7774eca1
SL
84
85 c1 = *p >> 2;
86 c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
87 c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
88 c4 = p[2] & 077;
89 putc(ENC(c1), f);
90 putc(ENC(c2), f);
91 putc(ENC(c3), f);
92 putc(ENC(c4), f);
93}