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