uudecode.c revision 1.20 1 /* $NetBSD: uudecode.c,v 1.20 2005/06/29 20:35:32 wiz 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if !defined(lint)
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #if 0
41 static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
42 #endif
43 __RCSID("$NetBSD: uudecode.c,v 1.20 2005/06/29 20:35:32 wiz Exp $");
44 #endif /* not lint */
45
46 /*
47 * uudecode [file ...]
48 *
49 * create the specified file, decoding as you go.
50 * used with uuencode.
51 */
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <limits.h>
58 #include <locale.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 static int decode(void);
66 static void usage(void);
67 int main(int, char *[]);
68
69 int pflag;
70 char *filename;
71
72 int
73 main(int argc, char *argv[])
74 {
75 int ch, rval;
76
77 setlocale(LC_ALL, "");
78 setprogname(argv[0]);
79
80 pflag = 0;
81 while ((ch = getopt(argc, argv, "p")) != -1)
82 switch (ch) {
83 case 'p':
84 pflag = 1;
85 break;
86 default:
87 usage();
88 }
89 argc -= optind;
90 argv += optind;
91
92 if (*argv) {
93 rval = 0;
94 do {
95 if (!freopen(filename = *argv, "r", stdin)) {
96 warn("%s", *argv);
97 rval = 1;
98 continue;
99 }
100 rval |= decode();
101 } while (*++argv);
102 } else {
103 filename = "stdin";
104 rval = decode();
105 }
106 exit(rval);
107 }
108
109 static int
110 decode(void)
111 {
112 struct passwd *pw;
113 int n;
114 char ch, *p;
115 int n1;
116 long mode;
117 char *fn;
118 char buf[MAXPATHLEN];
119
120 /* search for header line */
121 do {
122 if (!fgets(buf, sizeof(buf), stdin)) {
123 warnx("%s: no \"begin\" line", filename);
124 return(1);
125 }
126 } while (strncmp(buf, "begin ", 6));
127 /* must be followed by an octal mode and a space */
128 mode = strtol(buf + 6, &fn, 8);
129 if (fn == (buf+6) || !isspace((unsigned char)*fn) || mode==LONG_MIN || mode==LONG_MAX)
130 {
131 warnx("%s: invalid mode on \"begin\" line", filename);
132 return(1);
133 }
134 /* skip whitespace for file name */
135 while (*fn && isspace((unsigned char)*fn)) fn++;
136 if (*fn == 0) {
137 warnx("%s: no filename on \"begin\" line", filename);
138 return(1);
139 }
140 /* zap newline */
141 for (p = fn; *p && *p != '\n'; p++)
142 ;
143 if (*p) *p = 0;
144
145 /* handle ~user/file format */
146 if (*fn == '~') {
147 if (!(p = strchr(fn, '/'))) {
148 warnx("%s: illegal ~user.", filename);
149 return(1);
150 }
151 *p++ = '\0';
152 if (!(pw = getpwnam(fn + 1))) {
153 warnx("%s: no user %s.", filename, buf);
154 return(1);
155 }
156 n = strlen(pw->pw_dir);
157 n1 = strlen(p);
158 if (n + n1 + 2 > MAXPATHLEN) {
159 warnx("%s: path too long.", filename);
160 return(1);
161 }
162 /* make space at beginning of buf by moving end of pathname */
163 memmove(buf + n + 1, p, n1 + 1);
164 memmove(buf, pw->pw_dir, n);
165 buf[n] = '/';
166 fn = buf;
167 }
168
169 /* create output file, set mode */
170 if (!pflag && (!freopen(fn, "w", stdout) ||
171 fchmod(fileno(stdout), mode & 0666))) {
172 warn("%s: %s", fn, filename);
173 return(1);
174 }
175
176 /* for each input line */
177 for (;;) {
178 if (!fgets(p = buf, sizeof(buf), stdin)) {
179 warnx("%s: short file.", filename);
180 return(1);
181 }
182 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
183 /*
184 * `n' is used to avoid writing out all the characters
185 * at the end of the file.
186 */
187 if ((n = DEC(*p)) <= 0)
188 break;
189 for (++p; n > 0; p += 4, n -= 3)
190 if (n >= 3) {
191 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
192 putchar(ch);
193 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
194 putchar(ch);
195 ch = DEC(p[2]) << 6 | DEC(p[3]);
196 putchar(ch);
197 }
198 else {
199 if (n >= 1) {
200 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
201 putchar(ch);
202 }
203 if (n >= 2) {
204 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
205 putchar(ch);
206 }
207 if (n >= 3) {
208 ch = DEC(p[2]) << 6 | DEC(p[3]);
209 putchar(ch);
210 }
211 }
212 }
213 if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
214 warnx("%s: no \"end\" line.", filename);
215 return(1);
216 }
217 return(0);
218 }
219
220 static void
221 usage()
222 {
223 (void)fprintf(stderr, "usage: %s [-p] [file ...]\n",
224 getprogname());
225 exit(1);
226 }
227