uudecode.c revision 1.9 1 /* $NetBSD: uudecode.c,v 1.9 1998/12/19 23:36:07 christos 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.9 1998/12/19 23:36:07 christos 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
65 static int decode __P((void));
66 static void usage __P((void));
67 int main __P((int, char **));
68
69 char *filename;
70
71 int
72 main(argc, argv)
73 int argc;
74 char *argv[];
75 {
76 int rval;
77
78 setlocale(LC_ALL, "");
79
80 while (getopt(argc, argv, "") != -1)
81 usage();
82 argc -= optind;
83 argv += optind;
84
85 if (*argv) {
86 rval = 0;
87 do {
88 if (!freopen(filename = *argv, "r", stdin)) {
89 warnx("%s", *argv);
90 rval = 1;
91 continue;
92 }
93 rval |= decode();
94 } while (*++argv);
95 } else {
96 filename = "stdin";
97 rval = decode();
98 }
99 exit(rval);
100 }
101
102 static int
103 decode()
104 {
105 struct passwd *pw;
106 int n;
107 char ch, *p;
108 int mode, n1;
109 char buf[MAXPATHLEN];
110
111 /* search for header line */
112 do {
113 if (!fgets(buf, sizeof(buf), stdin)) {
114 warnx("%s: no \"begin\" line", filename);
115 return(1);
116 }
117 } while (strncmp(buf, "begin ", 6));
118 (void)sscanf(buf, "begin %o %s", &mode, buf);
119
120 /* handle ~user/file format */
121 if (buf[0] == '~') {
122 if (!(p = strchr(buf, '/'))) {
123 warnx("%s: illegal ~user.", filename);
124 return(1);
125 }
126 *p++ = '\0';
127 if (!(pw = getpwnam(buf + 1))) {
128 warnx("%s: no user %s.", filename, buf);
129 return(1);
130 }
131 n = strlen(pw->pw_dir);
132 n1 = strlen(p);
133 if (n + n1 + 2 > MAXPATHLEN) {
134 warnx("%s: path too long.", filename);
135 return(1);
136 }
137 memmove(buf + n + 1, p, n1 + 1);
138 memmove(buf, pw->pw_dir, n);
139 buf[n] = '/';
140 }
141
142 /* create output file, set mode */
143 if (!freopen(buf, "w", stdout) ||
144 fchmod(fileno(stdout), mode&0666)) {
145 warnx("%s: %s", buf, filename);
146 return(1);
147 }
148
149 /* for each input line */
150 for (;;) {
151 if (!fgets(p = buf, sizeof(buf), stdin)) {
152 warnx("%s: short file.", filename);
153 return(1);
154 }
155 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
156 /*
157 * `n' is used to avoid writing out all the characters
158 * at the end of the file.
159 */
160 if ((n = DEC(*p)) <= 0)
161 break;
162 for (++p; n > 0; p += 4, n -= 3)
163 if (n >= 3) {
164 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
165 putchar(ch);
166 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
167 putchar(ch);
168 ch = DEC(p[2]) << 6 | DEC(p[3]);
169 putchar(ch);
170 }
171 else {
172 if (n >= 1) {
173 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
174 putchar(ch);
175 }
176 if (n >= 2) {
177 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
178 putchar(ch);
179 }
180 if (n >= 3) {
181 ch = DEC(p[2]) << 6 | DEC(p[3]);
182 putchar(ch);
183 }
184 }
185 }
186 if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
187 warnx("%s: no \"end\" line.", filename);
188 return(1);
189 }
190 return(0);
191 }
192
193 static void
194 usage()
195 {
196 (void)fprintf(stderr, "usage: uudecode [file ...]\n");
197 exit(1);
198 }
199