uuencode.c revision 96386
1/*- 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. 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 const char copyright[] = 36"@(#) Copyright (c) 1983, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)uuencode.c 8.2 (Berkeley) 4/2/94"; 43#endif 44static const char rcsid[] = 45 "$FreeBSD: head/usr.bin/uuencode/uuencode.c 96386 2002-05-11 03:29:04Z alfred $"; 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 <sys/param.h> 54#include <sys/socket.h> 55#include <sys/stat.h> 56 57#include <netinet/in.h> 58 59#include <err.h> 60#include <resolv.h> 61#include <stdio.h> 62#include <stdlib.h> 63#include <string.h> 64#include <unistd.h> 65 66void encode(void); 67void base64_encode(void); 68static void usage(void); 69 70FILE *output; 71int mode; 72char **av; 73 74int 75main(int argc, char *argv[]) 76{ 77 struct stat sb; 78 int base64; 79 char ch; 80 char *outfile; 81 82 base64 = 0; 83 outfile = NULL; 84 85 while ((ch = getopt(argc, argv, "mo:")) != -1) { 86 switch (ch) { 87 case 'm': 88 base64 = 1; 89 break; 90 case 'o': 91 outfile = optarg; 92 break; 93 case '?': 94 default: 95 usage(); 96 } 97 } 98 argv += optind; 99 argc -= optind; 100 101 switch(argc) { 102 case 2: /* optional first argument is input file */ 103 if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) 104 err(1, "%s", *argv); 105#define RWX (S_IRWXU|S_IRWXG|S_IRWXO) 106 mode = sb.st_mode & RWX; 107 ++argv; 108 break; 109 case 1: 110#define RW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) 111 mode = RW & ~umask(RW); 112 break; 113 case 0: 114 default: 115 usage(); 116 } 117 118 av = argv; 119 120 if (outfile != NULL) { 121 output = fopen(outfile, "w+"); 122 if (output == NULL) 123 err(1, "unable to open %s for output", outfile); 124 } else 125 output = stdout; 126 if (base64) 127 base64_encode(); 128 else 129 encode(); 130 if (ferror(output)) 131 errx(1, "write error"); 132 exit(0); 133} 134 135/* ENC is the basic 1 character encoding function to make a char printing */ 136#define ENC(c) ((c) ? ((c) & 077) + ' ': '`') 137 138/* 139 * Copy from in to out, encoding in base64 as you go along. 140 */ 141void 142base64_encode() 143{ 144#define GROUPS 8 /* Group output chunks */ 145 unsigned char buf[6]; 146 char buf2[16]; 147 size_t n; 148 int rv, sequence; 149 150 sequence = 0; 151 152 fprintf(output, "begin-base64 %o %s\n", mode, *av); 153 while ((n = fread(buf, 1, sizeof(buf), stdin))) { 154 ++sequence; 155 rv = b64_ntop(buf, n, buf2, (sizeof(buf2) / sizeof(buf2[0]))); 156 if (rv == -1) 157 errx(1, "b64_ntop: error encoding base64"); 158 fprintf(output, "%s%s", buf2, (sequence % GROUPS) ? "" : "\n"); 159 } 160 if (sequence % GROUPS) 161 fprintf(output, "\n"); 162 fprintf(output, "====\n"); 163} 164 165/* 166 * Copy from in to out, encoding as you go along. 167 */ 168void 169encode() 170{ 171 register int ch, n; 172 register char *p; 173 char buf[80]; 174 175 (void)fprintf(output, "begin %o %s\n", mode, *av); 176 while ((n = fread(buf, 1, 45, stdin))) { 177 ch = ENC(n); 178 if (fputc(ch, output) == EOF) 179 break; 180 for (p = buf; n > 0; n -= 3, p += 3) { 181 /* Pad with nulls if not a multiple of 3. */ 182 if (n < 3) { 183 p[2] = '\0'; 184 if (n < 2) 185 p[1] = '\0'; 186 } 187 ch = *p >> 2; 188 ch = ENC(ch); 189 if (fputc(ch, output) == EOF) 190 break; 191 ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017); 192 ch = ENC(ch); 193 if (fputc(ch, output) == EOF) 194 break; 195 ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03); 196 ch = ENC(ch); 197 if (fputc(ch, output) == EOF) 198 break; 199 ch = p[2] & 077; 200 ch = ENC(ch); 201 if (fputc(ch, output) == EOF) 202 break; 203 } 204 if (fputc('\n', output) == EOF) 205 break; 206 } 207 if (ferror(stdin)) 208 errx(1, "read error"); 209 (void)fprintf(output, "%c\nend\n", ENC('\0')); 210} 211 212static void 213usage() 214{ 215 (void)fprintf(stderr,"usage: uuencode [-m] [-o outfile] [infile] remotefile\n"); 216 exit(1); 217} 218