xref: /freebsd-13.1/usr.bin/uuencode/uuencode.c (revision 9b50d902)
1*9b50d902SRodney W. Grimes /*-
2*9b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
3*9b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4*9b50d902SRodney W. Grimes  *
5*9b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6*9b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
7*9b50d902SRodney W. Grimes  * are met:
8*9b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9*9b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10*9b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11*9b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12*9b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13*9b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14*9b50d902SRodney W. Grimes  *    must display the following acknowledgement:
15*9b50d902SRodney W. Grimes  *	This product includes software developed by the University of
16*9b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
17*9b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18*9b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19*9b50d902SRodney W. Grimes  *    without specific prior written permission.
20*9b50d902SRodney W. Grimes  *
21*9b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*9b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*9b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*9b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*9b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*9b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*9b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*9b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*9b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*9b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*9b50d902SRodney W. Grimes  * SUCH DAMAGE.
32*9b50d902SRodney W. Grimes  */
33*9b50d902SRodney W. Grimes 
34*9b50d902SRodney W. Grimes #ifndef lint
35*9b50d902SRodney W. Grimes char copyright[] =
36*9b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\
37*9b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
38*9b50d902SRodney W. Grimes #endif /* not lint */
39*9b50d902SRodney W. Grimes 
40*9b50d902SRodney W. Grimes #ifndef lint
41*9b50d902SRodney W. Grimes static char sccsid[] = "@(#)uuencode.c	8.2 (Berkeley) 4/2/94";
42*9b50d902SRodney W. Grimes #endif /* not lint */
43*9b50d902SRodney W. Grimes 
44*9b50d902SRodney W. Grimes /*
45*9b50d902SRodney W. Grimes  * uuencode [input] output
46*9b50d902SRodney W. Grimes  *
47*9b50d902SRodney W. Grimes  * Encode a file so it can be mailed to a remote system.
48*9b50d902SRodney W. Grimes  */
49*9b50d902SRodney W. Grimes #include <sys/types.h>
50*9b50d902SRodney W. Grimes #include <sys/stat.h>
51*9b50d902SRodney W. Grimes 
52*9b50d902SRodney W. Grimes #include <stdio.h>
53*9b50d902SRodney W. Grimes 
54*9b50d902SRodney W. Grimes int
55*9b50d902SRodney W. Grimes main(argc, argv)
56*9b50d902SRodney W. Grimes 	int argc;
57*9b50d902SRodney W. Grimes 	char *argv[];
58*9b50d902SRodney W. Grimes {
59*9b50d902SRodney W. Grimes 	extern int optind;
60*9b50d902SRodney W. Grimes 	extern int errno;
61*9b50d902SRodney W. Grimes 	struct stat sb;
62*9b50d902SRodney W. Grimes 	int mode;
63*9b50d902SRodney W. Grimes 	char *strerror();
64*9b50d902SRodney W. Grimes 
65*9b50d902SRodney W. Grimes 	while (getopt(argc, argv, "") != EOF)
66*9b50d902SRodney W. Grimes 		usage();
67*9b50d902SRodney W. Grimes 	argv += optind;
68*9b50d902SRodney W. Grimes 	argc -= optind;
69*9b50d902SRodney W. Grimes 
70*9b50d902SRodney W. Grimes 	switch(argc) {
71*9b50d902SRodney W. Grimes 	case 2:			/* optional first argument is input file */
72*9b50d902SRodney W. Grimes 		if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) {
73*9b50d902SRodney W. Grimes 			(void)fprintf(stderr, "uuencode: %s: %s.\n",
74*9b50d902SRodney W. Grimes 			    *argv, strerror(errno));
75*9b50d902SRodney W. Grimes 			exit(1);
76*9b50d902SRodney W. Grimes 		}
77*9b50d902SRodney W. Grimes #define	RWX	(S_IRWXU|S_IRWXG|S_IRWXO)
78*9b50d902SRodney W. Grimes 		mode = sb.st_mode & RWX;
79*9b50d902SRodney W. Grimes 		++argv;
80*9b50d902SRodney W. Grimes 		break;
81*9b50d902SRodney W. Grimes 	case 1:
82*9b50d902SRodney W. Grimes #define	RW	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
83*9b50d902SRodney W. Grimes 		mode = RW & ~umask(RW);
84*9b50d902SRodney W. Grimes 		break;
85*9b50d902SRodney W. Grimes 	case 0:
86*9b50d902SRodney W. Grimes 	default:
87*9b50d902SRodney W. Grimes 		usage();
88*9b50d902SRodney W. Grimes 	}
89*9b50d902SRodney W. Grimes 
90*9b50d902SRodney W. Grimes 	(void)printf("begin %o %s\n", mode, *argv);
91*9b50d902SRodney W. Grimes 	encode();
92*9b50d902SRodney W. Grimes 	(void)printf("end\n");
93*9b50d902SRodney W. Grimes 	if (ferror(stdout)) {
94*9b50d902SRodney W. Grimes 		(void)fprintf(stderr, "uuencode: write error.\n");
95*9b50d902SRodney W. Grimes 		exit(1);
96*9b50d902SRodney W. Grimes 	}
97*9b50d902SRodney W. Grimes 	exit(0);
98*9b50d902SRodney W. Grimes }
99*9b50d902SRodney W. Grimes 
100*9b50d902SRodney W. Grimes /* ENC is the basic 1 character encoding function to make a char printing */
101*9b50d902SRodney W. Grimes #define	ENC(c) ((c) ? ((c) & 077) + ' ': '`')
102*9b50d902SRodney W. Grimes 
103*9b50d902SRodney W. Grimes /*
104*9b50d902SRodney W. Grimes  * copy from in to out, encoding as you go along.
105*9b50d902SRodney W. Grimes  */
106*9b50d902SRodney W. Grimes encode()
107*9b50d902SRodney W. Grimes {
108*9b50d902SRodney W. Grimes 	register int ch, n;
109*9b50d902SRodney W. Grimes 	register char *p;
110*9b50d902SRodney W. Grimes 	char buf[80];
111*9b50d902SRodney W. Grimes 
112*9b50d902SRodney W. Grimes 	while (n = fread(buf, 1, 45, stdin)) {
113*9b50d902SRodney W. Grimes 		ch = ENC(n);
114*9b50d902SRodney W. Grimes 		if (putchar(ch) == EOF)
115*9b50d902SRodney W. Grimes 			break;
116*9b50d902SRodney W. Grimes 		for (p = buf; n > 0; n -= 3, p += 3) {
117*9b50d902SRodney W. Grimes 			ch = *p >> 2;
118*9b50d902SRodney W. Grimes 			ch = ENC(ch);
119*9b50d902SRodney W. Grimes 			if (putchar(ch) == EOF)
120*9b50d902SRodney W. Grimes 				break;
121*9b50d902SRodney W. Grimes 			ch = (*p << 4) & 060 | (p[1] >> 4) & 017;
122*9b50d902SRodney W. Grimes 			ch = ENC(ch);
123*9b50d902SRodney W. Grimes 			if (putchar(ch) == EOF)
124*9b50d902SRodney W. Grimes 				break;
125*9b50d902SRodney W. Grimes 			ch = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
126*9b50d902SRodney W. Grimes 			ch = ENC(ch);
127*9b50d902SRodney W. Grimes 			if (putchar(ch) == EOF)
128*9b50d902SRodney W. Grimes 				break;
129*9b50d902SRodney W. Grimes 			ch = p[2] & 077;
130*9b50d902SRodney W. Grimes 			ch = ENC(ch);
131*9b50d902SRodney W. Grimes 			if (putchar(ch) == EOF)
132*9b50d902SRodney W. Grimes 				break;
133*9b50d902SRodney W. Grimes 		}
134*9b50d902SRodney W. Grimes 		if (putchar('\n') == EOF)
135*9b50d902SRodney W. Grimes 			break;
136*9b50d902SRodney W. Grimes 	}
137*9b50d902SRodney W. Grimes 	if (ferror(stdin)) {
138*9b50d902SRodney W. Grimes 		(void)fprintf(stderr, "uuencode: read error.\n");
139*9b50d902SRodney W. Grimes 		exit(1);
140*9b50d902SRodney W. Grimes 	}
141*9b50d902SRodney W. Grimes 	ch = ENC('\0');
142*9b50d902SRodney W. Grimes 	(void)putchar(ch);
143*9b50d902SRodney W. Grimes 	(void)putchar('\n');
144*9b50d902SRodney W. Grimes }
145*9b50d902SRodney W. Grimes 
146*9b50d902SRodney W. Grimes usage()
147*9b50d902SRodney W. Grimes {
148*9b50d902SRodney W. Grimes 	(void)fprintf(stderr,"usage: uuencode [infile] remotefile\n");
149*9b50d902SRodney W. Grimes 	exit(1);
150*9b50d902SRodney W. Grimes }
151