Home | History | Annotate | Line # | Download | only in uudecode
uudecode.c revision 1.3
      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.3 1993/08/27 22:30:54 jtc 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 #include <errno.h>
     51 
     52 static int decode();
     53 static void usage();
     54 char *filename;
     55 
     56 /* ARGSUSED */
     57 int
     58 main(argc, argv)
     59 	int argc;
     60 	char **argv;
     61 {
     62 	int rval;
     63 
     64 	if (*++argv) {
     65 		rval = 0;
     66 		do {
     67 			if (!freopen(filename = *argv, "r", stdin)) {
     68 				(void)fprintf(stderr, "uudecode: %s: %s\n",
     69 				    *argv, strerror(errno));
     70 				rval = 1;
     71 				continue;
     72 			}
     73 			rval |= decode();
     74 		} while (*++argv);
     75 	} else {
     76 		filename = "stdin";
     77 		rval = decode();
     78 	}
     79 	exit(rval);
     80 }
     81 
     82 static int
     83 decode()
     84 {
     85 	extern int errno;
     86 	struct passwd *pw;
     87 	register int n;
     88 	register char ch, *p;
     89 	int mode, n1;
     90 	char buf[MAXPATHLEN];
     91 
     92 	/* search for header line */
     93 	do {
     94 		if (!fgets(buf, sizeof(buf), stdin)) {
     95 			(void)fprintf(stderr,
     96 			    "uudecode: %s: no \"begin\" line\n", filename);
     97 			return(1);
     98 		}
     99 	} while (strncmp(buf, "begin ", 6));
    100 	(void)sscanf(buf, "begin %o %s", &mode, buf);
    101 
    102 	/* handle ~user/file format */
    103 	if (buf[0] == '~') {
    104 		if (!(p = index(buf, '/'))) {
    105 			(void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
    106 			    filename);
    107 			return(1);
    108 		}
    109 		*p++ = NULL;
    110 		if (!(pw = getpwnam(buf + 1))) {
    111 			(void)fprintf(stderr, "uudecode: %s: no user %s.\n",
    112 			    filename, buf);
    113 			return(1);
    114 		}
    115 		n = strlen(pw->pw_dir);
    116 		n1 = strlen(p);
    117 		if (n + n1 + 2 > MAXPATHLEN) {
    118 			(void)fprintf(stderr, "uudecode: %s: path too long.\n",
    119 			    filename);
    120 			return(1);
    121 		}
    122 		bcopy(p, buf + n + 1, n1 + 1);
    123 		bcopy(pw->pw_dir, buf, n);
    124 		buf[n] = '/';
    125 	}
    126 
    127 	/* create output file, set mode */
    128 	if (!freopen(buf, "w", stdout) ||
    129 	    fchmod(fileno(stdout), mode&0666)) {
    130 		(void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
    131 		    filename, strerror(errno));
    132 		return(1);
    133 	}
    134 
    135 	/* for each input line */
    136 	for (;;) {
    137 		if (!fgets(p = buf, sizeof(buf), stdin)) {
    138 			(void)fprintf(stderr, "uudecode: %s: short file.\n",
    139 			    filename);
    140 			return(1);
    141 		}
    142 #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
    143 		/*
    144 		 * `n' is used to avoid writing out all the characters
    145 		 * at the end of the file.
    146 		 */
    147 		if ((n = DEC(*p)) <= 0)
    148 			break;
    149 		for (++p; n > 0; p += 4, n -= 3)
    150 			if (n >= 3) {
    151 				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    152 				putchar(ch);
    153 				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    154 				putchar(ch);
    155 				ch = DEC(p[2]) << 6 | DEC(p[3]);
    156 				putchar(ch);
    157 			}
    158 			else {
    159 				if (n >= 1) {
    160 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    161 					putchar(ch);
    162 				}
    163 				if (n >= 2) {
    164 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    165 					putchar(ch);
    166 				}
    167 				if (n >= 3) {
    168 					ch = DEC(p[2]) << 6 | DEC(p[3]);
    169 					putchar(ch);
    170 				}
    171 			}
    172 	}
    173 	if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
    174 		(void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
    175 		    filename);
    176 		return(1);
    177 	}
    178 	return(0);
    179 }
    180 
    181 static void
    182 usage()
    183 {
    184 	(void)fprintf(stderr, "usage: uudecode [file ...]\n");
    185 	exit(1);
    186 }
    187