2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
44 static const char rcsid[] =
51 * create the specified file, decoding as you go.
54 #include <sys/param.h>
66 int cflag, iflag, pflag, sflag;
68 static void usage __P((void));
69 int decode __P((void));
70 int decode2 __P((int));
79 while ((ch = getopt(argc, argv, "cips")) != -1) {
82 cflag = 1; /* multiple uudecode'd files */
85 iflag = 1; /* ask before override files */
88 pflag = 1; /* print output to stdout */
91 sflag = 1; /* do not strip pathnames for output */
104 if (!freopen(filename = *argv, "r", stdin)) {
123 /* decode only one file per input stream */
127 /* multiple uudecode'd files */
128 for (flag = 0; ; flag++)
131 else if (feof(stdin))
143 register char ch, *p;
144 int ignore, mode, n1;
145 char buf[MAXPATHLEN];
146 char buffn[MAXPATHLEN]; /* file name buffer */
149 /* search for header line */
151 if (!fgets(buf, sizeof(buf), stdin)) {
152 if (flag) /* no error */
155 warnx("%s: no \"begin\" line", filename);
158 } while (strncmp(buf, "begin ", 6) ||
159 fnmatch("begin [0-7]* *", buf, 0));
161 (void)sscanf(buf, "begin %o %[^\n\r]", &mode, buf);
163 if (!sflag && !pflag) {
164 strncpy(buffn, buf, sizeof(buffn));
165 if (strrchr(buffn, '/') != NULL)
166 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf));
167 if (buf[0] == '\0') {
168 warnx("%s: illegal filename", buffn);
173 /* handle ~user/file format */
175 if (!(p = index(buf, '/'))) {
176 warnx("%s: illegal ~user", filename);
180 if (!(pw = getpwnam(buf + 1))) {
181 warnx("%s: no user %s", filename, buf);
184 n = strlen(pw->pw_dir);
186 if (n + n1 + 2 > MAXPATHLEN) {
187 warnx("%s: path too long", filename);
190 bcopy(p, buf + n + 1, n1 + 1);
191 bcopy(pw->pw_dir, buf, n);
195 /* create output file, set mode */
197 ; /* print to stdout */
200 if (iflag && !access(buf, F_OK)) {
201 (void)fprintf(stderr, "not overwritten: %s\n", buf);
203 } else if (!freopen(buf, "w", stdout) ||
204 fchmod(fileno(stdout), mode&0666)) {
205 warn("%s: %s", buf, filename);
209 strcpy(buffn, buf); /* store file name from header line */
211 /* for each input line */
213 if (!fgets(p = buf, sizeof(buf), stdin)) {
214 warnx("%s: short file", filename);
217 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
218 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
219 /* #define IS_DEC(c) (1) */
221 #define OUT_OF_RANGE \
224 "\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \
225 filename, buffn, 1 + ' ', 077 + ' ' + 1); \
234 * `n' is used to avoid writing out all the characters
235 * at the end of the file.
237 if ((n = DEC(*p)) <= 0)
239 for (++p; n > 0; p += 4, n -= 3)
241 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
242 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
245 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
247 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
249 ch = DEC(p[2]) << 6 | DEC(p[3]);
255 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
257 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
261 if (!(IS_DEC(*(p + 1)) &&
265 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
269 if (!(IS_DEC(*(p + 2)) &&
272 ch = DEC(p[2]) << 6 | DEC(p[3]);
277 if (fgets(buf, sizeof(buf), stdin) == NULL ||
278 (strcmp(buf, "end") && strcmp(buf, "end\n") &&
279 strcmp(buf, "end\r\n"))) {
280 warnx("%s: no \"end\" line", filename);
289 (void)fprintf(stderr, "usage: uudecode [-cips] [file ...]\n");