2 * Copyright (c) 1983 Regents of the University of California.
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.
43 static char sccsid[] = "@(#)uudecode.c 5.5 (Berkeley) 7/6/88";
46 #ifdef __MSDOS__ /* For Turbo C */
61 * create the specified file, decoding as you go.
70 # if !defined(MSDOS) && !defined(WIN32)
73 # include <sys/types.h> /* MSDOS, WIN32, or UNIX */
74 # include <sys/stat.h>
79 static void decode(FILE *, FILE *);
80 static void outdec(char *, FILE *, int);
82 /* single-character decode */
83 #define DEC(c) (((c) - ' ') & 077)
94 /* optional input arg */
96 if ((in = fopen(argv[1], "r")) == NULL) {
105 printf("Usage: uudecode [infile]\n");
109 /* search for header line */
111 if (fgets(buf, sizeof buf, in) == NULL) {
112 fprintf(stderr, "No begin line\n");
115 if (strncmp(buf, "begin ", 6) == 0)
118 (void)sscanf(buf, "begin %o %s", &mode, dest);
120 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
121 /* handle ~user/file format */
122 if (dest[0] == '~') {
124 struct passwd *getpwnam();
126 char dnbuf[100], *index(), *strcat(), *strcpy();
128 sl = index(dest, '/');
130 fprintf(stderr, "Illegal ~user\n");
134 user = getpwnam(dest+1);
136 fprintf(stderr, "No such user as %s\n", dest);
139 strcpy(dnbuf, user->pw_dir);
144 #endif /* !defined(MSDOS) && !defined(VMS) */
146 /* create output file */
147 #if defined(MSDOS) || defined(WIN32)
148 out = fopen(dest, "wb"); /* Binary file */
150 out = fopen(dest, "w");
156 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32) /* i.e., UNIX */
162 if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
163 fprintf(stderr, "No end line\n");
172 * copy from in to out, decoding as you go along.
184 /* for each input line */
185 if (fgets(buf, sizeof buf, in) == NULL) {
186 printf("Short file\n");
190 if ((n <= 0) || (buf[0] == '\n'))
193 /* Calculate expected # of chars and pad if necessary */
194 expected = ((n+2)/3)<<2;
195 for (i = strlen(buf)-1; i <= expected; i++) buf[i] = ' ';
207 * output a group of 3 bytes (4 input characters).
208 * the input chars are pointed to by p, they are to
209 * be output to file f. n is used to tell us not to
210 * output all of them at the end of the file.
220 c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
221 c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
222 c3 = DEC(p[2]) << 6 | DEC(p[3]);
231 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
233 * Return the ptr in sp at which the character c appears;
243 register char *sp, c;