Home | History | Annotate | Line # | Download | only in uuencode
uuencode.c revision 1.13
      1  1.13  lukem /*	$NetBSD: uuencode.c,v 1.13 2008/07/21 14:19:27 lukem Exp $	*/
      2   1.6    jtc 
      3   1.6    jtc /*-
      4   1.6    jtc  * Copyright (c) 1983, 1993
      5   1.6    jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1    cgd  *
      7   1.1    cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1    cgd  * modification, are permitted provided that the following conditions
      9   1.1    cgd  * are met:
     10   1.1    cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1    cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1    cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    cgd  *    documentation and/or other materials provided with the distribution.
     15   1.9    agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1    cgd  *    may be used to endorse or promote products derived from this software
     17   1.1    cgd  *    without specific prior written permission.
     18   1.1    cgd  *
     19   1.1    cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1    cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1    cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1    cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1    cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1    cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1    cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1    cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1    cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1    cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1    cgd  * SUCH DAMAGE.
     30   1.1    cgd  */
     31   1.1    cgd 
     32   1.8  lukem #include <sys/cdefs.h>
     33   1.8  lukem #ifndef lint
     34  1.13  lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     35  1.13  lukem  The Regents of the University of California.  All rights reserved.");
     36   1.8  lukem #endif /* not lint */
     37   1.6    jtc 
     38   1.1    cgd #ifndef lint
     39   1.6    jtc #if 0
     40   1.6    jtc static char sccsid[] = "@(#)uuencode.c	8.2 (Berkeley) 4/2/94";
     41   1.8  lukem #else
     42  1.13  lukem __RCSID("$NetBSD: uuencode.c,v 1.13 2008/07/21 14:19:27 lukem Exp $");
     43   1.6    jtc #endif
     44   1.1    cgd #endif /* not lint */
     45   1.1    cgd 
     46   1.1    cgd /*
     47   1.1    cgd  * uuencode [input] output
     48   1.1    cgd  *
     49   1.1    cgd  * Encode a file so it can be mailed to a remote system.
     50   1.1    cgd  */
     51   1.8  lukem #include <sys/types.h>
     52   1.8  lukem #include <sys/stat.h>
     53  1.12   elad #include <netinet/in.h>
     54   1.8  lukem #include <err.h>
     55   1.8  lukem #include <errno.h>
     56   1.8  lukem #include <locale.h>
     57  1.12   elad #include <resolv.h>
     58   1.1    cgd #include <stdio.h>
     59   1.3    jtc #include <stdlib.h>
     60   1.3    jtc #include <string.h>
     61   1.4    jtc #include <unistd.h>
     62   1.1    cgd 
     63  1.10    wiz int main(int, char *[]);
     64  1.10    wiz static void encode(void);
     65  1.12   elad static void base64_encode(void);
     66  1.10    wiz static void usage(void);
     67   1.3    jtc 
     68   1.3    jtc int
     69  1.10    wiz main(int argc, char *argv[])
     70   1.1    cgd {
     71   1.1    cgd 	struct stat sb;
     72  1.12   elad 	int base64, ch, mode;
     73   1.1    cgd 
     74   1.8  lukem 	mode = 0;
     75  1.12   elad 	base64 = 0;
     76   1.5    jtc 	setlocale(LC_ALL, "");
     77  1.11    wiz 	setprogname(argv[0]);
     78   1.5    jtc 
     79  1.12   elad 	while ((ch = getopt(argc, argv, "m")) != -1) {
     80  1.12   elad 		switch(ch) {
     81  1.12   elad 		case 'm':
     82  1.12   elad 			base64 = 1;
     83  1.12   elad 			break;
     84  1.12   elad 		default:
     85  1.12   elad 			usage();
     86  1.12   elad 		}
     87  1.12   elad 	}
     88   1.1    cgd 	argv += optind;
     89   1.1    cgd 	argc -= optind;
     90   1.1    cgd 
     91   1.1    cgd 	switch(argc) {
     92   1.1    cgd 	case 2:			/* optional first argument is input file */
     93   1.8  lukem 		if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb))
     94   1.8  lukem 			err(1, "%s", *argv);
     95   1.1    cgd #define	RWX	(S_IRWXU|S_IRWXG|S_IRWXO)
     96   1.1    cgd 		mode = sb.st_mode & RWX;
     97   1.1    cgd 		++argv;
     98   1.1    cgd 		break;
     99   1.1    cgd 	case 1:
    100   1.1    cgd #define	RW	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
    101   1.1    cgd 		mode = RW & ~umask(RW);
    102   1.1    cgd 		break;
    103   1.1    cgd 	case 0:
    104   1.1    cgd 	default:
    105   1.1    cgd 		usage();
    106   1.1    cgd 	}
    107   1.1    cgd 
    108  1.12   elad 	if (base64) {
    109  1.12   elad 		(void)printf("begin-base64 %o %s\n", mode, *argv);
    110  1.12   elad 		base64_encode();
    111  1.12   elad 		(void)printf("====\n");
    112  1.12   elad 	} else {
    113  1.12   elad 		(void)printf("begin %o %s\n", mode, *argv);
    114  1.12   elad 		encode();
    115  1.12   elad 		(void)printf("end\n");
    116  1.12   elad 	}
    117  1.12   elad 
    118   1.8  lukem 	if (ferror(stdout))
    119   1.8  lukem 		err(1, "write error");
    120   1.1    cgd 	exit(0);
    121   1.1    cgd }
    122   1.1    cgd 
    123   1.1    cgd /* ENC is the basic 1 character encoding function to make a char printing */
    124   1.1    cgd #define	ENC(c) ((c) ? ((c) & 077) + ' ': '`')
    125   1.1    cgd 
    126   1.1    cgd /*
    127  1.12   elad  * copy from in to out, encoding in base64 as you go along.
    128  1.12   elad  */
    129  1.12   elad static void
    130  1.12   elad base64_encode(void)
    131  1.12   elad {
    132  1.12   elad 	/*
    133  1.12   elad 	 * Output must fit into 80 columns, chunks come in 4, leave 1.
    134  1.12   elad 	 */
    135  1.12   elad #define GROUPS 	((70 / 4) - 1)
    136  1.12   elad 	unsigned char buf[3];
    137  1.12   elad 	char buf2[sizeof(buf) * 2 + 1];
    138  1.12   elad 	size_t n;
    139  1.12   elad 	int rv, sequence;
    140  1.12   elad 
    141  1.12   elad 	sequence = 0;
    142  1.12   elad 
    143  1.12   elad 	while ((n = fread(buf, 1, sizeof(buf), stdin))) {
    144  1.12   elad 		++sequence;
    145  1.12   elad 		rv = b64_ntop(buf, n, buf2, (sizeof(buf2) / sizeof(buf2[0])));
    146  1.12   elad 		if (rv == -1)
    147  1.12   elad 			errx(1, "b64_ntop: error encoding base64");
    148  1.12   elad 		printf("%s%s", buf2, (sequence % GROUPS) ? "" : "\n");
    149  1.12   elad 	}
    150  1.12   elad 	if (sequence % GROUPS)
    151  1.12   elad 		printf("\n");
    152  1.12   elad }
    153  1.12   elad 
    154  1.12   elad /*
    155   1.1    cgd  * copy from in to out, encoding as you go along.
    156   1.1    cgd  */
    157   1.3    jtc static void
    158  1.10    wiz encode(void)
    159   1.1    cgd {
    160   1.8  lukem 	int ch, n;
    161   1.8  lukem 	char *p;
    162   1.1    cgd 	char buf[80];
    163   1.1    cgd 
    164   1.8  lukem 	while ((n = fread(buf, 1, 45, stdin)) > 0) {
    165   1.1    cgd 		ch = ENC(n);
    166   1.1    cgd 		if (putchar(ch) == EOF)
    167   1.1    cgd 			break;
    168   1.1    cgd 		for (p = buf; n > 0; n -= 3, p += 3) {
    169   1.1    cgd 			ch = *p >> 2;
    170   1.1    cgd 			ch = ENC(ch);
    171   1.1    cgd 			if (putchar(ch) == EOF)
    172   1.1    cgd 				break;
    173   1.8  lukem 			ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
    174   1.1    cgd 			ch = ENC(ch);
    175   1.1    cgd 			if (putchar(ch) == EOF)
    176   1.1    cgd 				break;
    177   1.8  lukem 			ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
    178   1.1    cgd 			ch = ENC(ch);
    179   1.1    cgd 			if (putchar(ch) == EOF)
    180   1.1    cgd 				break;
    181   1.1    cgd 			ch = p[2] & 077;
    182   1.1    cgd 			ch = ENC(ch);
    183   1.1    cgd 			if (putchar(ch) == EOF)
    184   1.1    cgd 				break;
    185   1.1    cgd 		}
    186   1.1    cgd 		if (putchar('\n') == EOF)
    187   1.1    cgd 			break;
    188   1.1    cgd 	}
    189   1.8  lukem 	if (ferror(stdin))
    190   1.8  lukem 		err(1, "read error.");
    191   1.1    cgd 	ch = ENC('\0');
    192   1.1    cgd 	(void)putchar(ch);
    193   1.1    cgd 	(void)putchar('\n');
    194   1.1    cgd }
    195   1.1    cgd 
    196   1.3    jtc static void
    197  1.10    wiz usage(void)
    198   1.1    cgd {
    199  1.12   elad 	(void)fprintf(stderr, "usage: %s [-m] [infile] remotefile\n",
    200  1.10    wiz 		      getprogname());
    201   1.1    cgd 	exit(1);
    202   1.1    cgd }
    203