uuencode.c revision 105519
1/*-
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)uuencode.c	8.2 (Berkeley) 4/2/94";
43#endif /* not lint */
44#endif
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/usr.bin/uuencode/uuencode.c 105519 2002-10-20 14:18:25Z fanf $");
48
49/*
50 * uuencode [input] output
51 *
52 * Encode a file so it can be mailed to a remote system.
53 */
54#include <sys/param.h>
55#include <sys/socket.h>
56#include <sys/stat.h>
57
58#include <netinet/in.h>
59
60#include <err.h>
61#include <libgen.h>
62#include <resolv.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <unistd.h>
67
68void encode(void);
69void base64_encode(void);
70static void usage(void);
71
72FILE *output;
73int mode;
74char **av;
75
76int
77main(int argc, char *argv[])
78{
79	struct stat sb;
80	int base64;
81	char ch;
82	char *outfile;
83
84	base64 = 0;
85	outfile = NULL;
86
87	if (strcmp(basename(argv[0]), "b64encode") == 0)
88		base64 = 1;
89
90	while ((ch = getopt(argc, argv, "mo:")) != -1) {
91		switch (ch) {
92		case 'm':
93			base64 = 1;
94			break;
95		case 'o':
96			outfile = optarg;
97			break;
98		case '?':
99		default:
100			usage();
101		}
102	}
103	argv += optind;
104	argc -= optind;
105
106	switch(argc) {
107	case 2:			/* optional first argument is input file */
108		if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb))
109			err(1, "%s", *argv);
110#define	RWX	(S_IRWXU|S_IRWXG|S_IRWXO)
111		mode = sb.st_mode & RWX;
112		++argv;
113		break;
114	case 1:
115#define	RW	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
116		mode = RW & ~umask(RW);
117		break;
118	case 0:
119	default:
120		usage();
121	}
122
123	av = argv;
124
125	if (outfile != NULL) {
126		output = fopen(outfile, "w+");
127		if (output == NULL)
128			err(1, "unable to open %s for output", outfile);
129	} else
130		output = stdout;
131	if (base64)
132		base64_encode();
133	else
134		encode();
135	if (ferror(output))
136		errx(1, "write error");
137	exit(0);
138}
139
140/* ENC is the basic 1 character encoding function to make a char printing */
141#define	ENC(c) ((c) ? ((c) & 077) + ' ': '`')
142
143/*
144 * Copy from in to out, encoding in base64 as you go along.
145 */
146void
147base64_encode(void)
148{
149	/*
150	 * Output must fit into 80 columns, chunks come in 4, leave 1.
151	 */
152#define	GROUPS	((80 / 4) - 1)
153	unsigned char buf[3];
154	char buf2[sizeof(buf) * 2 + 1];
155	size_t n;
156	int rv, sequence;
157
158	sequence = 0;
159
160	fprintf(output, "begin-base64 %o %s\n", mode, *av);
161	while ((n = fread(buf, 1, sizeof(buf), stdin))) {
162		++sequence;
163		rv = b64_ntop(buf, n, buf2, (sizeof(buf2) / sizeof(buf2[0])));
164		if (rv == -1)
165			errx(1, "b64_ntop: error encoding base64");
166		fprintf(output, "%s%s", buf2, (sequence % GROUPS) ? "" : "\n");
167	}
168	if (sequence % GROUPS)
169		fprintf(output, "\n");
170	fprintf(output, "====\n");
171}
172
173/*
174 * Copy from in to out, encoding as you go along.
175 */
176void
177encode(void)
178{
179	register int ch, n;
180	register char *p;
181	char buf[80];
182
183	(void)fprintf(output, "begin %o %s\n", mode, *av);
184	while ((n = fread(buf, 1, 45, stdin))) {
185		ch = ENC(n);
186		if (fputc(ch, output) == EOF)
187			break;
188		for (p = buf; n > 0; n -= 3, p += 3) {
189			/* Pad with nulls if not a multiple of 3. */
190			if (n < 3) {
191				p[2] = '\0';
192				if (n < 2)
193					p[1] = '\0';
194			}
195			ch = *p >> 2;
196			ch = ENC(ch);
197			if (fputc(ch, output) == EOF)
198				break;
199			ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
200			ch = ENC(ch);
201			if (fputc(ch, output) == EOF)
202				break;
203			ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
204			ch = ENC(ch);
205			if (fputc(ch, output) == EOF)
206				break;
207			ch = p[2] & 077;
208			ch = ENC(ch);
209			if (fputc(ch, output) == EOF)
210				break;
211		}
212		if (fputc('\n', output) == EOF)
213			break;
214	}
215	if (ferror(stdin))
216		errx(1, "read error");
217	(void)fprintf(output, "%c\nend\n", ENC('\0'));
218}
219
220static void
221usage(void)
222{
223	(void)fprintf(stderr,
224"usage: uuencode [-m] [-o outfile] [infile] remotefile\n"
225"       b64encode [-o outfile] [infile] remotefile\n");
226	exit(1);
227}
228