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