Annotation of src/usr.bin/uuencode/uuencode.c, Revision 1.4
1.4 ! mpech 1: /* $OpenBSD: uuencode.c,v 1.3 2001/07/12 05:17:28 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: uuencode.c,v 1.7 1994/11/17 07:41:15 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: char copyright[] =
38: "@(#) Copyright (c) 1983, 1993\n\
39: The Regents of the University of California. All rights reserved.\n";
40:
41: #ifndef lint
42: #if 0
43: static char sccsid[] = "@(#)uuencode.c 8.2 (Berkeley) 4/2/94";
44: #endif
1.4 ! mpech 45: static char rcsid[] = "$OpenBSD: uuencode.c,v 1.3 2001/07/12 05:17:28 deraadt Exp $";
1.1 deraadt 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: {
1.4 ! mpech 119: int ch, n;
! 120: char *p;
1.1 deraadt 121: char buf[80];
122:
1.3 deraadt 123: while ((n = fread(buf, 1, 45, stdin))) {
1.1 deraadt 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: }