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