uuencode.c revision 28564
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1983, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3528564Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1983, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4128564Scharnier#if 0
421590Srgrimesstatic char sccsid[] = "@(#)uuencode.c	8.2 (Berkeley) 4/2/94";
4328564Scharnier#endif
4428564Scharnierstatic const char rcsid[] =
4528564Scharnier	"$Id$";
461590Srgrimes#endif /* not lint */
471590Srgrimes
481590Srgrimes/*
491590Srgrimes * uuencode [input] output
501590Srgrimes *
511590Srgrimes * Encode a file so it can be mailed to a remote system.
521590Srgrimes */
531590Srgrimes#include <sys/types.h>
541590Srgrimes#include <sys/stat.h>
551590Srgrimes
5628564Scharnier#include <err.h>
571590Srgrimes#include <stdio.h>
5828564Scharnier#include <unistd.h>
591590Srgrimes
6028564Scharniervoid encode __P((void));
6128564Scharnierstatic void usage __P((void));
6228564Scharnier
631590Srgrimesint
641590Srgrimesmain(argc, argv)
651590Srgrimes	int argc;
661590Srgrimes	char *argv[];
671590Srgrimes{
681590Srgrimes	struct stat sb;
691590Srgrimes	int mode;
701590Srgrimes
7124360Simp	while (getopt(argc, argv, "") != -1)
721590Srgrimes		usage();
731590Srgrimes	argv += optind;
741590Srgrimes	argc -= optind;
751590Srgrimes
761590Srgrimes	switch(argc) {
771590Srgrimes	case 2:			/* optional first argument is input file */
7828564Scharnier		if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb))
7928564Scharnier			err(1, "%s", *argv);
801590Srgrimes#define	RWX	(S_IRWXU|S_IRWXG|S_IRWXO)
811590Srgrimes		mode = sb.st_mode & RWX;
821590Srgrimes		++argv;
831590Srgrimes		break;
841590Srgrimes	case 1:
851590Srgrimes#define	RW	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
861590Srgrimes		mode = RW & ~umask(RW);
871590Srgrimes		break;
881590Srgrimes	case 0:
891590Srgrimes	default:
901590Srgrimes		usage();
911590Srgrimes	}
921590Srgrimes
931590Srgrimes	(void)printf("begin %o %s\n", mode, *argv);
941590Srgrimes	encode();
951590Srgrimes	(void)printf("end\n");
9628564Scharnier	if (ferror(stdout))
9728564Scharnier		errx(1, "write error");
981590Srgrimes	exit(0);
991590Srgrimes}
1001590Srgrimes
1011590Srgrimes/* ENC is the basic 1 character encoding function to make a char printing */
1021590Srgrimes#define	ENC(c) ((c) ? ((c) & 077) + ' ': '`')
1031590Srgrimes
1041590Srgrimes/*
1051590Srgrimes * copy from in to out, encoding as you go along.
1061590Srgrimes */
10728564Scharniervoid
1081590Srgrimesencode()
1091590Srgrimes{
1101590Srgrimes	register int ch, n;
1111590Srgrimes	register char *p;
1121590Srgrimes	char buf[80];
1131590Srgrimes
11428564Scharnier	while ((n = fread(buf, 1, 45, stdin))) {
1151590Srgrimes		ch = ENC(n);
1161590Srgrimes		if (putchar(ch) == EOF)
1171590Srgrimes			break;
1181590Srgrimes		for (p = buf; n > 0; n -= 3, p += 3) {
1191590Srgrimes			ch = *p >> 2;
1201590Srgrimes			ch = ENC(ch);
1211590Srgrimes			if (putchar(ch) == EOF)
1221590Srgrimes				break;
1231590Srgrimes			ch = (*p << 4) & 060 | (p[1] >> 4) & 017;
1241590Srgrimes			ch = ENC(ch);
1251590Srgrimes			if (putchar(ch) == EOF)
1261590Srgrimes				break;
1271590Srgrimes			ch = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
1281590Srgrimes			ch = ENC(ch);
1291590Srgrimes			if (putchar(ch) == EOF)
1301590Srgrimes				break;
1311590Srgrimes			ch = p[2] & 077;
1321590Srgrimes			ch = ENC(ch);
1331590Srgrimes			if (putchar(ch) == EOF)
1341590Srgrimes				break;
1351590Srgrimes		}
1361590Srgrimes		if (putchar('\n') == EOF)
1371590Srgrimes			break;
1381590Srgrimes	}
13928564Scharnier	if (ferror(stdin))
14028564Scharnier		errx(1, "read error");
1411590Srgrimes	ch = ENC('\0');
1421590Srgrimes	(void)putchar(ch);
1431590Srgrimes	(void)putchar('\n');
1441590Srgrimes}
1451590Srgrimes
14628564Scharnierstatic void
1471590Srgrimesusage()
1481590Srgrimes{
1491590Srgrimes	(void)fprintf(stderr,"usage: uuencode [infile] remotefile\n");
1501590Srgrimes	exit(1);
1511590Srgrimes}
152