[comp.sys.amiga] newsbreak 1.04

grwalter@watmath.waterloo.edu (Fred Walter) (05/13/89)

In article <104429@sun.Eng.Sun.COM> page@sun.UUCP (Bob Page) writes:
>sparks@corpane.UUCP (John Sparks) wrote:
>>There is no 'unshar' program on unix (or ultrix).
>
>Actually there are a couple of them.  They simply take a file, toss
>the junk before the #!/bin/sh header, and feed the rest to 'sh'.  Much
>easier than editing those pesky headers by hand.
>
>X * newsbreak.c (version 1.02)

I have a later verion of this which will work with the bundles that (at least
one of) the Amiga Archive mail server sends out.

	fred

------cut-me--------------------------and-I'll-sue!!!!!!!----------------
/*
 * newsbreak.c (version 1.04)
 *
 * by G. R. Walter (Fred) December 30, 1988
 *
 * Description:
 *     Takes a series of files which are shar files (strips any
 *     garbage at the start of the shar file) that have been posted to
 *     comp.{sources|binaries}.amiga and feeds them through sh.
 *     After they have been fed through sh the original files are
 *     deleted. Then any uuencoded files are uudecoded, after which
 *     the uuencoded files are also deleted.
 *
 * NOTES:
 *     1) This program assumes that all necessary shar files are in the
 *        current directory. It attempts to not delete stuff if it can't
 *        handle it (like when not all the parts of a multi-part uuencoded
 *        file are available).
 *     2) When there are multiple parts to a uuencoded file, a maximum
 *        of 99 parts are currently handled.
 *
 * HISTORY:
 * 1.00 - original version
 *        (GRWalter)
 *
 * 1.01 - small enhancements/bug fixes
 *        (GRWalter)
 *
 * 1.02 - now handle .zu's with > 9 parts correctly
 *        (GRWalter)
 *
 * 1.03 - now check for ":\tshar\:Shell Archiver" when checking if a file
 *        is a shar archive
 *      - now handles files ending in .uu#
 *        (GRWalter)
 *
 * 1.04 - now check for ": run sh on this file to unbundle" when checking
 *        if a file is a shar archive
 *        (GRWalter)
 */

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/dir.h>

main()
{
    DIR            *dirp;
    struct direct  *dp;

    void            unshar();
    void            uudecode();

    /* unshar everything */
    dirp = opendir(".");
    for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
	unshar(dp->d_name);
    closedir(dirp);

    /* uudecode everything */
    dirp = opendir(".");
    for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
	uudecode(dp->d_name);
    closedir(dirp);

    exit(0);
}

void
unshar(name)
    char           *name;
{
    FILE           *fin;
    FILE           *fout;
    char            buf[200];

    fin = fopen(name, "r");
    if (fin == NULL)		/* file doesn't exist !? */
	return;

    for (;;) {
	if (fgets(buf, 200, fin) == NULL) {	/* not a shar file !? */
	    fclose(fin);
	    return;
	}
	if ((strncmp(buf, "#!/bin/sh", 9) == 0)
	 || (strncmp(buf, "#! /bin/sh", 10) == 0)
	 || (strncmp(buf, ":\tshar:\tShell Archiver", 22) == 0)
	 || (strncmp(buf, ": run sh on this file to unbundle", 33) == 0))
	    break;
    }

    fout = fopen(".unshar.temp.file", "w");
    while (fgets(buf, 200, fin) != NULL)
	fprintf(fout, "%s", buf);
    fclose(fout);
    fclose(fin);

    if (system("sh .unshar.temp.file >> .unshar.output") == 0)
        unlink(name);

    unlink(".unshar.temp.file");
}

void
uudecode(name)
    char           *name;
{
    FILE           *file;
    char            buf[200];
    char            name_buf[200];
    char           *ptr;

    /* check if the file still exists */
    file = fopen(name, "r");
    if (file == NULL)
	return;
    fclose(file);

    /* if the file ends in ".uue" or ".uu" or ".zuu" just uudecode it */
    ptr = name + strlen(name) - 1;
    if ((*ptr == 'e') && (*(ptr - 1) == 'u')) {
	ptr -= 2;
	if ((*ptr == 'u') && (*(ptr - 1) == '.')) {
	    sprintf(buf, "uudecode %s", name);
	    if (system(buf) == 0)
	        unlink(name);
	}
	return;
    }
    if ((*ptr == 'u') && (*(ptr - 1) == 'u')) {
	ptr -= 2;
	if ((*ptr == '.') || ((*ptr == 'z') && (*(ptr - 1) == '.'))) {
	    sprintf(buf, "uudecode %s", name);
	    if (system(buf) == 0)
	        unlink(name);
	}
	return;
    }

    /* handle ".zu#" and ".uu#" where # is a number */
    while (isdigit(*ptr))
	ptr--;

    if ((*ptr == 'u') && ((*(ptr - 1) == 'z') || (*(ptr - 1) == 'u')) &&
        (*(ptr - 2) == '.')) {
	*(ptr + 1) = '\0';

	sprintf(name_buf, "%s10", name);
        file = fopen(name_buf, "r");
        if (file == NULL) {
	    sprintf(buf, "cat %s? | uudecode", name);
	} else {
            fclose(file);
	    sprintf(buf, "cat %s? %s?? | uudecode", name, name);
	}

	if (system(buf) == 0) {
	    sprintf(buf, "rm %s*", name);
	    system(buf);
	}
    }
}