uuencode.c revision 1591
1/*-
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  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
35char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)uuencode.c	8.2 (Berkeley) 4/2/94";
42#endif /* not lint */
43
44/*
45 * uuencode [input] output
46 *
47 * Encode a file so it can be mailed to a remote system.
48 */
49#include <sys/types.h>
50#include <sys/stat.h>
51
52#include <stdio.h>
53
54int
55main(argc, argv)
56	int argc;
57	char *argv[];
58{
59	extern int optind;
60	extern int errno;
61	struct stat sb;
62	int mode;
63	char *strerror();
64
65	while (getopt(argc, argv, "") != EOF)
66		usage();
67	argv += optind;
68	argc -= optind;
69
70	switch(argc) {
71	case 2:			/* optional first argument is input file */
72		if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) {
73			(void)fprintf(stderr, "uuencode: %s: %s.\n",
74			    *argv, strerror(errno));
75			exit(1);
76		}
77#define	RWX	(S_IRWXU|S_IRWXG|S_IRWXO)
78		mode = sb.st_mode & RWX;
79		++argv;
80		break;
81	case 1:
82#define	RW	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
83		mode = RW & ~umask(RW);
84		break;
85	case 0:
86	default:
87		usage();
88	}
89
90	(void)printf("begin %o %s\n", mode, *argv);
91	encode();
92	(void)printf("end\n");
93	if (ferror(stdout)) {
94		(void)fprintf(stderr, "uuencode: write error.\n");
95		exit(1);
96	}
97	exit(0);
98}
99
100/* ENC is the basic 1 character encoding function to make a char printing */
101#define	ENC(c) ((c) ? ((c) & 077) + ' ': '`')
102
103/*
104 * copy from in to out, encoding as you go along.
105 */
106encode()
107{
108	register int ch, n;
109	register char *p;
110	char buf[80];
111
112	while (n = fread(buf, 1, 45, stdin)) {
113		ch = ENC(n);
114		if (putchar(ch) == EOF)
115			break;
116		for (p = buf; n > 0; n -= 3, p += 3) {
117			ch = *p >> 2;
118			ch = ENC(ch);
119			if (putchar(ch) == EOF)
120				break;
121			ch = (*p << 4) & 060 | (p[1] >> 4) & 017;
122			ch = ENC(ch);
123			if (putchar(ch) == EOF)
124				break;
125			ch = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
126			ch = ENC(ch);
127			if (putchar(ch) == EOF)
128				break;
129			ch = p[2] & 077;
130			ch = ENC(ch);
131			if (putchar(ch) == EOF)
132				break;
133		}
134		if (putchar('\n') == EOF)
135			break;
136	}
137	if (ferror(stdin)) {
138		(void)fprintf(stderr, "uuencode: read error.\n");
139		exit(1);
140	}
141	ch = ENC('\0');
142	(void)putchar(ch);
143	(void)putchar('\n');
144}
145
146usage()
147{
148	(void)fprintf(stderr,"usage: uuencode [infile] remotefile\n");
149	exit(1);
150}
151