
/*
 *  UUARCHIVE
 *
 *  UUARCHIVE infile outfile
 *
 *  Appends infile to outfile then deletes infile.  Removes any uuencoded
 *  files from infile (by looking for the begin line and deleting the
 *  contents).
 *
 *  Usually used with dmail, dmail archives to a temporary file anything
 *  you send out, then appends the temporary file to your mbox via this
 *  command when it quits.  Thus, distributions you send out do not
 *  get archived.
 */

#include <stdio.h>
#include "version.h"

IDENT(".01");

char Buf[2048];

main(ac, av)
char *av[];
{
    FILE *fi;
    FILE *fo;

    if (ac != 3) {
	puts("UUARCHIVE infile outfile");
	exit(1);
    }
    if (fi = fopen(av[1], "r")) {
	if (fo = fopen(av[2], "a")) {
	    int r = ScanArchive(fi, fo);
	    fclose(fi);
	    fclose(fo);
	    if (r >= 0)
		remove(av[1]);
	} else {
	    printf("unable to create/append %s\n", av[2]);
	}
    } else {
	printf("unable to open %s\n", av[1]);
    }
    return(0);
}

int
ScanArchive(fi, fo)
FILE *fi;
FILE *fo;
{
    while (fgets(Buf, sizeof(Buf), fi)) {
	if (strncmp(Buf, "begin ", 6) == 0 && isoct(Buf[6]) && isoct(Buf[7]) && isoct(Buf[8]) && Buf[9] == ' ') {
	    fputs(Buf, fo);
	    fprintf(fo, "** DELETED FOR ARCHIVE **\n");
	    while (fgets(Buf, sizeof(Buf), fi)) {
		if (strncmp(Buf, "From ", 5) == 0)
		    break;
		if (Buf[0] == '\n')
		    break;
		if (strncmp(Buf, "end", 3) == 0)
		    break;
	    }
	}
	fputs(Buf, fo);
    }
    if (ferror(fi) || ferror(fo))
	return(-1);
    return(1);
}

int
isoct(c)
char c;
{
    if (c >= '0' && c <= '7')
	return(1);
    return(0);
}

