386BSD 0.1 development
[unix-history] / usr / src / usr.bin / uuencode / uuencode.c
CommitLineData
0b89895b
WJ
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
35static char sccsid[] = "@(#)uuencode.c 5.9 (Berkeley) 6/1/90";
36#endif /* not lint */
37
38/*
39 * uuencode [input] output
40 *
41 * Encode a file so it can be mailed to a remote system.
42 */
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <stdio.h>
46
47main(argc, argv)
48 int argc;
49 char **argv;
50{
51 extern int optind;
52 extern int errno;
53 struct stat sb;
54 int mode;
55 char *strerror();
56
57 while (getopt(argc, argv, "") != EOF)
58 usage();
59 argv += optind;
60 argc -= optind;
61
62 switch(argc) {
63 case 2: /* optional first argument is input file */
64 if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) {
65 (void)fprintf(stderr, "uuencode: %s: %s.\n",
66 *argv, strerror(errno));
67 exit(1);
68 }
69#define RWX (S_IRWXU|S_IRWXG|S_IRWXO)
70 mode = sb.st_mode & RWX;
71 ++argv;
72 break;
73 case 1:
74#define RW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
75 mode = RW & ~umask(RW);
76 break;
77 case 0:
78 default:
79 usage();
80 }
81
82 (void)printf("begin %o %s\n", mode, *argv);
83 encode();
84 (void)printf("end\n");
85 if (ferror(stdout)) {
86 (void)fprintf(stderr, "uuencode: write error.\n");
87 exit(1);
88 }
89 exit(0);
90}
91
92/* ENC is the basic 1 character encoding function to make a char printing */
93#define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
94
95/*
96 * copy from in to out, encoding as you go along.
97 */
98encode()
99{
100 register int ch, n;
101 register char *p;
102 char buf[80];
103
104 while (n = fread(buf, 1, 45, stdin)) {
105 ch = ENC(n);
106 if (putchar(ch) == EOF)
107 break;
108 for (p = buf; n > 0; n -= 3, p += 3) {
109 ch = *p >> 2;
110 ch = ENC(ch);
111 if (putchar(ch) == EOF)
112 break;
113 ch = (*p << 4) & 060 | (p[1] >> 4) & 017;
114 ch = ENC(ch);
115 if (putchar(ch) == EOF)
116 break;
117 ch = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
118 ch = ENC(ch);
119 if (putchar(ch) == EOF)
120 break;
121 ch = p[2] & 077;
122 ch = ENC(ch);
123 if (putchar(ch) == EOF)
124 break;
125 }
126 if (putchar('\n') == EOF)
127 break;
128 }
129 if (ferror(stdin)) {
130 (void)fprintf(stderr, "uuencode: read error.\n");
131 exit(1);
132 }
133 ch = ENC('\0');
134 (void)putchar(ch);
135 (void)putchar('\n');
136}
137
138usage()
139{
140 (void)fprintf(stderr,"usage: uuencode [infile] remotefile\n");
141 exit(1);
142}