[comp.sys.amiga] automate comp.{binaries|sources}.amiga article unbundling

grwalter@watcgl.waterloo.edu (Fred Walter) (12/31/88)

With the volume coming through comp.{binaries|sources}.amiga lately
(Bob, you're working too hard :-) :-)) I threw together a program (which I
call newsbreak) to do all the stripping of headers, unshar'ing, cat'ing and
uudecode'ing for me. It currently only works under Unix (since I do all
the above before I send stuff down to my Amiga), but as soon as I can get
AmigaUUCP working on my setup, I'll have a version that works on an Amiga
as well. 

Think of this as a belated Christmass present :-)

If anyone enhances this, I'd appreciate a copy.

	fred

-------cut-here----------ouch!--------------------
/*
 * newsbreak.c (version 1.00)
 *
 * 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.
 *
 * NOTE:
 *     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).
 */

#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)
	    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           *ptr;

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

    /* if the file ends in ".uu" or ".zuu" just uudecode it */
    ptr = name + strlen(name) - 1;
    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);
	}
    }
    /* handle ".zu#where # is a number */
    while (isdigit(*ptr))
	ptr--;

    if ((*ptr == 'u') && ((*(ptr - 1) == 'z') && (*(ptr - 2) == '.'))) {
	*(ptr + 1) = NULL;
	sprintf(buf, "cat %s* | uudecode", name);
	if (system(buf) == 0) {
	    sprintf(buf, "rm %s*", name);
	    system(buf);
	}
    }
}