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