disclosing genocided/extinct monsters
[aNetHack.git] / sys / share / uudecode.c
blobb998e4698e190828ac7f96bca679f1d0c5a43e08
1 /*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * Modified 12 April 1990 by Mark Adler for use on MSDOS systems with
20 * Microsoft C and Turbo C.
22 * Modifed 13 February 1991 by Greg Roelofs for use on VMS systems. As
23 * with the MS-DOS version, the setting of the file mode has been disabled.
24 * Compile and link normally (but note that the shared-image link option
25 * produces a binary only 6 blocks long, as opposed to the 137-block one
26 * produced by an ordinary link). To set up the VMS symbol to run the
27 * program ("run uudecode filename" won't work), do:
28 * uudecode :== "$disk:[directory]uudecode.exe"
29 * and don't forget the leading "$" or it still won't work. The binaries
30 * produced by this program are in VMS "stream-LF" format; this makes no
31 * difference to VMS when running decoded executables, nor to VMS unzip,
32 * but other programs such as zoo or arc may or may not require the file
33 * to be "BILFed" (or "unBILFed" or whatever). Also, unlike the other
34 * flavors, VMS files don't get overwritten (a higher version is created).
36 * Modified 13 April 1991 by Gary Mussar to be forgiving of systems that
37 * appear to be stripping trailing blanks.
39 * Modified 28 February 2002 for use on WIN32 systems with Microsoft C.
41 * Modified 08 July 2006 to cast strlen() result to int to suppress a
42 * warning on platforms where size_t > sizeof(int).
44 * $NHDT-Date: 1432512787 2015/05/25 00:13:07 $ $NHDT-Branch: master $:$NHDT-Revision: 1.7 $
47 #ifndef lint
48 static char sccsid[] = "@(#)uudecode.c 5.5 (Berkeley) 7/6/88";
49 #endif /* not lint */
51 #ifdef __MSDOS__ /* For Turbo C */
52 #define MSDOS 1
53 #endif
55 #ifdef _WIN32
56 #undef MSDOS
57 #undef __MSDOS__
58 #ifndef WIN32
59 #define WIN32
60 #endif
61 #endif
64 * uudecode [input]
66 * create the specified file, decoding as you go.
67 * used with uuencode.
69 #include <stdio.h>
71 #ifdef VMS
72 #include <types.h>
73 #include <stat.h>
74 #else
75 #if !defined(MSDOS) && !defined(WIN32)
76 #include <pwd.h>
77 #endif
78 #include <sys/types.h> /* MSDOS, WIN32, or UNIX */
79 #include <sys/stat.h>
80 #include <string.h>
81 #include <stdlib.h>
82 #endif
84 static void decode(FILE *, FILE *);
85 static void outdec(char *, FILE *, int);
87 /* single-character decode */
88 #define DEC(c) (((c) - ' ') & 077)
90 int
91 main(argc, argv)
92 int argc;
93 char **argv;
95 FILE *in, *out;
96 int mode;
97 char dest[128];
98 char buf[80];
100 /* optional input arg */
101 if (argc > 1) {
102 if ((in = fopen(argv[1], "r")) == NULL) {
103 perror(argv[1]);
104 exit(1);
106 argv++;
107 argc--;
108 } else
109 in = stdin;
111 if (argc != 1) {
112 printf("Usage: uudecode [infile]\n");
113 exit(2);
116 /* search for header line */
117 for (;;) {
118 if (fgets(buf, sizeof buf, in) == NULL) {
119 fprintf(stderr, "No begin line\n");
120 exit(3);
122 if (strncmp(buf, "begin ", 6) == 0)
123 break;
125 (void) sscanf(buf, "begin %o %s", &mode, dest);
127 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
128 /* handle ~user/file format */
129 if (dest[0] == '~') {
130 char *sl;
131 struct passwd *getpwnam();
132 struct passwd *user;
133 char dnbuf[100], *index(), *strcat(), *strcpy();
135 sl = index(dest, '/');
136 if (sl == NULL) {
137 fprintf(stderr, "Illegal ~user\n");
138 exit(3);
140 *sl++ = 0;
141 user = getpwnam(dest + 1);
142 if (user == NULL) {
143 fprintf(stderr, "No such user as %s\n", dest);
144 exit(4);
146 strcpy(dnbuf, user->pw_dir);
147 strcat(dnbuf, "/");
148 strcat(dnbuf, sl);
149 strcpy(dest, dnbuf);
151 #endif /* !defined(MSDOS) && !defined(VMS) */
153 /* create output file */
154 #if defined(MSDOS) || defined(WIN32)
155 out = fopen(dest, "wb"); /* Binary file */
156 #else
157 out = fopen(dest, "w");
158 #endif
159 if (out == NULL) {
160 perror(dest);
161 exit(4);
163 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32) /* i.e., UNIX */
164 chmod(dest, mode);
165 #endif
167 decode(in, out);
169 if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
170 fprintf(stderr, "No end line\n");
171 exit(5);
173 exit(0);
174 /*NOTREACHED*/
175 return 0;
179 * copy from in to out, decoding as you go along.
181 void
182 decode(in, out)
183 FILE *in;
184 FILE *out;
186 char buf[80];
187 char *bp;
188 int n, i, expected;
190 for (;;) {
191 /* for each input line */
192 if (fgets(buf, sizeof buf, in) == NULL) {
193 printf("Short file\n");
194 exit(10);
196 n = DEC(buf[0]);
197 if ((n <= 0) || (buf[0] == '\n'))
198 break;
200 /* Calculate expected # of chars and pad if necessary */
201 expected = ((n + 2) / 3) << 2;
202 for (i = (int) strlen(buf) - 1; i <= expected; i++)
203 buf[i] = ' ';
205 bp = &buf[1];
206 while (n > 0) {
207 outdec(bp, out, n);
208 bp += 4;
209 n -= 3;
215 * output a group of 3 bytes (4 input characters).
216 * the input chars are pointed to by p, they are to
217 * be output to file f. n is used to tell us not to
218 * output all of them at the end of the file.
220 void
221 outdec(p, f, n)
222 char *p;
223 FILE *f;
224 int n;
226 int c1, c2, c3;
228 c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
229 c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
230 c3 = DEC(p[2]) << 6 | DEC(p[3]);
231 if (n >= 1)
232 putc(c1, f);
233 if (n >= 2)
234 putc(c2, f);
235 if (n >= 3)
236 putc(c3, f);
239 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
241 * Return the ptr in sp at which the character c appears;
242 * NULL if not found
245 #ifndef NULL
246 #define NULL 0
247 #endif
249 char *
250 index(sp, c)
251 register char *sp, c;
253 do {
254 if (*sp == c)
255 return (sp);
256 } while (*sp++);
257 return (NULL);
259 #endif