tools/llvm: Do not build with symbols
[minix3.git] / minix / commands / uud / uud.c
blob3e4a970ff857fc6cff7f9e8f88b69af9501705ce
1 /* uud - bulletproof version of uudecode */
3 /*
4 * Uud -- decode a uuencoded file back to binary form.
6 * From the Berkeley original, modified by MSD, RDR, JPHD & WLS.
7 * The Atari GEMDOS version compiled with MWC 2.x.
8 * The MSDOS version with TurboC.
9 * The Unix version with cc.
10 * this version is made: 25 Nov 1988.
11 * Jan 2 1990: Change system definition and change MSDOS to open the output
12 * file for write binary do cr/lf replacement.
15 #define UNIX 1 /* define one of: UNIX (Minix too!), MSDOS, or GEMDOS */
17 #ifdef GEMDOS
18 #define SYSNAME "gemdos"
19 #define SMALL 1
20 #endif
21 #ifdef MSDOS
22 #define SYSNAME "msdos"
23 #define SMALL 1
24 #endif
25 #ifdef UNIX
26 #define SYSNAME "unix"
27 #endif
29 #include <sys/types.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <stdio.h>
37 #ifdef GEMDOS
38 #include <osbind.h>
39 #define Error(n) { Bconin(2); exit(n); }
40 #else
41 #define Error(n) exit(n)
42 #endif
43 #ifdef UNIX
44 #define WRITE "w"
45 #else
46 #define WRITE "wb" /* for both MSDOS and GEMDOS! */
47 #endif
49 #define loop while (1)
51 #define NCHARS 256
52 #define LINELEN 256
53 #define FILELEN 64
54 #define NORMLEN 60 /* allows for 80 encoded chars per line */
56 #define SEQMAX 'z'
57 #define SEQMIN 'a'
58 char seqc;
59 int first, secnd, check, numl;
61 FILE *in, *out;
62 char *pos;
63 char ifname[FILELEN], ofname[FILELEN];
64 char *source = NULL, *target = NULL;
65 char blank, part = '\0';
66 int partn, lens;
67 int debug = 0, nochk = 0, onedone = 0;
68 int chtbl[NCHARS], cdlen[NORMLEN + 3];
70 int main(int argc, char **argv);
71 char *getnword(char *str, int n);
72 void gettable(void);
73 void decode(void);
74 void getfile(char *buf);
75 void format(char *fp, ...);
76 void doprnt(char *fp, char *ap);
77 void puti(unsigned int i, unsigned int r);
78 void outc(int c);
80 int main(argc, argv) int argc; char *argv[];
82 int mode;
83 register int i, j;
84 char *curarg;
85 char dest[FILELEN], buf[LINELEN];
87 while ((curarg = argv[1]) != NULL && curarg[0] == '-') {
88 if (((curarg[1] == 'd') || (curarg[1] == 'D')) &&
89 (curarg[2] == '\0')) {
90 debug = 1;
91 } else if (((curarg[1] == 'n') || (curarg[1] == 'N')) &&
92 (curarg[2] == '\0')) {
93 nochk = 1;
94 } else if (((curarg[1] == 't') || (curarg[1] == 'T')) &&
95 (curarg[2] == '\0')) {
96 argv++;
97 argc--;
98 if (argc < 2) {
99 format("uud: Missing target directory.\n");
100 Error(15);
102 target = argv[1];
103 if (debug)
104 format("Target dir = %s\n",target);
105 } else if (((curarg[1] == 's') || (curarg[1] == 'S')) &&
106 (curarg[2] == '\0')) {
107 argv++;
108 argc--;
109 if (argc < 2) {
110 format("uud: Missing source directory.\n");
111 Error(15);
113 source = argv[1];
114 if (debug)
115 format("Source dir = %s\n",source);
116 } else if (curarg[1] != '\0') {
117 format("Usage: uud [-n] [-d] [-s dir] [-t dir] [input-file]\n");
118 Error(1);
119 } else
120 break;
121 argv++;
122 argc--;
125 if (curarg == NULL || ((curarg[0] == '-') && (curarg[1] == '\0'))) {
126 in = stdin;
127 strcpy(ifname, "<stdin>");
128 } else {
129 if (source != NULL) {
130 strcpy(ifname, source);
131 strcat(ifname, curarg);
132 } else
133 strcpy(ifname, curarg);
134 if ((in = fopen(ifname, "r")) == NULL) {
135 format("uud: Can't open %s\n", ifname);
136 Error(2);
138 numl = 0;
142 * Set up the default translation table.
144 for (i = 0; i < ' '; i++) chtbl[i] = -1;
145 for (i = ' ', j = 0; i < ' ' + 64; i++, j++) chtbl[i] = j;
146 for (i = ' ' + 64; i < NCHARS; i++) chtbl[i] = -1;
147 chtbl['`'] = chtbl[' ']; /* common mutation */
148 chtbl['~'] = chtbl['^']; /* an other common mutation */
149 blank = ' ';
151 * set up the line length table, to avoid computing lotsa * and / ...
153 cdlen[0] = 1;
154 for (i = 1, j = 5; i <= NORMLEN; i += 3, j += 4)
155 cdlen[i] = (cdlen[i + 1] = (cdlen[i + 2] = j));
157 * search for header or translation table line.
159 loop { /* master loop for multiple decodes in one file */
160 partn = 'a';
161 loop {
162 if (fgets(buf, sizeof buf, in) == NULL) {
163 if (onedone) {
164 if (debug) format("End of file.\n");
165 exit(0);
166 } else {
167 format("uud: No begin line.\n");
168 Error(3);
171 numl++;
172 if (strncmp(buf, "table", (size_t)5) == 0) {
173 gettable();
174 continue;
176 if (strncmp(buf, "begin", (size_t)5) == 0) {
177 break;
180 lens = strlen(buf);
181 if (lens) buf[--lens] = '\0';
182 #ifdef SMALL
183 if ((pos = getnword(buf, 3))) {
184 strcpy(dest, pos);
185 } else
186 #else
187 if(sscanf(buf,"begin%o%s", &mode, dest) != 2)
188 #endif
190 format("uud: Missing filename in begin line.\n");
191 Error(10);
194 if (target != NULL) {
195 strcpy(ofname, target);
196 strcat(ofname, dest);
197 } else
198 strcpy(ofname, dest);
200 if((out = fopen(ofname, WRITE)) == NULL) {
201 format("uud: Cannot open output file: %s\n", ofname);
202 Error(4);
204 if (debug) format("Begin uudecoding: %s\n", ofname);
205 seqc = SEQMAX;
206 check = nochk ? 0 : 1;
207 first = 1;
208 secnd = 0;
209 decode();
210 fclose(out);
211 #ifdef UNIX
212 chmod(ofname, mode);
213 #endif
214 onedone = 1;
215 if (debug) format("End uudecoding: %s\n", ofname);
216 } /* master loop for multiple decodes in one file */
220 * Bring back a pointer to the start of the nth word.
222 char *getnword(str, n) register char *str; register int n;
224 while((*str == '\t') || (*str == ' ')) str++;
225 if (! *str) return NULL;
226 while(--n) {
227 while ((*str != '\t') && (*str != ' ') && (*str)) str++;
228 if (! *str) return NULL;
229 while((*str == '\t') || (*str == ' ')) str++;
230 if (! *str) return NULL;
232 return str;
236 * Install the table in memory for later use.
238 void gettable()
240 char buf[LINELEN];
241 register int c, n = 0;
242 register char *cpt;
244 for (c = 0; c < NCHARS; c++) chtbl[c] = -1;
246 again: if (fgets(buf, sizeof buf, in) == NULL) {
247 format("uud: EOF while in translation table.\n");
248 Error(5);
250 numl++;
251 if (strncmp(buf, "begin", (size_t)5) == 0) {
252 format("uud: Incomplete translation table.\n");
253 Error(6);
255 cpt = buf + strlen(buf) - 1;
256 *cpt = ' ';
257 while (*(cpt) == ' ') {
258 *cpt = 0;
259 cpt--;
261 cpt = buf;
262 while ((c = *cpt)) {
263 if (chtbl[c] != -1) {
264 format("uud: Duplicate char in translation table.\n");
265 Error(7);
267 if (n == 0) blank = c;
268 chtbl[c] = n++;
269 if (n >= 64) return;
270 cpt++;
272 goto again;
276 * copy from in to out, decoding as you go along.
279 void decode()
281 char buf[LINELEN], outl[LINELEN];
282 register char *bp, *ut;
283 register int *trtbl = chtbl;
284 register int n, c, rlen;
285 register unsigned int len;
287 loop {
288 if (fgets(buf, sizeof buf, in) == NULL) {
289 format("uud: EOF before end.\n");
290 fclose(out);
291 Error(8);
293 numl++;
294 len = strlen(buf);
295 if (len) buf[--len] = '\0';
297 * Is it an unprotected empty line before the end line ?
299 if (len == 0) continue;
301 * Get the binary line length.
303 n = trtbl[(unsigned char)*buf];
304 if (n >= 0) goto decod;
306 * end of uuencoded file ?
308 if (strncmp(buf, "end", (size_t)3) == 0) return;
310 * end of current file ? : get next one.
312 if (strncmp(buf, "include", (size_t)7) == 0) {
313 getfile(buf);
314 continue;
316 format("uud: Bad prefix line %d in file: %s\n",numl, ifname);
317 if (debug) format("Bad line =%s\n",buf);
318 Error(11);
320 * Sequence checking ?
322 decod: rlen = cdlen[n];
324 * Is it the empty line before the end line ?
326 if (n == 0) continue;
328 * Pad with blanks.
330 for (bp = &buf[c = len];
331 c < rlen; c++, bp++) *bp = blank;
333 * Verify if asked for.
335 if (debug) {
336 for (len = 0, bp = buf; len < rlen; len++) {
337 if (trtbl[(unsigned char)*bp] < 0) {
338 format(
339 "Non uuencoded char <%c>, line %d in file: %s\n", *bp, numl, ifname);
340 format("Bad line =%s\n",buf);
341 Error(16);
343 bp++;
347 * All this just to check for uuencodes that append a 'z' to each line....
349 if (secnd && check) {
350 secnd = 0;
351 if (buf[rlen] == SEQMAX) {
352 check = 0;
353 if (debug) format("Sequence check turned off (2).\n");
354 } else
355 if (debug) format("Sequence check on (2).\n");
356 } else if (first && check) {
357 first = 0;
358 secnd = 1;
359 if (buf[rlen] != SEQMAX) {
360 check = 0;
361 if (debug) format("No sequence check (1).\n");
362 } else
363 if (debug) format("Sequence check on (1).\n");
366 * There we check.
368 if (check) {
369 if (buf[rlen] != seqc) {
370 format("uud: Wrong sequence line %d in %s\n",
371 numl, ifname);
372 if (debug)
373 format(
374 "Sequence char is <%c> instead of <%c>.\n", buf[rlen], seqc);
375 Error(18);
377 seqc--;
378 if (seqc < SEQMIN) seqc = SEQMAX;
381 * output a group of 3 bytes (4 input characters).
382 * the input chars are pointed to by p, they are to
383 * be output to file f.n is used to tell us not to
384 * output all of them at the end of the file.
386 ut = outl;
387 len = n;
388 bp = &buf[1];
389 while (n > 0) {
390 *(ut++) = trtbl[(unsigned char)*bp] << 2 | trtbl[(unsigned char)bp[1]] >> 4;
391 n--;
392 if (n) {
393 *(ut++) = (trtbl[(unsigned char)bp[1]] << 4) |
394 (trtbl[(unsigned char)bp[2]] >> 2);
395 n--;
397 if (n) {
398 *(ut++) = trtbl[(unsigned char)bp[2]] << 6 | trtbl[(unsigned char)bp[3]];
399 n--;
401 bp += 4;
403 if ((n = fwrite(outl, (size_t)1, (size_t)len, out)) <= 0) {
404 format("uud: Error on writing decoded file.\n");
405 Error(18);
411 * Find the next needed file, if existing, otherwise try further
412 * on next file.
414 void getfile(buf) register char *buf;
416 if ((pos = getnword(buf, 2)) == NULL) {
417 format("uud: Missing include file name.\n");
418 Error(17);
419 } else
420 if (source != NULL) {
421 strcpy(ifname, source);
422 strcat(ifname, pos);
423 } else
424 strcpy(ifname, pos);
425 #ifdef GEMDOS
426 if (Fattrib(ifname, 0, 0) < 0)
427 #else
428 if (access(ifname, 04))
429 #endif
431 if (debug) {
432 format("Cant find: %s\n", ifname);
433 format("Continuing to read same file.\n");
436 else {
437 if (freopen(ifname, "r", in) == in) {
438 numl = 0;
439 if (debug)
440 format("Reading next section from: %s\n", ifname);
441 } else {
442 format("uud: Freopen abort: %s\n", ifname);
443 Error(9);
446 loop {
447 if (fgets(buf, LINELEN, in) == NULL) {
448 format("uud: No begin line after include: %s\n", ifname);
449 Error(12);
451 numl++;
452 if (strncmp(buf, "table", (size_t)5) == 0) {
453 gettable();
454 continue;
456 if (strncmp(buf, "begin", (size_t)5) == 0) break;
458 lens = strlen(buf);
459 if (lens) buf[--lens] = '\0';
461 * Check the part suffix.
463 if ((pos = getnword(buf, 3)) == NULL ) {
464 format("uud: Missing part name, in included file: %s\n", ifname);
465 Error(13);
466 } else {
467 part = *pos;
468 partn++;
469 if (partn > 'z') partn = 'a';
470 if (part != partn) {
471 format("uud: Part suffix mismatch: <%c> instead of <%c>.\n",
472 part, partn);
473 Error(14);
475 if (debug) format("Reading part %c\n", *pos);
480 * Printf style formatting. (Borrowed from MicroEmacs by Dave Conroy.)
481 * A lot smaller than the full fledged printf.
483 #ifdef __STDC__
484 void format(char *fp, ...)
486 va_list args;
488 va_start (args, fp);
489 doprnt(fp, (char *)&args);
490 va_end(args);
492 #else
493 /* VARARGS1 */
494 void format(fp, args) char *fp;
496 doprnt(fp, (char *)&args);
498 #endif
500 void doprnt(fp, ap)
501 register char *fp;
502 register char *ap;
504 register int c, k;
505 register char *s;
507 while ((c = *fp++) != '\0') {
508 if (c != '%')
509 outc(c);
510 else {
511 c = *fp++;
512 switch (c) {
513 case 'd':
514 puti(*(int *)ap, 10);
515 ap += sizeof(int);
516 break;
518 case 's':
519 s = *(char **)ap;
520 while ((k = *s++) != '\0')
521 outc(k);
522 ap += sizeof(char *);
523 break;
525 case 'c':
526 outc(*(int *)ap);
527 ap += sizeof(int);
528 break;
530 default:
531 outc(c);
538 * Put integer, in radix "r".
540 void puti(i, r)
541 register unsigned int i;
542 register unsigned int r;
544 register unsigned int q, s;
546 if ((q = i / r) != 0)
547 puti(q, r);
548 s = i % r;
549 if (s <= 9)
550 outc(s + '0');
551 else
552 outc(s - 10 + 'A');
554 void outc(c) register char c;
556 #ifdef GEMDOS
557 if (c == '\n') Bconout(2, '\r');
558 Bconout(2, c);
559 #else
560 putchar(c);
561 #endif