dont barf if getuid fails (yellowpages suck)
[unix-history] / usr / src / usr.bin / uuencode / uuencode.c
CommitLineData
7774eca1 1#ifndef lint
612884c9 2static char sccsid[] = "@(#)uuencode.c 5.4 (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) {
35 printf("Usage: uuencode [infile] remotefile\n");
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)
56FILE *in;
57FILE *out;
58{
59 char buf[80];
60 int i, n;
61
62 for (;;) {
63 /* 1 (up to) 45 character line */
64 n = fr(in, buf, 45);
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)
80char *p;
81FILE *f;
82{
83 int c1, c2, c3, c4;
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}
94
95/* fr: like read but stdio */
96int
97fr(fd, buf, cnt)
98FILE *fd;
99char *buf;
100int cnt;
101{
102 int c, i;
103
104 for (i=0; i<cnt; i++) {
105 c = getc(fd);
106 if (c == EOF)
107 return(i);
108 buf[i] = c;
109 }
110 return (cnt);
111}