Annotation of src/usr.bin/uudecode/uudecode.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: uudecode.c,v 1.10 2003/06/03 02:56:21 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: uudecode.c,v 1.6 1994/11/17 07:40:43 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.10 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: char copyright[] =
34: "@(#) Copyright (c) 1983, 1993\n\
35: The Regents of the University of California. All rights reserved.\n";
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
40: #endif
1.11 ! deraadt 41: static char rcsid[] = "$OpenBSD: uudecode.c,v 1.10 2003/06/03 02:56:21 millert Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: /*
1.6 dgregor 45: * uudecode [-p] [file ...]
1.1 deraadt 46: *
47: * create the specified file, decoding as you go.
48: * used with uuencode.
1.6 dgregor 49: *
50: * Write to stdout if '-p' is specified. Use this option if you care about
51: * security at all.
1.1 deraadt 52: */
53: #include <stdio.h>
54: #include <string.h>
55: #include <locale.h>
56: #include <errno.h>
57: #include <sys/param.h>
58: #include <sys/stat.h>
59:
60: #include <pwd.h>
61: #include <unistd.h>
62:
1.6 dgregor 63: static int decode(int);
1.11 ! deraadt 64: static void usage(void);
1.1 deraadt 65: char *filename;
66:
67: int
1.11 ! deraadt 68: main(int argc, char *argv[])
1.1 deraadt 69: {
70: int rval;
1.7 millert 71: int ch;
1.6 dgregor 72: int tostdout = 0;
1.1 deraadt 73:
74: setlocale(LC_ALL, "");
75:
1.6 dgregor 76: while ((ch = getopt(argc, argv, "p")) != -1)
77: switch((char)ch) {
78: case 'p':
79: tostdout++;
80: break;
81: case '?':
82: default:
83: usage();
84: }
1.1 deraadt 85: argc -= optind;
86: argv += optind;
87:
88: if (*argv) {
89: rval = 0;
90: do {
91: if (!freopen(filename = *argv, "r", stdin)) {
92: (void)fprintf(stderr, "uudecode: %s: %s\n",
93: *argv, strerror(errno));
94: rval = 1;
95: continue;
96: }
1.6 dgregor 97: rval |= decode(tostdout);
1.1 deraadt 98: } while (*++argv);
99: } else {
100: filename = "stdin";
1.6 dgregor 101: rval = decode(tostdout);
1.1 deraadt 102: }
103: exit(rval);
104: }
105:
106: static int
1.6 dgregor 107: decode(int tostdout)
1.1 deraadt 108: {
109: struct passwd *pw;
1.9 mpech 110: int n;
111: char ch, *p;
1.1 deraadt 112: int mode, n1;
113: char buf[MAXPATHLEN];
114:
115: /* search for header line */
116: do {
117: if (!fgets(buf, sizeof(buf), stdin)) {
118: (void)fprintf(stderr,
119: "uudecode: %s: no \"begin\" line\n", filename);
120: return(1);
121: }
122: } while (strncmp(buf, "begin ", 6));
1.5 deraadt 123: (void)sscanf(buf, "begin %o %1023[^\n\r]", &mode, buf);
1.1 deraadt 124:
125: /* handle ~user/file format */
126: if (buf[0] == '~') {
1.3 millert 127: if (!(p = strchr(buf, '/'))) {
1.1 deraadt 128: (void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
129: filename);
130: return(1);
131: }
132: *p++ = NULL;
133: if (!(pw = getpwnam(buf + 1))) {
134: (void)fprintf(stderr, "uudecode: %s: no user %s.\n",
135: filename, buf);
136: return(1);
137: }
138: n = strlen(pw->pw_dir);
139: n1 = strlen(p);
140: if (n + n1 + 2 > MAXPATHLEN) {
141: (void)fprintf(stderr, "uudecode: %s: path too long.\n",
142: filename);
143: return(1);
144: }
145: bcopy(p, buf + n + 1, n1 + 1);
146: bcopy(pw->pw_dir, buf, n);
147: buf[n] = '/';
148: }
149:
1.6 dgregor 150: if (!tostdout) {
151: /* create output file, set mode */
152: if (!freopen(buf, "w", stdout) ||
153: fchmod(fileno(stdout), mode&0666)) {
154: (void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
155: filename, strerror(errno));
156: return(1);
157: }
1.1 deraadt 158: }
159:
160: /* for each input line */
161: for (;;) {
162: if (!fgets(p = buf, sizeof(buf), stdin)) {
163: (void)fprintf(stderr, "uudecode: %s: short file.\n",
164: filename);
165: return(1);
166: }
167: #define DEC(c) (((c) - ' ') & 077) /* single character decode */
168: /*
169: * `n' is used to avoid writing out all the characters
170: * at the end of the file.
171: */
172: if ((n = DEC(*p)) <= 0)
173: break;
174: for (++p; n > 0; p += 4, n -= 3)
175: if (n >= 3) {
176: ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
177: putchar(ch);
178: ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
179: putchar(ch);
180: ch = DEC(p[2]) << 6 | DEC(p[3]);
181: putchar(ch);
182: }
183: else {
184: if (n >= 1) {
185: ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
186: putchar(ch);
187: }
188: if (n >= 2) {
189: ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
190: putchar(ch);
191: }
192: if (n >= 3) {
193: ch = DEC(p[2]) << 6 | DEC(p[3]);
194: putchar(ch);
195: }
196: }
197: }
198: if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
199: (void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
200: filename);
201: return(1);
202: }
203: return(0);
204: }
205:
206: static void
1.11 ! deraadt 207: usage(void)
1.1 deraadt 208: {
1.6 dgregor 209: (void)fprintf(stderr, "usage: uudecode [-p] [file ...]\n");
1.1 deraadt 210: exit(1);
211: }