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.
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 $
48 static char sccsid
[] = "@(#)uudecode.c 5.5 (Berkeley) 7/6/88";
51 #ifdef __MSDOS__ /* For Turbo C */
66 * create the specified file, decoding as you go.
75 #if !defined(MSDOS) && !defined(WIN32)
78 #include <sys/types.h> /* MSDOS, WIN32, or UNIX */
84 static void decode(FILE *, FILE *);
85 static void outdec(char *, FILE *, int);
87 /* single-character decode */
88 #define DEC(c) (((c) - ' ') & 077)
100 /* optional input arg */
102 if ((in
= fopen(argv
[1], "r")) == NULL
) {
112 printf("Usage: uudecode [infile]\n");
116 /* search for header line */
118 if (fgets(buf
, sizeof buf
, in
) == NULL
) {
119 fprintf(stderr
, "No begin line\n");
122 if (strncmp(buf
, "begin ", 6) == 0)
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] == '~') {
131 struct passwd
*getpwnam();
133 char dnbuf
[100], *index(), *strcat(), *strcpy();
135 sl
= index(dest
, '/');
137 fprintf(stderr
, "Illegal ~user\n");
141 user
= getpwnam(dest
+ 1);
143 fprintf(stderr
, "No such user as %s\n", dest
);
146 strcpy(dnbuf
, user
->pw_dir
);
151 #endif /* !defined(MSDOS) && !defined(VMS) */
153 /* create output file */
154 #if defined(MSDOS) || defined(WIN32)
155 out
= fopen(dest
, "wb"); /* Binary file */
157 out
= fopen(dest
, "w");
163 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32) /* i.e., UNIX */
169 if (fgets(buf
, sizeof buf
, in
) == NULL
|| strcmp(buf
, "end\n")) {
170 fprintf(stderr
, "No end line\n");
179 * copy from in to out, decoding as you go along.
191 /* for each input line */
192 if (fgets(buf
, sizeof buf
, in
) == NULL
) {
193 printf("Short file\n");
197 if ((n
<= 0) || (buf
[0] == '\n'))
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
++)
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.
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]);
239 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
241 * Return the ptr in sp at which the character c appears;
251 register char *sp
, c
;