uudecode.c revision 1590
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
57char *filename;
58
59int
60main(argc, argv)
61	int argc;
62	char *argv[];
63{
64	extern int errno;
65	int rval;
66
67	if (*++argv) {
68		rval = 0;
69		do {
70			if (!freopen(filename = *argv, "r", stdin)) {
71				(void)fprintf(stderr, "uudecode: %s: %s\n",
72				    *argv, strerror(errno));
73				rval = 1;
74				continue;
75			}
76			rval |= decode();
77		} while (*++argv);
78	} else {
79		filename = "stdin";
80		rval = decode();
81	}
82	exit(rval);
83}
84
85decode()
86{
87	extern int errno;
88	struct passwd *pw;
89	register int n;
90	register char ch, *p;
91	int mode, n1;
92	char buf[MAXPATHLEN];
93
94	/* search for header line */
95	do {
96		if (!fgets(buf, sizeof(buf), stdin)) {
97			(void)fprintf(stderr,
98			    "uudecode: %s: no \"begin\" line\n", filename);
99			return(1);
100		}
101	} while (strncmp(buf, "begin ", 6));
102	(void)sscanf(buf, "begin %o %s", &mode, buf);
103
104	/* handle ~user/file format */
105	if (buf[0] == '~') {
106		if (!(p = index(buf, '/'))) {
107			(void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
108			    filename);
109			return(1);
110		}
111		*p++ = NULL;
112		if (!(pw = getpwnam(buf + 1))) {
113			(void)fprintf(stderr, "uudecode: %s: no user %s.\n",
114			    filename, buf);
115			return(1);
116		}
117		n = strlen(pw->pw_dir);
118		n1 = strlen(p);
119		if (n + n1 + 2 > MAXPATHLEN) {
120			(void)fprintf(stderr, "uudecode: %s: path too long.\n",
121			    filename);
122			return(1);
123		}
124		bcopy(p, buf + n + 1, n1 + 1);
125		bcopy(pw->pw_dir, buf, n);
126		buf[n] = '/';
127	}
128
129	/* create output file, set mode */
130	if (!freopen(buf, "w", stdout) ||
131	    fchmod(fileno(stdout), mode&0666)) {
132		(void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
133		    filename, strerror(errno));
134		return(1);
135	}
136
137	/* for each input line */
138	for (;;) {
139		if (!fgets(p = buf, sizeof(buf), stdin)) {
140			(void)fprintf(stderr, "uudecode: %s: short file.\n",
141			    filename);
142			return(1);
143		}
144#define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
145		/*
146		 * `n' is used to avoid writing out all the characters
147		 * at the end of the file.
148		 */
149		if ((n = DEC(*p)) <= 0)
150			break;
151		for (++p; n > 0; p += 4, n -= 3)
152			if (n >= 3) {
153				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
154				putchar(ch);
155				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
156				putchar(ch);
157				ch = DEC(p[2]) << 6 | DEC(p[3]);
158				putchar(ch);
159			}
160			else {
161				if (n >= 1) {
162					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
163					putchar(ch);
164				}
165				if (n >= 2) {
166					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
167					putchar(ch);
168				}
169				if (n >= 3) {
170					ch = DEC(p[2]) << 6 | DEC(p[3]);
171					putchar(ch);
172				}
173			}
174	}
175	if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
176		(void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
177		    filename);
178		return(1);
179	}
180	return(0);
181}
182
183usage()
184{
185	(void)fprintf(stderr, "usage: uudecode [file ...]\n");
186	exit(1);
187}
188