home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume10 / newsbr.eak < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  8.9 KB

  1. From decwrl!wuarchive!uunet!allbery Tue Jan 30 09:20:05 PST 1990
  2. Article 1315 of comp.sources.misc:
  3. Path: decwrl!wuarchive!uunet!allbery
  4. From: grwalter@watfun.waterloo.edu (Fred Walter)
  5. Newsgroups: comp.sources.misc
  6. Subject: v10i045: newsbreak.c; automate unpacking source and binary group postings
  7. Message-ID: <77896@uunet.UU.NET>
  8. Date: 28 Jan 90 18:29:16 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Lines: 293
  11. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  12.  
  13. Posting-number: Volume 10, Issue 45
  14. Submitted-by: grwalter@watfun.waterloo.edu (Fred Walter)
  15. Archive-name: newsbreak
  16.  
  17. Here is a program I've written to automate unpacking postings to
  18. comp.sources.misc, comp.sources.unix, comp.sources.amiga, comp.binaries.amiga
  19. and possibly other groups as well.
  20.  
  21. I typically use this by creating a subdirectory in /tmp, then copying all
  22. the articles from /usr/spool/news/comp/whatever to my directory in /tmp.
  23.  
  24. Then I execute
  25.  
  26. newsbreak >& .newsbreak.out &
  27.  
  28. in my subdirectory in /tmp. After a while, all the postings that match the
  29. accepted forms will be unpacked. I then look into the .newsbreak.out
  30. file to see if any problems occured when unpacking.
  31.  
  32.     fred
  33.  
  34. ------cut here and make my day--------------------------------
  35. #! /bin/sh
  36. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  37. # Contents:  newsbreak.c
  38. echo extracting 'newsbreak.c'
  39. if test -f 'newsbreak.c' -a -z "$1"; then echo Not overwriting 'newsbreak.c'; else
  40. sed 's/^X//' << \EOF > 'newsbreak.c'
  41. X/*
  42. X * newsbreak.c (version 1.06)
  43. X *
  44. X * by G. R. Walter (Fred) December 30, 1988
  45. X *
  46. X * Description:
  47. X *     Takes a series of files which are shar files (strips any
  48. X *     garbage at the start of the shar file) that have been posted to
  49. X *     comp.{sources|binaries}.* and feeds them through sh.
  50. X *     After they have been fed through sh the original files are
  51. X *     deleted. Then any uuencoded files are uudecoded, after which
  52. X *     the uuencoded files are also deleted.
  53. X *
  54. X * NOTES:
  55. X *     1) This program assumes that all necessary shar files are in the
  56. X *        current directory. It attempts to not delete stuff if it can't
  57. X *        handle it (like when not all the parts of a multi-part uuencoded
  58. X *        file are available).
  59. X *     2) When there are multiple parts to a uuencoded file, a maximum
  60. X *        of 99 parts are currently handled.
  61. X *
  62. X * HISTORY:
  63. X * 1.00 - original version
  64. X * 1.01 - small enhancements/bug fixes
  65. X * 1.02 - now handle .zu's with > 9 parts correctly
  66. X * 1.03 - now check for ":\tshar\:Shell Archiver" when checking if a file
  67. X *        is a shar archive
  68. X *      - now handles files ending in .uu#
  69. X * 1.04 - now check for ": run sh on this file to unbundle" when checking
  70. X *        if a file is a shar archive
  71. X * 1.05 - now check for "# This is a shell archive." when checking
  72. X *        if a file is a shar archive
  73. X *      - now prints out the version (and author name) when run
  74. X * 1.06 - now check for "Archive-name:" to see what directory the
  75. X *        resulting files should be put in. NOTE: any parts after
  76. X *        a "part*" section in the path are not mkdir'ed
  77. X *      - now handles files ending in .zuu#
  78. X *      - now handles files ending in .uu# properly
  79. X *      - now doesn't attempt to process files starting in "."
  80. X *      - now prints some useful info (so you know what it is doing)
  81. X *      - now check for "# After you unpack everything" when checking
  82. X *        if a file is a shar archive
  83. X *      - make sure I don't try to uudecode directories
  84. X */
  85. X
  86. X#include <stdio.h>
  87. X#include <ctype.h>
  88. X#include <sys/types.h>
  89. X#include <sys/dir.h>
  90. X#include <sys/file.h>
  91. X#include <sys/stat.h>
  92. X
  93. Xchar           *strcpy();
  94. Xchar           *getwd();
  95. X
  96. Xmain()
  97. X{
  98. X    DIR            *dirp;
  99. X    struct direct  *dp;
  100. X
  101. X    void            unshar();
  102. X    void            uudecode();
  103. X
  104. X    fprintf(stderr, "newsbreak 1.06 by G. R. Walter\n");
  105. X
  106. X    /* unshar everything */
  107. X    dirp = opendir(".");
  108. X    for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  109. X    unshar(dp->d_name);
  110. X    closedir(dirp);
  111. X
  112. X    /* uudecode everything */
  113. X    dirp = opendir(".");
  114. X    for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  115. X    uudecode(dp->d_name);
  116. X    closedir(dirp);
  117. X
  118. X    exit(0);
  119. X}
  120. X
  121. Xvoid
  122. Xunshar(name)
  123. X    char           *name;
  124. X{
  125. X    FILE           *fin;
  126. X    FILE           *fout;
  127. X    char            buf[200];
  128. X    char            dir[200];
  129. X    char            path[200];
  130. X    char            tmp_path[200];
  131. X    char           *p;
  132. X
  133. X    void            uudecode();
  134. X
  135. X    if (name[0] == '.')
  136. X    return;
  137. X
  138. X    fprintf(stderr, "Attempting to unshar %s\n", name);
  139. X    fin = fopen(name, "r");
  140. X    if (fin == NULL)        /* file doesn't exist !? */
  141. X    return;
  142. X
  143. X    strcpy(dir, ".");        /* initialize directory to use */
  144. X    for (;;) {
  145. X    if (fgets(buf, 200, fin) == NULL) {    /* not a shar file !? */
  146. X        fclose(fin);
  147. X        return;
  148. X    }
  149. X    if (strncmp(buf, "Archive-name:", 13) == 0) {    /* directory to use */
  150. X        if (sscanf(buf, "Archive-name: %s", path) > 0) {
  151. X        strcpy(dir, path);
  152. X        tmp_path[0] = '\0';
  153. X        fprintf(stderr, "Found Archive-name: %s\n", dir);
  154. X        for (p = dir; *p != NULL; p++)
  155. X            if (*p == '/') {
  156. X            *p = NULL;
  157. X            if (strncmp(p + 1, "part", 4) == 0)
  158. X                break;
  159. X            if (access(dir, F_OK) < 0)
  160. X                if (mkdir(dir, 0777) < 0)
  161. X                goto ABORT_ATTEMPT;
  162. X            strcpy(tmp_path, path);
  163. X            *p = '/';
  164. X            }
  165. X        if (access(dir, F_OK) < 0) {
  166. X            if (mkdir(dir, 0777) < 0) {
  167. X        ABORT_ATTEMPT:
  168. X            fprintf(stderr, "Couldn't mkdir %s\n", dir);
  169. X            fprintf(stderr, "Aborting this attempt\n");
  170. X            if (tmp_path[0] != '\0')
  171. X                (void) unlink(tmp_path);
  172. X            fclose(fin);
  173. X            return;
  174. X            }
  175. X        }
  176. X        }
  177. X    }
  178. X    if ((strncmp(buf, "#!/bin/sh", 9) == 0)
  179. X        || (strncmp(buf, "#! /bin/sh", 10) == 0)
  180. X        || (strncmp(buf, "# This is a shell archive.", 26) == 0)
  181. X        || (strncmp(buf, ":\tshar:\tShell Archiver", 22) == 0)
  182. X        || (strncmp(buf, ": run sh on this file to unbundle", 33) == 0)
  183. X        || (strncmp(buf, "# After you unpack everything", 29) == 0))
  184. X        break;
  185. X    }
  186. X
  187. X    sprintf(path, "%s/.unshar.temp.file", dir);
  188. X    fout = fopen(path, "w");
  189. X    while (fgets(buf, 200, fin) != NULL)
  190. X    fprintf(fout, "%s", buf);
  191. X    fclose(fout);
  192. X    fclose(fin);
  193. X
  194. X    sprintf(buf, "cd %s; sh .unshar.temp.file", dir);
  195. X    if (system(buf) == 0)
  196. X    (void) unlink(name);
  197. X
  198. X    (void) unlink(path);
  199. X}
  200. X
  201. Xvoid
  202. Xuudecode(name)
  203. X    char           *name;
  204. X{
  205. X    DIR            *dirp;
  206. X    struct direct  *dp;
  207. X    FILE           *file;
  208. X    char            buf[200];
  209. X    char            name_buf[200];
  210. X    char            path[200];
  211. X    char           *p;
  212. X    struct stat     stat_buf;
  213. X
  214. X    if (name[0] == '.')
  215. X    return;
  216. X
  217. X    if (stat(name, &stat_buf))
  218. X    return;
  219. X
  220. X    if ((stat_buf.st_mode & S_IFDIR)) {
  221. X    /* uudecode everything in this directory */
  222. X    if (!getwd(path))
  223. X        return;
  224. X    if (chdir(name))
  225. X        return;
  226. X    dirp = opendir(".");
  227. X    for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  228. X        uudecode(dp->d_name);
  229. X    closedir(dirp);
  230. X    (void) chdir(path);
  231. X    return;
  232. X    }
  233. X    /* check if the file still exists */
  234. X    file = fopen(name, "r");
  235. X    if (file == NULL)
  236. X    return;
  237. X    fclose(file);
  238. X
  239. X    /* if the file ends in ".uue" or ".uu" or ".zuu" just uudecode it */
  240. X    p = name + strlen(name) - 1;
  241. X    if ((*p == 'e') && (*(p - 1) == 'u')) {
  242. X    p -= 2;
  243. X    if ((*p == 'u') && (*(p - 1) == '.')) {
  244. X        sprintf(buf, "uudecode %s", name);
  245. X        if (system(buf) == 0)
  246. X        (void) unlink(name);
  247. X    }
  248. X    return;
  249. X    }
  250. X    if ((*p == 'u') && (*(p - 1) == 'u')) {
  251. X    p -= 2;
  252. X    if ((*p == '.') || ((*p == 'z') && (*(p - 1) == '.'))) {
  253. X        sprintf(buf, "uudecode %s", name);
  254. X        if (system(buf) == 0)
  255. X        (void) unlink(name);
  256. X    }
  257. X    return;
  258. X    }
  259. X    /* handle ".zuu#", ".zu#" and ".uu#" where # is a number */
  260. X    while (isdigit(*p))
  261. X    p--;
  262. X
  263. X    if ((*p == 'u') && ((*(p - 1) == 'z') || (*(p - 1) == 'u')) &&
  264. X    (*(p - 2) == '.')) {
  265. X
  266. X    if (*(p + 1) == '0') {
  267. X        *(p + 1) = '\0';
  268. X        sprintf(buf, "cat %s* | uudecode", name);
  269. X    } else {
  270. X        *(p + 1) = '\0';
  271. X
  272. X        sprintf(name_buf, "%s10", name);
  273. X        file = fopen(name_buf, "r");
  274. X        if (file == NULL) {
  275. X        sprintf(buf, "cat %s? | uudecode", name);
  276. X        } else {
  277. X        fclose(file);
  278. X        sprintf(buf, "cat %s? %s?? | uudecode", name, name);
  279. X        }
  280. X    }
  281. X
  282. X    if (system(buf) == 0) {
  283. X        sprintf(buf, "rm %s*", name);
  284. X        system(buf);
  285. X    }
  286. X    } else if ((*p == 'u') && (*(p - 1) == 'u') && (*(p - 2) == 'z') &&
  287. X           (*(p - 3) == '.')) {
  288. X    *(p + 1) = '\0';
  289. X
  290. X    sprintf(buf, "cat %s* | uudecode", name);
  291. X    if (system(buf) == 0) {
  292. X        sprintf(buf, "rm %s*", name);
  293. X        system(buf);
  294. X    }
  295. X    }
  296. X}
  297. EOF
  298. chars=`wc -c < 'newsbreak.c'`
  299. if test $chars !=     6939; then echo 'newsbreak.c' is $chars characters, should be     6939 characters!; fi
  300. fi
  301. exit 0
  302. grwalter@watmath.uwaterloo.ca                  (Canadian domain)
  303. grwalter@watmath.waterloo.edu                  (US Internet, including CSNET)
  304. grwalter@watmath.waterloo.cdn                  (CDNnet and some European nets)
  305. watmath!grwalter                               (UUCP)
  306.  
  307.  
  308.