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