Home | History | Annotate | Line # | Download | only in uudecode
uudecode.c revision 1.14
      1 /*	$NetBSD: uudecode.c,v 1.14 2002/01/31 19:31:31 tv 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 #include <sys/cdefs.h>
     37 #if defined(__COPYRIGHT) && !defined(lint)
     38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif
     41 
     42 #if defined(__RCSID) && !defined(lint)
     43 #if 0
     44 static char sccsid[] = "@(#)uudecode.c	8.2 (Berkeley) 4/2/94";
     45 #endif
     46 __RCSID("$NetBSD: uudecode.c,v 1.14 2002/01/31 19:31:31 tv Exp $");
     47 #endif /* not lint */
     48 
     49 #if HAVE_CONFIG_H
     50 #include "config.h"
     51 #endif
     52 
     53 /*
     54  * uudecode [file ...]
     55  *
     56  * create the specified file, decoding as you go.
     57  * used with uuencode.
     58  */
     59 #include <sys/param.h>
     60 #include <sys/stat.h>
     61 #include <ctype.h>
     62 #include <err.h>
     63 #include <errno.h>
     64 #include <limits.h>
     65 #include <locale.h>
     66 #include <pwd.h>
     67 #include <stdio.h>
     68 #include <stdlib.h>
     69 #include <string.h>
     70 #include <unistd.h>
     71 
     72 static int decode __P((void));
     73 static void usage __P((void));
     74 int main __P((int, char **));
     75 
     76 int pflag;
     77 char *filename;
     78 
     79 int
     80 main(argc, argv)
     81 	int argc;
     82 	char *argv[];
     83 {
     84 	int ch, rval;
     85 
     86 	setlocale(LC_ALL, "");
     87 
     88 	pflag = 0;
     89 	while ((ch = getopt(argc, argv, "p")) != -1)
     90 		switch (ch) {
     91 		case 'p':
     92 			pflag = 1;
     93 			break;
     94 		default:
     95 			usage();
     96 		}
     97 	argc -= optind;
     98 	argv += optind;
     99 
    100 	if (*argv) {
    101 		rval = 0;
    102 		do {
    103 			if (!freopen(filename = *argv, "r", stdin)) {
    104 				warnx("%s", *argv);
    105 				rval = 1;
    106 				continue;
    107 			}
    108 			rval |= decode();
    109 		} while (*++argv);
    110 	} else {
    111 		filename = "stdin";
    112 		rval = decode();
    113 	}
    114 	exit(rval);
    115 }
    116 
    117 static int
    118 decode()
    119 {
    120 	struct passwd *pw;
    121 	int n;
    122 	char ch, *p;
    123 	int n1;
    124 	long mode;
    125 	char *fn;
    126 	char buf[MAXPATHLEN];
    127 
    128 	/* search for header line */
    129 	do {
    130 		if (!fgets(buf, sizeof(buf), stdin)) {
    131 			warnx("%s: no \"begin\" line", filename);
    132 			return(1);
    133 		}
    134 	} while (strncmp(buf, "begin ", 6));
    135         /* must be followed by an octal mode and a space */
    136 	mode = strtol(buf + 6, &fn, 8);
    137 	if (fn == (buf+6) || !isspace(*fn) || mode==LONG_MIN || mode==LONG_MAX)
    138 	{
    139 	        warnx("%s: invalid mode on \"begin\" line\n", filename);
    140 		return(1);
    141 	}
    142 	/* skip whitespace for file name */
    143 	while (*fn && isspace(*fn)) fn++;
    144 	if (*fn == 0) {
    145                 warnx("%s: no filename on \"begin\" line\n", filename);
    146 		return(1);
    147 	}
    148 	/* zap newline */
    149 	for (p = fn; *p && *p != '\n'; p++)
    150 	        ;
    151 	if (*p) *p = 0;
    152 
    153 	/* handle ~user/file format */
    154 	if (*fn == '~') {
    155 		if (!(p = strchr(fn, '/'))) {
    156 			warnx("%s: illegal ~user.", filename);
    157 			return(1);
    158 		}
    159 		*p++ = '\0';
    160 		if (!(pw = getpwnam(fn + 1))) {
    161 			warnx("%s: no user %s.", filename, buf);
    162 			return(1);
    163 		}
    164 		n = strlen(pw->pw_dir);
    165 		n1 = strlen(p);
    166 		if (n + n1 + 2 > MAXPATHLEN) {
    167 			warnx("%s: path too long.", filename);
    168 			return(1);
    169 		}
    170 		/* make space at beginning of buf by moving end of pathname */
    171 		memmove(buf + n + 1, p, n1 + 1);
    172 		memmove(buf, pw->pw_dir, n);
    173 		buf[n] = '/';
    174 		fn = buf;
    175 	}
    176 
    177 	/* create output file, set mode */
    178 	if (!pflag && (!freopen(fn, "w", stdout) ||
    179 	    fchmod(fileno(stdout), mode & 0666))) {
    180 		warnx("%s: %s", fn, filename);
    181 		return(1);
    182 	}
    183 
    184 	/* for each input line */
    185 	for (;;) {
    186 		if (!fgets(p = buf, sizeof(buf), stdin)) {
    187 			warnx("%s: short file.", filename);
    188 			return(1);
    189 		}
    190 #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
    191 		/*
    192 		 * `n' is used to avoid writing out all the characters
    193 		 * at the end of the file.
    194 		 */
    195 		if ((n = DEC(*p)) <= 0)
    196 			break;
    197 		for (++p; n > 0; p += 4, n -= 3)
    198 			if (n >= 3) {
    199 				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    200 				putchar(ch);
    201 				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    202 				putchar(ch);
    203 				ch = DEC(p[2]) << 6 | DEC(p[3]);
    204 				putchar(ch);
    205 			}
    206 			else {
    207 				if (n >= 1) {
    208 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    209 					putchar(ch);
    210 				}
    211 				if (n >= 2) {
    212 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    213 					putchar(ch);
    214 				}
    215 				if (n >= 3) {
    216 					ch = DEC(p[2]) << 6 | DEC(p[3]);
    217 					putchar(ch);
    218 				}
    219 			}
    220 	}
    221 	if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
    222 		warnx("%s: no \"end\" line.", filename);
    223 		return(1);
    224 	}
    225 	return(0);
    226 }
    227 
    228 static void
    229 usage()
    230 {
    231 	(void)fprintf(stderr, "usage: uudecode [-p] [file ...]\n");
    232 	exit(1);
    233 }
    234