Annotation of 40BSD/cmd/uuencode.c, revision 1.1

1.1     ! root        1: static char *sccsid = "@(#)uuencode.c  4.1 (Berkeley) 10/1/80";
        !             2: /*
        !             3:  * uuencode [input] output
        !             4:  *
        !             5:  * Encode a file so it can be mailed to a remote system.
        !             6:  */
        !             7: #include <stdio.h>
        !             8: #include <sys/types.h>
        !             9: #include <sys/stat.h>
        !            10: 
        !            11: /* ENC is the basic 1 character encoding function to make a char printing */
        !            12: #define ENC(c) (((c) & 077) + ' ')
        !            13: 
        !            14: main(argc, argv)
        !            15: char **argv;
        !            16: {
        !            17:        FILE *in;
        !            18:        struct stat sbuf;
        !            19:        int mode;
        !            20: 
        !            21:        /* optional 1st argument */
        !            22:        if (argc > 2) {
        !            23:                if ((in = fopen(argv[1], "r")) == NULL) {
        !            24:                        perror(argv[1]);
        !            25:                        exit(1);
        !            26:                }
        !            27:                argv++; argc--;
        !            28:        } else
        !            29:                in = stdin;
        !            30: 
        !            31:        if (argc != 2) {
        !            32:                printf("Usage: uuencode [infile] remotefile\n");
        !            33:                exit(2);
        !            34:        }
        !            35: 
        !            36:        /* figure out the input file mode */
        !            37:        fstat(fileno(in), &sbuf);
        !            38:        mode = sbuf.st_mode & 0777;
        !            39:        printf("begin %o %s\n", mode, argv[1]);
        !            40: 
        !            41:        encode(in, stdout);
        !            42: 
        !            43:        printf("end\n");
        !            44:        exit(0);
        !            45: }
        !            46: 
        !            47: /*
        !            48:  * copy from in to out, encoding as you go along.
        !            49:  */
        !            50: encode(in, out)
        !            51: FILE *in;
        !            52: FILE *out;
        !            53: {
        !            54:        char buf[80];
        !            55:        int i, n;
        !            56: 
        !            57:        for (;;) {
        !            58:                /* 1 (up to) 45 character line */
        !            59:                n = fr(in, buf, 45);
        !            60:                putc(ENC(n), out);
        !            61: 
        !            62:                for (i=0; i<n; i += 3)
        !            63:                        outdec(&buf[i], out);
        !            64: 
        !            65:                putc('\n', out);
        !            66:                if (n <= 0)
        !            67:                        break;
        !            68:        }
        !            69: }
        !            70: 
        !            71: /*
        !            72:  * output one group of 3 bytes, pointed at by p, on file f.
        !            73:  */
        !            74: outdec(p, f)
        !            75: char *p;
        !            76: FILE *f;
        !            77: {
        !            78:        int c1, c2, c3, c4;
        !            79: 
        !            80:        c1 = *p >> 2;
        !            81:        c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
        !            82:        c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
        !            83:        c4 = p[2] & 077;
        !            84:        putc(ENC(c1), f);
        !            85:        putc(ENC(c2), f);
        !            86:        putc(ENC(c3), f);
        !            87:        putc(ENC(c4), f);
        !            88: }
        !            89: 
        !            90: /* fr: like read but stdio */
        !            91: int
        !            92: fr(fd, buf, cnt)
        !            93: FILE *fd;
        !            94: char *buf;
        !            95: int cnt;
        !            96: {
        !            97:        int c, i;
        !            98: 
        !            99:        for (i=0; i<cnt; i++) {
        !           100:                c = getc(fd);
        !           101:                if (c == EOF)
        !           102:                        return(i);
        !           103:                buf[i] = c;
        !           104:        }
        !           105:        return (cnt);
        !           106: }

unix.superglobalmegacorp.com

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