uuencode.c revision 1.6 1 /* $NetBSD: uuencode.c,v 1.6 1994/11/17 07:39:43 jtc 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 char copyright[] =
37 "@(#) Copyright (c) 1983, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)uuencode.c 8.2 (Berkeley) 4/2/94";
44 #endif
45 static char rcsid[] = "$NetBSD: uuencode.c,v 1.6 1994/11/17 07:39:43 jtc Exp $";
46 #endif /* not lint */
47
48 /*
49 * uuencode [input] output
50 *
51 * Encode a file so it can be mailed to a remote system.
52 */
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <locale.h>
57 #include <errno.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <unistd.h>
61
62 static void encode();
63 static __dead void usage();
64
65 int
66 main(argc, argv)
67 int argc;
68 char *argv[];
69 {
70 struct stat sb;
71 int mode;
72
73 setlocale(LC_ALL, "");
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 (void)fprintf(stderr, "uuencode: %s: %s.\n",
84 *argv, strerror(errno));
85 exit(1);
86 }
87 #define RWX (S_IRWXU|S_IRWXG|S_IRWXO)
88 mode = sb.st_mode & RWX;
89 ++argv;
90 break;
91 case 1:
92 #define RW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
93 mode = RW & ~umask(RW);
94 break;
95 case 0:
96 default:
97 usage();
98 }
99
100 (void)printf("begin %o %s\n", mode, *argv);
101 encode();
102 (void)printf("end\n");
103 if (ferror(stdout)) {
104 (void)fprintf(stderr, "uuencode: write error.\n");
105 exit(1);
106 }
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 register int ch, n;
120 register char *p;
121 char buf[80];
122
123 while (n = fread(buf, 1, 45, stdin)) {
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 (void)fprintf(stderr, "uuencode: read error.\n");
150 exit(1);
151 }
152 ch = ENC('\0');
153 (void)putchar(ch);
154 (void)putchar('\n');
155 }
156
157 static void
158 usage()
159 {
160 (void)fprintf(stderr,"usage: uuencode [infile] remotefile\n");
161 exit(1);
162 }
163