xref: /netbsd-src/usr.bin/uudecode/uudecode.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * 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
35 /*static char sccsid[] = "from: @(#)uudecode.c	5.10 (Berkeley) 6/1/90";*/
36 static char rcsid[] = "$Id: uudecode.c,v 1.4 1993/11/09 01:46:38 jtc Exp $";
37 #endif /* not lint */
38 
39 /*
40  * uudecode [file ...]
41  *
42  * create the specified file, decoding as you go.
43  * used with uuencode.
44  */
45 #include <stdio.h>
46 #include <string.h>
47 #include <locale.h>
48 #include <errno.h>
49 #include <sys/param.h>
50 #include <sys/stat.h>
51 #include <pwd.h>
52 #include <unistd.h>
53 
54 static int decode();
55 static void usage();
56 char *filename;
57 
58 /* ARGSUSED */
59 int
60 main(argc, argv)
61 	int argc;
62 	char **argv;
63 {
64 	int rval;
65 
66 	setlocale(LC_ALL, "");
67 
68 	while (getopt(argc, argv, "") != -1)
69 		usage();
70 	argc -= optind;
71 	argv += optind;
72 
73 	if (*argv) {
74 		rval = 0;
75 		do {
76 			if (!freopen(filename = *argv, "r", stdin)) {
77 				(void)fprintf(stderr, "uudecode: %s: %s\n",
78 				    *argv, strerror(errno));
79 				rval = 1;
80 				continue;
81 			}
82 			rval |= decode();
83 		} while (*++argv);
84 	} else {
85 		filename = "stdin";
86 		rval = decode();
87 	}
88 	exit(rval);
89 }
90 
91 static int
92 decode()
93 {
94 	extern int errno;
95 	struct passwd *pw;
96 	register int n;
97 	register char ch, *p;
98 	int mode, n1;
99 	char buf[MAXPATHLEN];
100 
101 	/* search for header line */
102 	do {
103 		if (!fgets(buf, sizeof(buf), stdin)) {
104 			(void)fprintf(stderr,
105 			    "uudecode: %s: no \"begin\" line\n", filename);
106 			return(1);
107 		}
108 	} while (strncmp(buf, "begin ", 6));
109 	(void)sscanf(buf, "begin %o %s", &mode, buf);
110 
111 	/* handle ~user/file format */
112 	if (buf[0] == '~') {
113 		if (!(p = index(buf, '/'))) {
114 			(void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
115 			    filename);
116 			return(1);
117 		}
118 		*p++ = NULL;
119 		if (!(pw = getpwnam(buf + 1))) {
120 			(void)fprintf(stderr, "uudecode: %s: no user %s.\n",
121 			    filename, buf);
122 			return(1);
123 		}
124 		n = strlen(pw->pw_dir);
125 		n1 = strlen(p);
126 		if (n + n1 + 2 > MAXPATHLEN) {
127 			(void)fprintf(stderr, "uudecode: %s: path too long.\n",
128 			    filename);
129 			return(1);
130 		}
131 		bcopy(p, buf + n + 1, n1 + 1);
132 		bcopy(pw->pw_dir, buf, n);
133 		buf[n] = '/';
134 	}
135 
136 	/* create output file, set mode */
137 	if (!freopen(buf, "w", stdout) ||
138 	    fchmod(fileno(stdout), mode&0666)) {
139 		(void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
140 		    filename, strerror(errno));
141 		return(1);
142 	}
143 
144 	/* for each input line */
145 	for (;;) {
146 		if (!fgets(p = buf, sizeof(buf), stdin)) {
147 			(void)fprintf(stderr, "uudecode: %s: short file.\n",
148 			    filename);
149 			return(1);
150 		}
151 #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
152 		/*
153 		 * `n' is used to avoid writing out all the characters
154 		 * at the end of the file.
155 		 */
156 		if ((n = DEC(*p)) <= 0)
157 			break;
158 		for (++p; n > 0; p += 4, n -= 3)
159 			if (n >= 3) {
160 				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
161 				putchar(ch);
162 				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
163 				putchar(ch);
164 				ch = DEC(p[2]) << 6 | DEC(p[3]);
165 				putchar(ch);
166 			}
167 			else {
168 				if (n >= 1) {
169 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
170 					putchar(ch);
171 				}
172 				if (n >= 2) {
173 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
174 					putchar(ch);
175 				}
176 				if (n >= 3) {
177 					ch = DEC(p[2]) << 6 | DEC(p[3]);
178 					putchar(ch);
179 				}
180 			}
181 	}
182 	if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
183 		(void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
184 		    filename);
185 		return(1);
186 	}
187 	return(0);
188 }
189 
190 static void
191 usage()
192 {
193 	(void)fprintf(stderr, "usage: uudecode [file ...]\n");
194 	exit(1);
195 }
196