Home | History | Annotate | Line # | Download | only in uudecode
uudecode.c revision 1.2
      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, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)uudecode.c	5.10 (Berkeley) 6/1/90";*/
     36 static char rcsid[] = "$Id: uudecode.c,v 1.2 1993/08/01 18:03:42 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 /*
     40  * uudecode [file ...]
     41  *
     42  * create the specified file, decoding as you go.
     43  * used with uuencode.
     44  */
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 #include <pwd.h>
     48 #include <stdio.h>
     49 #include <string.h>
     50 
     51 char *filename;
     52 
     53 /* ARGSUSED */
     54 main(argc, argv)
     55 	int argc;
     56 	char **argv;
     57 {
     58 	extern int errno;
     59 	int rval;
     60 
     61 	if (*++argv) {
     62 		rval = 0;
     63 		do {
     64 			if (!freopen(filename = *argv, "r", stdin)) {
     65 				(void)fprintf(stderr, "uudecode: %s: %s\n",
     66 				    *argv, strerror(errno));
     67 				rval = 1;
     68 				continue;
     69 			}
     70 			rval |= decode();
     71 		} while (*++argv);
     72 	} else {
     73 		filename = "stdin";
     74 		rval = decode();
     75 	}
     76 	exit(rval);
     77 }
     78 
     79 decode()
     80 {
     81 	extern int errno;
     82 	struct passwd *pw;
     83 	register int n;
     84 	register char ch, *p;
     85 	int mode, n1;
     86 	char buf[MAXPATHLEN];
     87 
     88 	/* search for header line */
     89 	do {
     90 		if (!fgets(buf, sizeof(buf), stdin)) {
     91 			(void)fprintf(stderr,
     92 			    "uudecode: %s: no \"begin\" line\n", filename);
     93 			return(1);
     94 		}
     95 	} while (strncmp(buf, "begin ", 6));
     96 	(void)sscanf(buf, "begin %o %s", &mode, buf);
     97 
     98 	/* handle ~user/file format */
     99 	if (buf[0] == '~') {
    100 		if (!(p = index(buf, '/'))) {
    101 			(void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
    102 			    filename);
    103 			return(1);
    104 		}
    105 		*p++ = NULL;
    106 		if (!(pw = getpwnam(buf + 1))) {
    107 			(void)fprintf(stderr, "uudecode: %s: no user %s.\n",
    108 			    filename, buf);
    109 			return(1);
    110 		}
    111 		n = strlen(pw->pw_dir);
    112 		n1 = strlen(p);
    113 		if (n + n1 + 2 > MAXPATHLEN) {
    114 			(void)fprintf(stderr, "uudecode: %s: path too long.\n",
    115 			    filename);
    116 			return(1);
    117 		}
    118 		bcopy(p, buf + n + 1, n1 + 1);
    119 		bcopy(pw->pw_dir, buf, n);
    120 		buf[n] = '/';
    121 	}
    122 
    123 	/* create output file, set mode */
    124 	if (!freopen(buf, "w", stdout) ||
    125 	    fchmod(fileno(stdout), mode&0666)) {
    126 		(void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
    127 		    filename, strerror(errno));
    128 		return(1);
    129 	}
    130 
    131 	/* for each input line */
    132 	for (;;) {
    133 		if (!fgets(p = buf, sizeof(buf), stdin)) {
    134 			(void)fprintf(stderr, "uudecode: %s: short file.\n",
    135 			    filename);
    136 			return(1);
    137 		}
    138 #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
    139 		/*
    140 		 * `n' is used to avoid writing out all the characters
    141 		 * at the end of the file.
    142 		 */
    143 		if ((n = DEC(*p)) <= 0)
    144 			break;
    145 		for (++p; n > 0; p += 4, n -= 3)
    146 			if (n >= 3) {
    147 				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    148 				putchar(ch);
    149 				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    150 				putchar(ch);
    151 				ch = DEC(p[2]) << 6 | DEC(p[3]);
    152 				putchar(ch);
    153 			}
    154 			else {
    155 				if (n >= 1) {
    156 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    157 					putchar(ch);
    158 				}
    159 				if (n >= 2) {
    160 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    161 					putchar(ch);
    162 				}
    163 				if (n >= 3) {
    164 					ch = DEC(p[2]) << 6 | DEC(p[3]);
    165 					putchar(ch);
    166 				}
    167 			}
    168 	}
    169 	if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
    170 		(void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
    171 		    filename);
    172 		return(1);
    173 	}
    174 	return(0);
    175 }
    176 
    177 usage()
    178 {
    179 	(void)fprintf(stderr, "usage: uudecode [file ...]\n");
    180 	exit(1);
    181 }
    182