uudecode.c revision 19078
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
35char 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#ifndef lint
41static char sccsid[] = "@(#)uudecode.c	8.2 (Berkeley) 4/2/94";
42#endif /* not lint */
43
44/*
45 * uudecode [file ...]
46 *
47 * create the specified file, decoding as you go.
48 * used with uuencode.
49 */
50#include <sys/param.h>
51#include <sys/stat.h>
52
53#include <pwd.h>
54#include <stdio.h>
55#include <string.h>
56#include <stdlib.h>
57
58char *filename;
59int cflag, pflag;
60
61void    usage __P((void));
62int	decode __P((void));
63int	decode2 __P((int));
64
65extern int optind;
66
67int
68main(argc, argv)
69	int argc;
70	char *argv[];
71{
72	extern int errno;
73	int rval, ch;
74
75	while ((ch = getopt(argc, argv, "cp")) != EOF) {
76		switch(ch) {
77		case 'c':
78			cflag = 1; /* multiple uudecode'd files */
79			break;
80		case 'p':
81			pflag = 1; /* print output to stdout */
82			break;
83		default:
84			(void)usage();
85		}
86	}
87        argc -= optind;
88        argv += optind;
89
90
91	if (*argv) {
92		rval = 0;
93		do {
94			if (!freopen(filename = *argv, "r", stdin)) {
95				(void)fprintf(stderr, "uudecode: %s: %s\n",
96				    *argv, strerror(errno));
97				rval = 1;
98				continue;
99			}
100			rval |= decode();
101		} while (*++argv);
102	} else {
103		filename = "stdin";
104		rval = decode();
105	}
106	exit(rval);
107}
108
109int
110decode ()
111{
112	int flag;
113
114	/* decode only one file per input stream */
115	if (!cflag)
116		return(decode2(0));
117
118	/* multiple uudecode'd files */
119	for (flag = 0; ; flag++)
120		if (decode2(flag))
121			return(1);
122		else if (feof(stdin))
123			break;
124
125	return(0);
126}
127
128int
129decode2(flag)
130	int flag;
131{
132	extern int errno;
133	struct passwd *pw;
134	register int n;
135	register char ch, *p;
136	int mode, n1;
137	char buf[MAXPATHLEN];
138
139
140	/* search for header line */
141	do {
142		if (!fgets(buf, sizeof(buf), stdin)) {
143			if (flag) /* no error */
144				return(0);
145
146			(void)fprintf(stderr,
147			    "uudecode: %s: no \"begin\" line\n", filename);
148			return(1);
149		}
150	} while (strncmp(buf, "begin ", 6));
151	(void)sscanf(buf, "begin %o %s", &mode, buf);
152
153	/* handle ~user/file format */
154	if (buf[0] == '~') {
155		if (!(p = index(buf, '/'))) {
156			(void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
157			    filename);
158			return(1);
159		}
160		*p++ = NULL;
161		if (!(pw = getpwnam(buf + 1))) {
162			(void)fprintf(stderr, "uudecode: %s: no user %s.\n",
163			    filename, buf);
164			return(1);
165		}
166		n = strlen(pw->pw_dir);
167		n1 = strlen(p);
168		if (n + n1 + 2 > MAXPATHLEN) {
169			(void)fprintf(stderr, "uudecode: %s: path too long.\n",
170			    filename);
171			return(1);
172		}
173		bcopy(p, buf + n + 1, n1 + 1);
174		bcopy(pw->pw_dir, buf, n);
175		buf[n] = '/';
176	}
177
178	/* create output file, set mode */
179	if (pflag)
180		; /* print to stdout */
181
182	else if (!freopen(buf, "w", stdout) ||
183	    fchmod(fileno(stdout), mode&0666)) {
184		(void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
185		    filename, strerror(errno));
186		return(1);
187	}
188
189	/* for each input line */
190	for (;;) {
191		if (!fgets(p = buf, sizeof(buf), stdin)) {
192			(void)fprintf(stderr, "uudecode: %s: short file.\n",
193			    filename);
194			return(1);
195		}
196#define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
197		/*
198		 * `n' is used to avoid writing out all the characters
199		 * at the end of the file.
200		 */
201		if ((n = DEC(*p)) <= 0)
202			break;
203		for (++p; n > 0; p += 4, n -= 3)
204			if (n >= 3) {
205				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
206				putchar(ch);
207				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
208				putchar(ch);
209				ch = DEC(p[2]) << 6 | DEC(p[3]);
210				putchar(ch);
211			}
212			else {
213				if (n >= 1) {
214					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
215					putchar(ch);
216				}
217				if (n >= 2) {
218					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
219					putchar(ch);
220				}
221				if (n >= 3) {
222					ch = DEC(p[2]) << 6 | DEC(p[3]);
223					putchar(ch);
224				}
225			}
226	}
227	if (!fgets(buf, sizeof(buf), stdin) || strncmp(buf, "end", 3) || (buf[3] && buf[3] != '\n')) {
228		(void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
229		    filename);
230		return(1);
231	}
232	return(0);
233}
234
235void
236usage()
237{
238	(void)fprintf(stderr, "usage: uudecode [-cp] [file ...]\n");
239	exit(1);
240}
241