Annotation of src/usr.bin/uuencode/uuencode.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: uuencode.c,v 1.7 1994/11/17 07:41:15 jtc Exp $ */
! 2:
! 3: /*-
! 4: * Copyright (c) 1983, 1993
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * Redistribution and use in source and binary forms, with or without
! 8: * modification, are permitted provided that the following conditions
! 9: * are met:
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. Redistributions in binary form must reproduce the above copyright
! 13: * notice, this list of conditions and the following disclaimer in the
! 14: * documentation and/or other materials provided with the distribution.
! 15: * 3. All advertising materials mentioning features or use of this software
! 16: * must display the following acknowledgement:
! 17: * This product includes software developed by the University of
! 18: * California, Berkeley and its contributors.
! 19: * 4. Neither the name of the University nor the names of its contributors
! 20: * may be used to endorse or promote products derived from this software
! 21: * without specific prior written permission.
! 22: *
! 23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 33: * SUCH DAMAGE.
! 34: */
! 35:
! 36: char copyright[] =
! 37: "@(#) Copyright (c) 1983, 1993\n\
! 38: The Regents of the University of California. All rights reserved.\n";
! 39:
! 40: #ifndef lint
! 41: #if 0
! 42: static char sccsid[] = "@(#)uuencode.c 8.2 (Berkeley) 4/2/94";
! 43: #endif
! 44: static char rcsid[] = "$NetBSD: uuencode.c,v 1.7 1994/11/17 07:41:15 jtc Exp $";
! 45: #endif /* not lint */
! 46:
! 47: /*
! 48: * uuencode [input] output
! 49: *
! 50: * Encode a file so it can be mailed to a remote system.
! 51: */
! 52: #include <stdio.h>
! 53: #include <stdlib.h>
! 54: #include <string.h>
! 55: #include <locale.h>
! 56: #include <errno.h>
! 57: #include <sys/types.h>
! 58: #include <sys/stat.h>
! 59: #include <unistd.h>
! 60:
! 61: static void encode();
! 62: static __dead void usage();
! 63:
! 64: int
! 65: main(argc, argv)
! 66: int argc;
! 67: char *argv[];
! 68: {
! 69: struct stat sb;
! 70: int mode;
! 71:
! 72: setlocale(LC_ALL, "");
! 73:
! 74: while (getopt(argc, argv, "") != -1)
! 75: usage();
! 76: argv += optind;
! 77: argc -= optind;
! 78:
! 79: switch(argc) {
! 80: case 2: /* optional first argument is input file */
! 81: if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) {
! 82: (void)fprintf(stderr, "uuencode: %s: %s.\n",
! 83: *argv, strerror(errno));
! 84: exit(1);
! 85: }
! 86: #define RWX (S_IRWXU|S_IRWXG|S_IRWXO)
! 87: mode = sb.st_mode & RWX;
! 88: ++argv;
! 89: break;
! 90: case 1:
! 91: #define RW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
! 92: mode = RW & ~umask(RW);
! 93: break;
! 94: case 0:
! 95: default:
! 96: usage();
! 97: }
! 98:
! 99: (void)printf("begin %o %s\n", mode, *argv);
! 100: encode();
! 101: (void)printf("end\n");
! 102: if (ferror(stdout)) {
! 103: (void)fprintf(stderr, "uuencode: write error.\n");
! 104: exit(1);
! 105: }
! 106: exit(0);
! 107: }
! 108:
! 109: /* ENC is the basic 1 character encoding function to make a char printing */
! 110: #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
! 111:
! 112: /*
! 113: * copy from in to out, encoding as you go along.
! 114: */
! 115: static void
! 116: encode()
! 117: {
! 118: register int ch, n;
! 119: register char *p;
! 120: char buf[80];
! 121:
! 122: while (n = fread(buf, 1, 45, stdin)) {
! 123: ch = ENC(n);
! 124: if (putchar(ch) == EOF)
! 125: break;
! 126: for (p = buf; n > 0; n -= 3, p += 3) {
! 127: ch = *p >> 2;
! 128: ch = ENC(ch);
! 129: if (putchar(ch) == EOF)
! 130: break;
! 131: ch = (*p << 4) & 060 | (p[1] >> 4) & 017;
! 132: ch = ENC(ch);
! 133: if (putchar(ch) == EOF)
! 134: break;
! 135: ch = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
! 136: ch = ENC(ch);
! 137: if (putchar(ch) == EOF)
! 138: break;
! 139: ch = p[2] & 077;
! 140: ch = ENC(ch);
! 141: if (putchar(ch) == EOF)
! 142: break;
! 143: }
! 144: if (putchar('\n') == EOF)
! 145: break;
! 146: }
! 147: if (ferror(stdin)) {
! 148: (void)fprintf(stderr, "uuencode: read error.\n");
! 149: exit(1);
! 150: }
! 151: ch = ENC('\0');
! 152: (void)putchar(ch);
! 153: (void)putchar('\n');
! 154: }
! 155:
! 156: static void
! 157: usage()
! 158: {
! 159: (void)fprintf(stderr,"usage: uuencode [infile] remotefile\n");
! 160: exit(1);
! 161: }