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
,"%s", "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
,"%s", "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
,"%s", "No end line\n");
170 * copy from in to out, decoding as you go along.
182 /* for each input line */
183 if (fgets(buf
, sizeof buf
, in
) == NULL
) {
184 printf("Short file\n");
188 if ((n
<= 0) || (buf
[0] == '\n'))
191 /* Calculate expected # of chars and pad if necessary */
192 expected
= ((n
+2)/3)<<2;
193 for (i
= strlen(buf
)-1; i
<= expected
; i
++) buf
[i
] = ' ';
205 * output a group of 3 bytes (4 input characters).
206 * the input chars are pointed to by p, they are to
207 * be output to file f. n is used to tell us not to
208 * output all of them at the end of the file.
218 c1
= DEC(*p
) << 2 | DEC(p
[1]) >> 4;
219 c2
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
220 c3
= DEC(p
[2]) << 6 | DEC(p
[3]);
229 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
231 * Return the ptr in sp at which the character c appears;
241 register char *sp
, c
;