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