uuencode.c revision 1.4
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";*/ 36static char rcsid[] = "$Id: uuencode.c,v 1.4 1993/10/13 18:34:49 jtc 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 <stdio.h> 45#include <stdlib.h> 46#include <string.h> 47#include <errno.h> 48#include <sys/types.h> 49#include <sys/stat.h> 50#include <unistd.h> 51 52static void encode(); 53static void usage(); 54 55int 56main(argc, argv) 57 int argc; 58 char **argv; 59{ 60 struct stat sb; 61 int mode; 62 63 while (getopt(argc, argv, "") != EOF) 64 usage(); 65 argv += optind; 66 argc -= optind; 67 68 switch(argc) { 69 case 2: /* optional first argument is input file */ 70 if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) { 71 (void)fprintf(stderr, "uuencode: %s: %s.\n", 72 *argv, strerror(errno)); 73 exit(1); 74 } 75#define RWX (S_IRWXU|S_IRWXG|S_IRWXO) 76 mode = sb.st_mode & RWX; 77 ++argv; 78 break; 79 case 1: 80#define RW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) 81 mode = RW & ~umask(RW); 82 break; 83 case 0: 84 default: 85 usage(); 86 } 87 88 (void)printf("begin %o %s\n", mode, *argv); 89 encode(); 90 (void)printf("end\n"); 91 if (ferror(stdout)) { 92 (void)fprintf(stderr, "uuencode: write error.\n"); 93 exit(1); 94 } 95 exit(0); 96} 97 98/* ENC is the basic 1 character encoding function to make a char printing */ 99#define ENC(c) ((c) ? ((c) & 077) + ' ': '`') 100 101/* 102 * copy from in to out, encoding as you go along. 103 */ 104static void 105encode() 106{ 107 register int ch, n; 108 register char *p; 109 char buf[80]; 110 111 while (n = fread(buf, 1, 45, stdin)) { 112 ch = ENC(n); 113 if (putchar(ch) == EOF) 114 break; 115 for (p = buf; n > 0; n -= 3, p += 3) { 116 ch = *p >> 2; 117 ch = ENC(ch); 118 if (putchar(ch) == EOF) 119 break; 120 ch = (*p << 4) & 060 | (p[1] >> 4) & 017; 121 ch = ENC(ch); 122 if (putchar(ch) == EOF) 123 break; 124 ch = (p[1] << 2) & 074 | (p[2] >> 6) & 03; 125 ch = ENC(ch); 126 if (putchar(ch) == EOF) 127 break; 128 ch = p[2] & 077; 129 ch = ENC(ch); 130 if (putchar(ch) == EOF) 131 break; 132 } 133 if (putchar('\n') == EOF) 134 break; 135 } 136 if (ferror(stdin)) { 137 (void)fprintf(stderr, "uuencode: read error.\n"); 138 exit(1); 139 } 140 ch = ENC('\0'); 141 (void)putchar(ch); 142 (void)putchar('\n'); 143} 144 145static void 146usage() 147{ 148 (void)fprintf(stderr,"usage: uuencode [infile] remotefile\n"); 149 exit(1); 150} 151