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