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