uudecode.c revision 1.10
1/*	$OpenBSD: uudecode.c,v 1.10 2003/06/03 02:56:21 millert Exp $	*/
2/*	$NetBSD: uudecode.c,v 1.6 1994/11/17 07:40:43 jtc Exp $	*/
3
4/*-
5 * Copyright (c) 1983, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33char copyright[] =
34"@(#) Copyright (c) 1983, 1993\n\
35	The Regents of the University of California.  All rights reserved.\n";
36
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)uudecode.c	8.2 (Berkeley) 4/2/94";
40#endif
41static char rcsid[] = "$OpenBSD: uudecode.c,v 1.10 2003/06/03 02:56:21 millert Exp $";
42#endif /* not lint */
43
44/*
45 * uudecode [-p] [file ...]
46 *
47 * create the specified file, decoding as you go.
48 * used with uuencode.
49 *
50 * Write to stdout if '-p' is specified.  Use this option if you care about
51 * security at all.
52 */
53#include <stdio.h>
54#include <string.h>
55#include <locale.h>
56#include <errno.h>
57#include <sys/param.h>
58#include <sys/stat.h>
59
60#include <pwd.h>
61#include <unistd.h>
62
63static int decode(int);
64static void usage();
65char *filename;
66
67int
68main(argc, argv)
69	int argc;
70	char *argv[];
71{
72	int rval;
73	int ch;
74	int tostdout = 0;
75
76	setlocale(LC_ALL, "");
77
78	while ((ch = getopt(argc, argv, "p")) != -1)
79		switch((char)ch) {
80		case 'p':
81			tostdout++;
82			break;
83		case '?':
84		default:
85			usage();
86		}
87	argc -= optind;
88	argv += optind;
89
90	if (*argv) {
91		rval = 0;
92		do {
93			if (!freopen(filename = *argv, "r", stdin)) {
94				(void)fprintf(stderr, "uudecode: %s: %s\n",
95				    *argv, strerror(errno));
96				rval = 1;
97				continue;
98			}
99			rval |= decode(tostdout);
100		} while (*++argv);
101	} else {
102		filename = "stdin";
103		rval = decode(tostdout);
104	}
105	exit(rval);
106}
107
108static int
109decode(int tostdout)
110{
111	struct passwd *pw;
112	int n;
113	char ch, *p;
114	int mode, n1;
115	char buf[MAXPATHLEN];
116
117	/* search for header line */
118	do {
119		if (!fgets(buf, sizeof(buf), stdin)) {
120			(void)fprintf(stderr,
121			    "uudecode: %s: no \"begin\" line\n", filename);
122			return(1);
123		}
124	} while (strncmp(buf, "begin ", 6));
125	(void)sscanf(buf, "begin %o %1023[^\n\r]", &mode, buf);
126
127	/* handle ~user/file format */
128	if (buf[0] == '~') {
129		if (!(p = strchr(buf, '/'))) {
130			(void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
131			    filename);
132			return(1);
133		}
134		*p++ = NULL;
135		if (!(pw = getpwnam(buf + 1))) {
136			(void)fprintf(stderr, "uudecode: %s: no user %s.\n",
137			    filename, buf);
138			return(1);
139		}
140		n = strlen(pw->pw_dir);
141		n1 = strlen(p);
142		if (n + n1 + 2 > MAXPATHLEN) {
143			(void)fprintf(stderr, "uudecode: %s: path too long.\n",
144			    filename);
145			return(1);
146		}
147		bcopy(p, buf + n + 1, n1 + 1);
148		bcopy(pw->pw_dir, buf, n);
149		buf[n] = '/';
150	}
151
152	if (!tostdout) {
153		/* create output file, set mode */
154		if (!freopen(buf, "w", stdout) ||
155		    fchmod(fileno(stdout), mode&0666)) {
156			(void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
157			    filename, strerror(errno));
158			return(1);
159		}
160	}
161
162	/* for each input line */
163	for (;;) {
164		if (!fgets(p = buf, sizeof(buf), stdin)) {
165			(void)fprintf(stderr, "uudecode: %s: short file.\n",
166			    filename);
167			return(1);
168		}
169#define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
170		/*
171		 * `n' is used to avoid writing out all the characters
172		 * at the end of the file.
173		 */
174		if ((n = DEC(*p)) <= 0)
175			break;
176		for (++p; n > 0; p += 4, n -= 3)
177			if (n >= 3) {
178				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
179				putchar(ch);
180				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
181				putchar(ch);
182				ch = DEC(p[2]) << 6 | DEC(p[3]);
183				putchar(ch);
184			}
185			else {
186				if (n >= 1) {
187					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
188					putchar(ch);
189				}
190				if (n >= 2) {
191					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
192					putchar(ch);
193				}
194				if (n >= 3) {
195					ch = DEC(p[2]) << 6 | DEC(p[3]);
196					putchar(ch);
197				}
198			}
199	}
200	if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
201		(void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
202		    filename);
203		return(1);
204	}
205	return(0);
206}
207
208static void
209usage()
210{
211	(void)fprintf(stderr, "usage: uudecode [-p] [file ...]\n");
212	exit(1);
213}
214