Add another missing extern.h define
[slashemextended.git] / sys / share / uudecode.c
blobc78f231da89bd055d4ff5e3664059281191ee3c7
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.
42 #ifndef lint
43 static char sccsid[] = "@(#)uudecode.c 5.5 (Berkeley) 7/6/88";
44 #endif /* not lint */
46 #ifdef __MSDOS__ /* For Turbo C */
47 #define MSDOS 1
48 #endif
50 #ifdef _WIN32
51 #undef MSDOS
52 #undef __MSDOS__
53 #ifndef WIN32
54 #define WIN32
55 #endif
56 #endif
59 * uudecode [input]
61 * create the specified file, decoding as you go.
62 * used with uuencode.
64 #include <stdio.h>
66 #ifdef VMS
67 # include <types.h>
68 # include <stat.h>
69 #else
70 # if !defined(MSDOS) && !defined(WIN32)
71 # include <pwd.h>
72 # endif
73 # include <sys/types.h> /* MSDOS, WIN32, or UNIX */
74 # include <sys/stat.h>
75 # include <string.h>
76 # include <stdlib.h>
77 #endif
79 static void decode(FILE *, FILE *);
80 static void outdec(char *, FILE *, int);
82 /* single-character decode */
83 #define DEC(c) (((c) - ' ') & 077)
85 int main(argc, argv)
86 int argc;
87 char **argv;
89 FILE *in, *out;
90 int mode;
91 char dest[128];
92 char buf[80];
94 /* optional input arg */
95 if (argc > 1) {
96 if ((in = fopen(argv[1], "r")) == NULL) {
97 perror(argv[1]);
98 exit(1);
100 argv++; argc--;
101 } else
102 in = stdin;
104 if (argc != 1) {
105 printf("Usage: uudecode [infile]\n");
106 exit(2);
109 /* search for header line */
110 for (;;) {
111 if (fgets(buf, sizeof buf, in) == NULL) {
112 fprintf(stderr,"%s", "No begin line\n");
113 exit(3);
115 if (strncmp(buf, "begin ", 6) == 0)
116 break;
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] == '~') {
123 char *sl;
124 struct passwd *getpwnam();
125 struct passwd *user;
126 char dnbuf[100], *index(), *strcat(), *strcpy();
128 sl = index(dest, '/');
129 if (sl == NULL) {
130 fprintf(stderr,"%s", "Illegal ~user\n");
131 exit(3);
133 *sl++ = 0;
134 user = getpwnam(dest+1);
135 if (user == NULL) {
136 fprintf(stderr, "No such user as %s\n", dest);
137 exit(4);
139 strcpy(dnbuf, user->pw_dir);
140 strcat(dnbuf, "/");
141 strcat(dnbuf, sl);
142 strcpy(dest, dnbuf);
144 #endif /* !defined(MSDOS) && !defined(VMS) */
146 /* create output file */
147 #if defined(MSDOS) || defined(WIN32)
148 out = fopen(dest, "wb"); /* Binary file */
149 #else
150 out = fopen(dest, "w");
151 #endif
152 if (out == NULL) {
153 perror(dest);
154 exit(4);
156 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32) /* i.e., UNIX */
157 chmod(dest, mode);
158 #endif
160 decode(in, out);
162 if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
163 fprintf(stderr,"%s", "No end line\n");
164 exit(5);
166 exit(0);
170 * copy from in to out, decoding as you go along.
172 void
173 decode(in, out)
174 FILE *in;
175 FILE *out;
177 char buf[80];
178 char *bp;
179 int n, i, expected;
181 for (;;) {
182 /* for each input line */
183 if (fgets(buf, sizeof buf, in) == NULL) {
184 printf("Short file\n");
185 exit(10);
187 n = DEC(buf[0]);
188 if ((n <= 0) || (buf[0] == '\n'))
189 break;
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] = ' ';
195 bp = &buf[1];
196 while (n > 0) {
197 outdec(bp, out, n);
198 bp += 4;
199 n -= 3;
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.
210 void
211 outdec(p, f, n)
212 char *p;
213 FILE *f;
214 int n;
216 int c1, c2, c3;
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]);
221 if (n >= 1)
222 putc(c1, f);
223 if (n >= 2)
224 putc(c2, f);
225 if (n >= 3)
226 putc(c3, f);
229 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
231 * Return the ptr in sp at which the character c appears;
232 * NULL if not found
235 #ifndef NULL
236 #define NULL 0
237 #endif
239 char *
240 index(sp, c)
241 register char *sp, c;
243 do {
244 if (*sp == c)
245 return(sp);
246 } while (*sp++);
247 return(NULL);
249 #endif