home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume10
/
newsbreak
< prev
next >
Wrap
Text File
|
1990-01-28
|
9KB
|
299 lines
Newsgroups: comp.sources.misc
subject: v10i045: newsbreak.c; automate unpacking source and binary group postings
from: grwalter@watfun.waterloo.edu (Fred Walter)
Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
Posting-number: Volume 10, Issue 45
Submitted-by: grwalter@watfun.waterloo.edu (Fred Walter)
Archive-name: newsbreak
Here is a program I've written to automate unpacking postings to
comp.sources.misc, comp.sources.unix, comp.sources.amiga, comp.binaries.amiga
and possibly other groups as well.
I typically use this by creating a subdirectory in /tmp, then copying all
the articles from /usr/spool/news/comp/whatever to my directory in /tmp.
Then I execute
newsbreak >& .newsbreak.out &
in my subdirectory in /tmp. After a while, all the postings that match the
accepted forms will be unpacked. I then look into the .newsbreak.out
file to see if any problems occured when unpacking.
fred
------cut here and make my day--------------------------------
#! /bin/sh
# This file was wrapped with "dummyshar". "sh" this file to extract.
# Contents: newsbreak.c
echo extracting 'newsbreak.c'
if test -f 'newsbreak.c' -a -z "$1"; then echo Not overwriting 'newsbreak.c'; else
sed 's/^X//' << \EOF > 'newsbreak.c'
X/*
X * newsbreak.c (version 1.06)
X *
X * by G. R. Walter (Fred) December 30, 1988
X *
X * Description:
X * Takes a series of files which are shar files (strips any
X * garbage at the start of the shar file) that have been posted to
X * comp.{sources|binaries}.* and feeds them through sh.
X * After they have been fed through sh the original files are
X * deleted. Then any uuencoded files are uudecoded, after which
X * the uuencoded files are also deleted.
X *
X * NOTES:
X * 1) This program assumes that all necessary shar files are in the
X * current directory. It attempts to not delete stuff if it can't
X * handle it (like when not all the parts of a multi-part uuencoded
X * file are available).
X * 2) When there are multiple parts to a uuencoded file, a maximum
X * of 99 parts are currently handled.
X *
X * HISTORY:
X * 1.00 - original version
X * 1.01 - small enhancements/bug fixes
X * 1.02 - now handle .zu's with > 9 parts correctly
X * 1.03 - now check for ":\tshar\:Shell Archiver" when checking if a file
X * is a shar archive
X * - now handles files ending in .uu#
X * 1.04 - now check for ": run sh on this file to unbundle" when checking
X * if a file is a shar archive
X * 1.05 - now check for "# This is a shell archive." when checking
X * if a file is a shar archive
X * - now prints out the version (and author name) when run
X * 1.06 - now check for "Archive-name:" to see what directory the
X * resulting files should be put in. NOTE: any parts after
X * a "part*" section in the path are not mkdir'ed
X * - now handles files ending in .zuu#
X * - now handles files ending in .uu# properly
X * - now doesn't attempt to process files starting in "."
X * - now prints some useful info (so you know what it is doing)
X * - now check for "# After you unpack everything" when checking
X * if a file is a shar archive
X * - make sure I don't try to uudecode directories
X */
X
X#include <stdio.h>
X#include <ctype.h>
X#include <sys/types.h>
X#include <sys/dir.h>
X#include <sys/file.h>
X#include <sys/stat.h>
X
Xchar *strcpy();
Xchar *getwd();
X
Xmain()
X{
X DIR *dirp;
X struct direct *dp;
X
X void unshar();
X void uudecode();
X
X fprintf(stderr, "newsbreak 1.06 by G. R. Walter\n");
X
X /* unshar everything */
X dirp = opendir(".");
X for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
X unshar(dp->d_name);
X closedir(dirp);
X
X /* uudecode everything */
X dirp = opendir(".");
X for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
X uudecode(dp->d_name);
X closedir(dirp);
X
X exit(0);
X}
X
Xvoid
Xunshar(name)
X char *name;
X{
X FILE *fin;
X FILE *fout;
X char buf[200];
X char dir[200];
X char path[200];
X char tmp_path[200];
X char *p;
X
X void uudecode();
X
X if (name[0] == '.')
X return;
X
X fprintf(stderr, "Attempting to unshar %s\n", name);
X fin = fopen(name, "r");
X if (fin == NULL) /* file doesn't exist !? */
X return;
X
X strcpy(dir, "."); /* initialize directory to use */
X for (;;) {
X if (fgets(buf, 200, fin) == NULL) { /* not a shar file !? */
X fclose(fin);
X return;
X }
X if (strncmp(buf, "Archive-name:", 13) == 0) { /* directory to use */
X if (sscanf(buf, "Archive-name: %s", path) > 0) {
X strcpy(dir, path);
X tmp_path[0] = '\0';
X fprintf(stderr, "Found Archive-name: %s\n", dir);
X for (p = dir; *p != NULL; p++)
X if (*p == '/') {
X *p = NULL;
X if (strncmp(p + 1, "part", 4) == 0)
X break;
X if (access(dir, F_OK) < 0)
X if (mkdir(dir, 0777) < 0)
X goto ABORT_ATTEMPT;
X strcpy(tmp_path, path);
X *p = '/';
X }
X if (access(dir, F_OK) < 0) {
X if (mkdir(dir, 0777) < 0) {
X ABORT_ATTEMPT:
X fprintf(stderr, "Couldn't mkdir %s\n", dir);
X fprintf(stderr, "Aborting this attempt\n");
X if (tmp_path[0] != '\0')
X (void) unlink(tmp_path);
X fclose(fin);
X return;
X }
X }
X }
X }
X if ((strncmp(buf, "#!/bin/sh", 9) == 0)
X || (strncmp(buf, "#! /bin/sh", 10) == 0)
X || (strncmp(buf, "# This is a shell archive.", 26) == 0)
X || (strncmp(buf, ":\tshar:\tShell Archiver", 22) == 0)
X || (strncmp(buf, ": run sh on this file to unbundle", 33) == 0)
X || (strncmp(buf, "# After you unpack everything", 29) == 0))
X break;
X }
X
X sprintf(path, "%s/.unshar.temp.file", dir);
X fout = fopen(path, "w");
X while (fgets(buf, 200, fin) != NULL)
X fprintf(fout, "%s", buf);
X fclose(fout);
X fclose(fin);
X
X sprintf(buf, "cd %s; sh .unshar.temp.file", dir);
X if (system(buf) == 0)
X (void) unlink(name);
X
X (void) unlink(path);
X}
X
Xvoid
Xuudecode(name)
X char *name;
X{
X DIR *dirp;
X struct direct *dp;
X FILE *file;
X char buf[200];
X char name_buf[200];
X char path[200];
X char *p;
X struct stat stat_buf;
X
X if (name[0] == '.')
X return;
X
X if (stat(name, &stat_buf))
X return;
X
X if ((stat_buf.st_mode & S_IFDIR)) {
X /* uudecode everything in this directory */
X if (!getwd(path))
X return;
X if (chdir(name))
X return;
X dirp = opendir(".");
X for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
X uudecode(dp->d_name);
X closedir(dirp);
X (void) chdir(path);
X return;
X }
X /* check if the file still exists */
X file = fopen(name, "r");
X if (file == NULL)
X return;
X fclose(file);
X
X /* if the file ends in ".uue" or ".uu" or ".zuu" just uudecode it */
X p = name + strlen(name) - 1;
X if ((*p == 'e') && (*(p - 1) == 'u')) {
X p -= 2;
X if ((*p == 'u') && (*(p - 1) == '.')) {
X sprintf(buf, "uudecode %s", name);
X if (system(buf) == 0)
X (void) unlink(name);
X }
X return;
X }
X if ((*p == 'u') && (*(p - 1) == 'u')) {
X p -= 2;
X if ((*p == '.') || ((*p == 'z') && (*(p - 1) == '.'))) {
X sprintf(buf, "uudecode %s", name);
X if (system(buf) == 0)
X (void) unlink(name);
X }
X return;
X }
X /* handle ".zuu#", ".zu#" and ".uu#" where # is a number */
X while (isdigit(*p))
X p--;
X
X if ((*p == 'u') && ((*(p - 1) == 'z') || (*(p - 1) == 'u')) &&
X (*(p - 2) == '.')) {
X
X if (*(p + 1) == '0') {
X *(p + 1) = '\0';
X sprintf(buf, "cat %s* | uudecode", name);
X } else {
X *(p + 1) = '\0';
X
X sprintf(name_buf, "%s10", name);
X file = fopen(name_buf, "r");
X if (file == NULL) {
X sprintf(buf, "cat %s? | uudecode", name);
X } else {
X fclose(file);
X sprintf(buf, "cat %s? %s?? | uudecode", name, name);
X }
X }
X
X if (system(buf) == 0) {
X sprintf(buf, "rm %s*", name);
X system(buf);
X }
X } else if ((*p == 'u') && (*(p - 1) == 'u') && (*(p - 2) == 'z') &&
X (*(p - 3) == '.')) {
X *(p + 1) = '\0';
X
X sprintf(buf, "cat %s* | uudecode", name);
X if (system(buf) == 0) {
X sprintf(buf, "rm %s*", name);
X system(buf);
X }
X }
X}
EOF
chars=`wc -c < 'newsbreak.c'`
if test $chars != 6939; then echo 'newsbreak.c' is $chars characters, should be 6939 characters!; fi
fi
exit 0
grwalter@watmath.uwaterloo.ca (Canadian domain)
grwalter@watmath.waterloo.edu (US Internet, including CSNET)
grwalter@watmath.waterloo.cdn (CDNnet and some European nets)
watmath!grwalter (UUCP)