[BACK]Return to uuencode.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / uuencode

Annotation of src/usr.bin/uuencode/uuencode.c, Revision 1.5

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