OSDN Git Service

Initial Import
[nethackexpress/trunk.git] / sys / share / uudecode.c
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
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.
16  */
17
18 /*
19  * Modified 12 April 1990 by Mark Adler for use on MSDOS systems with
20  * Microsoft C and Turbo C.
21  *
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).
35  * 
36  * Modified 13 April 1991 by Gary Mussar to be forgiving of systems that
37  * appear to be stripping trailing blanks.
38  *
39  * Modified 28 February 2002 for use on WIN32 systems with Microsoft C.
40  */
41
42 #ifndef lint
43 static char sccsid[] = "@(#)uudecode.c  5.5 (Berkeley) 7/6/88";
44 #endif /* not lint */
45
46 #ifdef __MSDOS__        /* For Turbo C */
47 #define MSDOS 1
48 #endif
49
50 #ifdef _WIN32
51 #undef MSDOS
52 #undef __MSDOS__
53 #ifndef WIN32
54 #define WIN32
55 #endif
56 #endif
57
58 /*
59  * uudecode [input]
60  *
61  * create the specified file, decoding as you go.
62  * used with uuencode.
63  */
64 #include <stdio.h>
65
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
78
79 static void decode(FILE *, FILE *);
80 static void outdec(char *, FILE *, int);
81
82 /* single-character decode */
83 #define DEC(c)  (((c) - ' ') & 077)
84
85 int main(argc, argv)
86 int argc;
87 char **argv;
88 {
89         FILE *in, *out;
90         int mode;
91         char dest[128];
92         char buf[80];
93
94         /* optional input arg */
95         if (argc > 1) {
96                 if ((in = fopen(argv[1], "r")) == NULL) {
97                         perror(argv[1]);
98                         exit(1);
99                 }
100                 argv++; argc--;
101         } else
102                 in = stdin;
103
104         if (argc != 1) {
105                 printf("Usage: uudecode [infile]\n");
106                 exit(2);
107         }
108
109         /* search for header line */
110         for (;;) {
111                 if (fgets(buf, sizeof buf, in) == NULL) {
112                         fprintf(stderr, "No begin line\n");
113                         exit(3);
114                 }
115                 if (strncmp(buf, "begin ", 6) == 0)
116                         break;
117         }
118         (void)sscanf(buf, "begin %o %s", &mode, dest);
119
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();
127
128                 sl = index(dest, '/');
129                 if (sl == NULL) {
130                         fprintf(stderr, "Illegal ~user\n");
131                         exit(3);
132                 }
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);
138                 }
139                 strcpy(dnbuf, user->pw_dir);
140                 strcat(dnbuf, "/");
141                 strcat(dnbuf, sl);
142                 strcpy(dest, dnbuf);
143         }
144 #endif  /* !defined(MSDOS) && !defined(VMS) */
145
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);
155         }
156 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32) /* i.e., UNIX */
157         chmod(dest, mode);
158 #endif
159
160         decode(in, out);
161
162         if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
163                 fprintf(stderr, "No end line\n");
164                 exit(5);
165         }
166         exit(0);
167         /*NOTREACHED*/
168         return 0;
169 }
170
171 /*
172  * copy from in to out, decoding as you go along.
173  */
174 void
175 decode(in, out)
176 FILE *in;
177 FILE *out;
178 {
179         char buf[80];
180         char *bp;
181         int n, i, expected;
182
183         for (;;) {
184                 /* for each input line */
185                 if (fgets(buf, sizeof buf, in) == NULL) {
186                         printf("Short file\n");
187                         exit(10);
188                 }
189                 n = DEC(buf[0]);
190                 if ((n <= 0) || (buf[0] == '\n'))
191                         break;
192
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] = ' ';
196
197                 bp = &buf[1];
198                 while (n > 0) {
199                         outdec(bp, out, n);
200                         bp += 4;
201                         n -= 3;
202                 }
203         }
204 }
205
206 /*
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.
211  */
212 void
213 outdec(p, f, n)
214 char *p;
215 FILE *f;
216 int n;
217 {
218         int c1, c2, c3;
219
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]);
223         if (n >= 1)
224                 putc(c1, f);
225         if (n >= 2)
226                 putc(c2, f);
227         if (n >= 3)
228                 putc(c3, f);
229 }
230
231 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
232 /*
233  * Return the ptr in sp at which the character c appears;
234  * NULL if not found
235  */
236
237 #ifndef NULL
238 #define NULL    0
239 #endif
240
241 char *
242 index(sp, c)
243 register char *sp, c;
244 {
245         do {
246                 if (*sp == c)
247                         return(sp);
248         } while (*sp++);
249         return(NULL);
250 }
251 #endif
252