uudecode?

root root@jacob.remcomp.fr
Sun Jan 31 23:52:00 GMT 1999


Let's stop this uuencode/decode discussion.
Here are two small little programs in that old but powerful language we all
know somehow.

------------------------------------------------------cut here: uuencode.c
#include <stdio.h>
#define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
static int uuencode(char *nom)  /* nom means 'name' in french :-) */
{
    int ch, n, idx;
    register char *p;
    char buf[80], outbuf[250];
    FILE *in;

    if ((in = fopen(nom, "rb")) == NULL)
        return 1;		/* please add some error checking/reporting */
    printf("begin 0666 %s\n",nom);/* note: no attempt to fake any permissions */
    while ((n = fread(buf, 1, 45, in)) != 0) {
        idx = 0;
        outbuf[idx++] = ENC(ch);
        for (p = buf; n > 0; n -= 3, p += 3) {
            ch = *p >> 2;
            ch = ENC(ch);
            outbuf[idx++] = ch;
            ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
            ch = ENC(ch);
            outbuf[idx++] = ch;
            ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
            ch = ENC(ch);
            outbuf[idx++] = ch;
            ch = p[2] & 077;
            ch = ENC(ch);
            outbuf[idx++] = ch;
        }
        outbuf[idx++] = '\n';
        outbuf[idx] = 0;
        printf("%s", outbuf);
    }
    printf("%c\nend\n", ENC(0));
    fclose(in);
    return 0;
}
int main(int argc, char *argv[]) // add error checking here if you feel like
{
    return uuencode(argv[1]);
}
---------------------------------------------------cut here uudecode.c
#include <stdio.h>
#include <string.h>
#define DEC(c)  (((c) - ' ') & 077)
static int uudecode(char *fname)
{
    register int n;
    register char ch, *p;
    char buf[2 * BUFSIZ],outname[512];
    FILE *out, *in = fopen(fname, "rb");
    if (in == NULL) {
        fprintf(stderr,"Impossible to open %s\n",fname);
        return 1;
    }
    while (fgets(buf, 80, in)) {
        if (!strncmp(buf, "begin", 5))
            goto decodeStart;
    }
    fprintf(stderr,"No 'begin' line found in %s\n",fname);
    return 1;
decodeStart:
    p = buf + sizeof("begin");
    while (*p == ' ' || *p == '\t')
        p++;
    while (*p >= '0' && *p <= '9') // ignore any permissions setting
        p++;
    while (*p == ' ' || *p == '\t')
        p++;
    strcpy(outname, p);
    p = outname;
    while (*p >= ' ') // This supposes file name chars are >= ' '
        p++;
    *p = 0;
    out = fopen(outname, "wb");
    if (out == NULL) {
        fprintf(stderr,"Impossible to open %s\n",outname);
        return 1;
    }
    while (1) {
        if (!fgets(buf, 80, in))
            break;
        if (!strncmp(buf, "end\r\n", 5))
            break;
        p = buf;
        n = DEC(*p);
        if (n <= 0)
            break;
        for (++p; n > 0; p += 4, n -= 3) {
            if (n >= 3) {
                ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
                fputc(ch, out);
                ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
                fputc(ch, out);
                ch = DEC(p[2]) << 6 | DEC(p[3]);
                fputc(ch, out);
            }
            else {
                if (n >= 1) {
                    ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
                    fputc(ch, out);
                }
                if (n >= 2) {
                    ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
                    fputc(ch, out);
                }
            }
        }
    }
    fclose(out);
    fclose(in);
    return 0;
}
int main(int argc, char *argv[]) // Add error checking here if you feel like
{
    return uudecode(argv[1]);
}

---------------------------------------------------------------------------
enjoy!
-- 
Jacob Navia	Logiciels/Informatique
41 rue Maurice Ravel			Tel 01 48.23.51.44
93430 Villetaneuse 			Fax 01 48.23.95.39
France
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".



More information about the Cygwin mailing list