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