uudecode.c revision 1.12 1 /* $NetBSD: uudecode.c,v 1.12 1999/03/23 05:55:40 cgd 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.12 1999/03/23 05:55:40 cgd 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 #include <ctype.h>
65 #include <stdlib.h>
66 #include <limits.h>
67
68 static int decode __P((void));
69 static void usage __P((void));
70 int main __P((int, char **));
71
72 int pflag;
73 char *filename;
74
75 int
76 main(argc, argv)
77 int argc;
78 char *argv[];
79 {
80 int ch, rval;
81
82 setlocale(LC_ALL, "");
83
84 pflag = 0;
85 while ((ch = getopt(argc, argv, "p")) != -1)
86 switch (ch) {
87 case 'p':
88 pflag = 1;
89 break;
90 default:
91 usage();
92 }
93 argc -= optind;
94 argv += optind;
95
96 if (*argv) {
97 rval = 0;
98 do {
99 if (!freopen(filename = *argv, "r", stdin)) {
100 warnx("%s", *argv);
101 rval = 1;
102 continue;
103 }
104 rval |= decode();
105 } while (*++argv);
106 } else {
107 filename = "stdin";
108 rval = decode();
109 }
110 exit(rval);
111 }
112
113 static int
114 decode()
115 {
116 struct passwd *pw;
117 int n;
118 char ch, *p;
119 int n1;
120 long mode;
121 char *fn;
122 char buf[MAXPATHLEN];
123
124 /* search for header line */
125 do {
126 if (!fgets(buf, sizeof(buf), stdin)) {
127 warnx("%s: no \"begin\" line", filename);
128 return(1);
129 }
130 } while (strncmp(buf, "begin ", 6));
131 /* must be followed by an octal mode and a space */
132 mode = strtol(buf + 6, &fn, 8);
133 if (fn == (buf+6) || !isspace(*fn) || mode==LONG_MIN || mode==LONG_MAX)
134 {
135 warnx("%s: invalid mode on \"begin\" line\n", filename);
136 return(1);
137 }
138 /* skip whitespace for file name */
139 while (*fn && isspace(*fn)) fn++;
140 if (*fn == 0) {
141 warnx("%s: no filename on \"begin\" line\n", filename);
142 return(1);
143 }
144 /* zap newline */
145 for (p = fn; *p && *p != '\n'; p++)
146 ;
147 if (*p) *p = 0;
148
149 /* handle ~user/file format */
150 if (*fn == '~') {
151 if (!(p = strchr(fn, '/'))) {
152 warnx("%s: illegal ~user.", filename);
153 return(1);
154 }
155 *p++ = '\0';
156 if (!(pw = getpwnam(fn + 1))) {
157 warnx("%s: no user %s.", filename, buf);
158 return(1);
159 }
160 n = strlen(pw->pw_dir);
161 n1 = strlen(p);
162 if (n + n1 + 2 > MAXPATHLEN) {
163 warnx("%s: path too long.", filename);
164 return(1);
165 }
166 /* make space at beginning of buf by moving end of pathname */
167 memmove(buf + n + 1, p, n1 + 1);
168 memmove(buf, pw->pw_dir, n);
169 buf[n] = '/';
170 fn = buf;
171 }
172
173 /* create output file, set mode */
174 if (!pflag && (!freopen(fn, "w", stdout) ||
175 fchmod(fileno(stdout), mode & 0666))) {
176 warnx("%s: %s", fn, filename);
177 return(1);
178 }
179
180 /* for each input line */
181 for (;;) {
182 if (!fgets(p = buf, sizeof(buf), stdin)) {
183 warnx("%s: short file.", filename);
184 return(1);
185 }
186 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
187 /*
188 * `n' is used to avoid writing out all the characters
189 * at the end of the file.
190 */
191 if ((n = DEC(*p)) <= 0)
192 break;
193 for (++p; n > 0; p += 4, n -= 3)
194 if (n >= 3) {
195 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
196 putchar(ch);
197 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
198 putchar(ch);
199 ch = DEC(p[2]) << 6 | DEC(p[3]);
200 putchar(ch);
201 }
202 else {
203 if (n >= 1) {
204 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
205 putchar(ch);
206 }
207 if (n >= 2) {
208 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
209 putchar(ch);
210 }
211 if (n >= 3) {
212 ch = DEC(p[2]) << 6 | DEC(p[3]);
213 putchar(ch);
214 }
215 }
216 }
217 if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
218 warnx("%s: no \"end\" line.", filename);
219 return(1);
220 }
221 return(0);
222 }
223
224 static void
225 usage()
226 {
227 (void)fprintf(stderr, "usage: uudecode [ -p ] [ file ... ]\n");
228 exit(1);
229 }
230