xref: /netbsd-src/usr.bin/uuencode/uuencode.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: uuencode.c,v 1.9 2003/08/07 11:16:59 agc 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.9 2003/08/07 11:16:59 agc 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 __P((int, char **));
62 static void encode __P((void));
63 static void usage __P((void));
64 
65 int
66 main(argc, argv)
67 	int argc;
68 	char *argv[];
69 {
70 	struct stat sb;
71 	int mode;
72 
73 	mode = 0;
74 	setlocale(LC_ALL, "");
75 
76 	while (getopt(argc, argv, "") != -1)
77 		usage();
78 	argv += optind;
79 	argc -= optind;
80 
81 	switch(argc) {
82 	case 2:			/* optional first argument is input file */
83 		if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb))
84 			err(1, "%s", *argv);
85 #define	RWX	(S_IRWXU|S_IRWXG|S_IRWXO)
86 		mode = sb.st_mode & RWX;
87 		++argv;
88 		break;
89 	case 1:
90 #define	RW	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
91 		mode = RW & ~umask(RW);
92 		break;
93 	case 0:
94 	default:
95 		usage();
96 	}
97 
98 	(void)printf("begin %o %s\n", mode, *argv);
99 	encode();
100 	(void)printf("end\n");
101 	if (ferror(stdout))
102 		err(1, "write error");
103 	exit(0);
104 }
105 
106 /* ENC is the basic 1 character encoding function to make a char printing */
107 #define	ENC(c) ((c) ? ((c) & 077) + ' ': '`')
108 
109 /*
110  * copy from in to out, encoding as you go along.
111  */
112 static void
113 encode()
114 {
115 	int ch, n;
116 	char *p;
117 	char buf[80];
118 
119 	while ((n = fread(buf, 1, 45, stdin)) > 0) {
120 		ch = ENC(n);
121 		if (putchar(ch) == EOF)
122 			break;
123 		for (p = buf; n > 0; n -= 3, p += 3) {
124 			ch = *p >> 2;
125 			ch = ENC(ch);
126 			if (putchar(ch) == EOF)
127 				break;
128 			ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
129 			ch = ENC(ch);
130 			if (putchar(ch) == EOF)
131 				break;
132 			ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
133 			ch = ENC(ch);
134 			if (putchar(ch) == EOF)
135 				break;
136 			ch = p[2] & 077;
137 			ch = ENC(ch);
138 			if (putchar(ch) == EOF)
139 				break;
140 		}
141 		if (putchar('\n') == EOF)
142 			break;
143 	}
144 	if (ferror(stdin))
145 		err(1, "read error.");
146 	ch = ENC('\0');
147 	(void)putchar(ch);
148 	(void)putchar('\n');
149 }
150 
151 static void
152 usage()
153 {
154 	(void)fprintf(stderr,"usage: uuencode [infile] remotefile\n");
155 	exit(1);
156 }
157