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 are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 static char sccsid[] = "@(#)uuencode.c 5.8 (Berkeley) 05/18/90"; 20 #endif /* not lint */ 21 22 /* 23 * uuencode [input] output 24 * 25 * Encode a file so it can be mailed to a remote system. 26 */ 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 #include <stdio.h> 30 31 main(argc, argv) 32 int argc; 33 char **argv; 34 { 35 extern int optind; 36 extern int errno; 37 struct stat sb; 38 int mode; 39 char *strerror(); 40 41 while (getopt(argc, argv, "") != EOF) 42 usage(); 43 argv += optind; 44 argc -= optind; 45 46 switch(argc) { 47 case 2: /* optional first argument is input file */ 48 if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) { 49 (void)fprintf(stderr, "uuencode: %s: %s.\n", 50 *argv, strerror(errno)); 51 exit(1); 52 } 53 #define RWX (S_IRWXU|S_IRWXG|S_IRWXO) 54 mode = sb.st_mode & RWX; 55 ++argv; 56 break; 57 case 1: 58 #define RW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) 59 mode = RW & ~umask(RW); 60 break; 61 case 0: 62 default: 63 usage(); 64 } 65 66 (void)printf("begin %o %s\n", mode, *argv); 67 encode(); 68 (void)printf("end\n"); 69 if (ferror(stdout)) { 70 (void)fprintf(stderr, "uuencode: write error.\n"); 71 exit(1); 72 } 73 exit(0); 74 } 75 76 /* ENC is the basic 1 character encoding function to make a char printing */ 77 #define ENC(c) ((c) ? ((c) & 077) + ' ': '`') 78 79 /* 80 * copy from in to out, encoding as you go along. 81 */ 82 encode() 83 { 84 register int ch, n; 85 register char *p; 86 char buf[80]; 87 88 while (n = fread(buf, 1, 45, stdin)) { 89 ch = ENC(n); 90 if (putchar(ch) == EOF) 91 break; 92 for (p = buf; n > 0; n -= 3, p += 3) { 93 ch = *p >> 2; 94 ch = ENC(ch); 95 if (putchar(ch) == EOF) 96 break; 97 ch = (*p << 4) & 060 | (p[1] >> 4) & 017; 98 ch = ENC(ch); 99 if (putchar(ch) == EOF) 100 break; 101 ch = (p[1] << 2) & 074 | (p[2] >> 6) & 03; 102 ch = ENC(ch); 103 if (putchar(ch) == EOF) 104 break; 105 ch = p[2] & 077; 106 ch = ENC(ch); 107 if (putchar(ch) == EOF) 108 break; 109 } 110 if (putchar('\n') == EOF) 111 break; 112 } 113 if (ferror(stdin)) { 114 (void)fprintf(stderr, "uuencode: read error.\n"); 115 exit(1); 116 } 117 ch = ENC('\0'); 118 (void)putchar(ch); 119 (void)putchar('\n'); 120 } 121 122 usage() 123 { 124 (void)fprintf(stderr,"usage: uuencode [infile] remotefile\n"); 125 exit(1); 126 } 127