Annotation of coherent/g/usr/bin/uudecode.c, revision 1.1.1.1

1.1       root        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[] = "@(#)uudecode.c 5.10 (Berkeley) 6/1/90";
                     36: #endif /* not lint */
                     37: 
                     38: /*
                     39:  * uudecode [file ...]
                     40:  *
                     41:  * create the specified file, decoding as you go.
                     42:  * used with uuencode.
                     43:  */
                     44: #include <sys/param.h>
                     45: #include <sys/stat.h>
                     46: #include <pwd.h>
                     47: #include <stdio.h>
                     48: #include <string.h>
                     49: 
                     50: #define MAXPATHLEN 128
                     51: 
                     52: char *filename;
                     53: 
                     54: /* ARGSUSED */
                     55: main(argc, argv)
                     56:        int argc;
                     57:        char **argv;
                     58: {
                     59:        extern int errno;
                     60:        int rval;
                     61: 
                     62:        if (*++argv) {
                     63:                rval = 0;
                     64:                do {
                     65:                        if (!freopen(filename = *argv, "r", stdin)) {
                     66:                                (void)fprintf(stderr, "uudecode: %s: %s\n",
                     67:                                    *argv, strerror(errno));
                     68:                                rval = 1;
                     69:                                continue;
                     70:                        }
                     71:                        rval |= decode();
                     72:                } while (*++argv);
                     73:        } else {
                     74:                filename = "stdin";
                     75:                rval = decode();
                     76:        }
                     77:        exit(rval);
                     78: }
                     79: 
                     80: decode()
                     81: {
                     82:        extern int errno;
                     83:        struct passwd *pw;
                     84:        register int n;
                     85:        register char ch, *p;
                     86:        int mode, n1;
                     87:        char buf[MAXPATHLEN];
                     88: 
                     89:        /* search for header line */
                     90:        do {
                     91:                if (!fgets(buf, sizeof(buf), stdin)) {
                     92:                        (void)fprintf(stderr,
                     93:                            "uudecode: %s: no \"begin\" line\n", filename);
                     94:                        return(1);
                     95:                }
                     96:        } while (strncmp(buf, "begin ", 6));
                     97:        (void)sscanf(buf, "begin %o %s", &mode, buf);
                     98: 
                     99:        /* handle ~user/file format */
                    100:        if (buf[0] == '~') {
                    101:                if (!(p = index(buf, '/'))) {
                    102:                        (void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
                    103:                            filename);
                    104:                        return(1);
                    105:                }
                    106:                *p++ = NULL;
                    107:                if (!(pw = getpwnam(buf + 1))) {
                    108:                        (void)fprintf(stderr, "uudecode: %s: no user %s.\n",
                    109:                            filename, buf);
                    110:                        return(1);
                    111:                }
                    112:                n = strlen(pw->pw_dir);
                    113:                n1 = strlen(p);
                    114:                if (n + n1 + 2 > MAXPATHLEN) {
                    115:                        (void)fprintf(stderr, "uudecode: %s: path too long.\n",
                    116:                            filename);
                    117:                        return(1);
                    118:                }
                    119:                /*
                    120:                 *      bcopy(p, buf + n + 1, n1 + 1);
                    121:                 *      bcopy(pw->pw_dir, buf, n);
                    122:                 *      buf[n] = '/';
                    123:                 */
                    124:                sprintf(buf, "%s/%s", pw->pw_dir, p);
                    125:        }
                    126: 
                    127:        /* create output file, set mode */
                    128:        if (!freopen(buf, "w", stdout) || chmod(buf, mode&0666) == -1) {
                    129:                (void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
                    130:                    filename, strerror(errno));
                    131:                return(1);
                    132:        }
                    133: 
                    134:        /* for each input line */
                    135:        for (;;) {
                    136:                if (!fgets(p = buf, sizeof(buf), stdin)) {
                    137:                        (void)fprintf(stderr, "uudecode: %s: short file.\n",
                    138:                            filename);
                    139:                        return(1);
                    140:                }
                    141: #define        DEC(c)  (((c) - ' ') & 077)             /* single character decode */
                    142:                /*
                    143:                 * `n' is used to avoid writing out all the characters
                    144:                 * at the end of the file.
                    145:                 */
                    146:                if ((n = DEC(*p)) <= 0)
                    147:                        break;
                    148:                for (++p; n > 0; p += 4, n -= 3)
                    149:                        if (n >= 3) {
                    150:                                ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
                    151:                                putchar(ch);
                    152:                                ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
                    153:                                putchar(ch);
                    154:                                ch = DEC(p[2]) << 6 | DEC(p[3]);
                    155:                                putchar(ch);
                    156:                        }
                    157:                        else {
                    158:                                if (n >= 1) {
                    159:                                        ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
                    160:                                        putchar(ch);
                    161:                                }
                    162:                                if (n >= 2) {
                    163:                                        ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
                    164:                                        putchar(ch);
                    165:                                }
                    166:                                if (n >= 3) {
                    167:                                        ch = DEC(p[2]) << 6 | DEC(p[3]);
                    168:                                        putchar(ch);
                    169:                                }
                    170:                        }
                    171:        }
                    172:        if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
                    173:                (void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
                    174:                    filename);
                    175:                return(1);
                    176:        }
                    177:        return(0);
                    178: }
                    179: 
                    180: usage()
                    181: {
                    182:        (void)fprintf(stderr, "usage: uudecode [file ...]\n");
                    183:        exit(1);
                    184: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.