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.6 (Berkeley) 07/06/88"; 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 <stdio.h> 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 31 /* ENC is the basic 1 character encoding function to make a char printing */ 32 #define ENC(c) ((c) ? ((c) & 077) + ' ': '`') 33 34 main(argc, argv) 35 char **argv; 36 { 37 FILE *in; 38 struct stat sbuf; 39 int mode; 40 41 /* optional 1st argument */ 42 if (argc > 2) { 43 if ((in = fopen(argv[1], "r")) == NULL) { 44 perror(argv[1]); 45 exit(1); 46 } 47 argv++; argc--; 48 } else 49 in = stdin; 50 51 if (argc != 2) { 52 fprintf(stderr,"Usage: uuencode [infile] remotefile\n"); 53 exit(2); 54 } 55 56 /* figure out the input file mode */ 57 if (fstat(fileno(in), &sbuf) < 0 || !isatty(fileno(in))) 58 mode = 0666 & ~umask(0666); 59 else 60 mode = sbuf.st_mode & 0777; 61 printf("begin %o %s\n", mode, argv[1]); 62 63 encode(in, stdout); 64 65 printf("end\n"); 66 exit(0); 67 } 68 69 /* 70 * copy from in to out, encoding as you go along. 71 */ 72 encode(in, out) 73 register FILE *in; 74 register FILE *out; 75 { 76 char buf[80]; 77 register int i, n; 78 79 for (;;) { 80 /* 1 (up to) 45 character line */ 81 n = fread(buf, 1, 45, in); 82 putc(ENC(n), out); 83 84 for (i=0; i<n; i += 3) 85 outdec(&buf[i], out); 86 87 putc('\n', out); 88 if (n <= 0) 89 break; 90 } 91 } 92 93 /* 94 * output one group of 3 bytes, pointed at by p, on file f. 95 */ 96 outdec(p, f) 97 register char *p; 98 register FILE *f; 99 { 100 register int c1, c2, c3, c4; 101 102 c1 = *p >> 2; 103 c2 = (*p << 4) & 060 | (p[1] >> 4) & 017; 104 c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03; 105 c4 = p[2] & 077; 106 putc(ENC(c1), f); 107 putc(ENC(c2), f); 108 putc(ENC(c3), f); 109 putc(ENC(c4), f); 110 } 111