uuencode.c revision 1.10 1 /* $NetBSD: uuencode.c,v 1.10 2005/06/29 20:34: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.10 2005/06/29 20:34: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
74 while (getopt(argc, argv, "") != -1)
75 usage();
76 argv += optind;
77 argc -= optind;
78
79 switch(argc) {
80 case 2: /* optional first argument is input file */
81 if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb))
82 err(1, "%s", *argv);
83 #define RWX (S_IRWXU|S_IRWXG|S_IRWXO)
84 mode = sb.st_mode & RWX;
85 ++argv;
86 break;
87 case 1:
88 #define RW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
89 mode = RW & ~umask(RW);
90 break;
91 case 0:
92 default:
93 usage();
94 }
95
96 (void)printf("begin %o %s\n", mode, *argv);
97 encode();
98 (void)printf("end\n");
99 if (ferror(stdout))
100 err(1, "write error");
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)) > 0) {
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 err(1, "read error.");
144 ch = ENC('\0');
145 (void)putchar(ch);
146 (void)putchar('\n');
147 }
148
149 static void
150 usage(void)
151 {
152 (void)fprintf(stderr, "usage: %s [infile] remotefile\n",
153 getprogname());
154 exit(1);
155 }
156