home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3100 < prev    next >
Encoding:
Internet Message Format  |  1991-03-21  |  7.1 KB

  1. From: rob@mtdiablo.Concord.CA.US (Rob Bernardo)
  2. Newsgroups: news.software.b,alt.sources
  3. Subject: unbatcher out of sync - another cure
  4. Message-ID: <1991Mar18.024210.3493@mtdiablo.Concord.CA.US>
  5. Date: 18 Mar 91 02:42:10 GMT
  6.  
  7. I've also had some difficulty lately with 'out of sync' unbatching
  8. problems. Unfortunately, Eamonn McManus's patchbatch didn't work.
  9. Below is a shell archive for a more robust program to fix batches
  10. with bad article character counts. Accompanying it are a makefile
  11. and a wrapper shell script, both of which you may need to tailor
  12. to your systems. The main significant difference between my and E.M.'s
  13. batch fixers is that mine does not presume that the correct article
  14. size is close to the article size given in the bad news batch file.
  15. Mine goes through the batch finding each article and recalculating
  16. it's size from scratch.
  17.  
  18. The syntax of the command line is
  19.  
  20.     rebatch [-v] -i input_file -o output_file
  21.  
  22. where the input file is a compressed newsbatch file (e.g.  as found in
  23. your in.coming/bad directory and where the output file is not
  24. compressed.  The wrapper shell script will take care of compressing the
  25. output file (so that you can just wait till cron next runs
  26. input/newsrun).
  27.  
  28. I wrote this real quick this morning, and it worked for me. Please
  29. let me know of any problems or suggestions.
  30.  
  31. #!/bin/sh
  32. # shar:    Shell Archiver  (v1.22)
  33. #    Packed Sun Mar 17 18:30:16 PST 1991 by mtdiablo!rob
  34. #    from directory /usr/local/src/rebatch
  35. #
  36. #    Run the following text with /bin/sh to create:
  37. #      Makefile
  38. #      rebatch
  39. #      dorebatch.c
  40. #
  41. if test -f Makefile; then echo "File Makefile exists"; else
  42. echo "x - extracting Makefile (Text)"
  43. sed 's/^X//' << 'SHAR_EOF' > Makefile &&
  44. XNEWSOWNER=bin
  45. XNEWSGRP=bin
  46. XMODE=755
  47. XNEWSLIB=/usr/local/lib
  48. X
  49. Xall:    dorebatch
  50. X
  51. Xinstall:    dorebatch
  52. X    install -g ${NEWSGRP} -o ${NEWSOWNER} -m ${MODE} rebatch ${NEWSLIB}/newsbin/batch
  53. X    install -g ${NEWSGRP} -o ${NEWSOWNER} -m ${MODE} dorebatch ${NEWSLIB}/newsbin/batch
  54. SHAR_EOF
  55. chmod 0644 Makefile || echo "restore of Makefile fails"
  56. set `wc -c Makefile`;Sum=$1
  57. if test "$Sum" != "262"
  58. then echo original size 262, current size $Sum;fi
  59. fi
  60. if test -f rebatch; then echo "File rebatch exists"; else
  61. echo "x - extracting rebatch (Text)"
  62. sed 's/^X//' << 'SHAR_EOF' > rebatch &&
  63. X#! /bin/sh
  64. X# =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  65. X. ${NEWSCONFIG-/usr/local/lib/news/bin/config}
  66. Xumask $NEWSUMASK
  67. X
  68. XPATH=$NEWSCTL/bin:$NEWSBIN/batch:$NEWSBIN:$NEWSPATH ; export PATH
  69. Xfor file in  ${NEWSARTS}/in.coming/bad/*
  70. Xdo
  71. X    barename=`basename $file`
  72. X    newbatch=${NEWSARTS}/in.coming/$barename
  73. X    echo "rebatching $barename"
  74. X    dorebatch -i $file -o $newbatch && compress $newbatch && mv $newbatch.Z $newbatch && rm $file
  75. Xdone
  76. SHAR_EOF
  77. chmod 0644 rebatch || echo "restore of rebatch fails"
  78. set `wc -c rebatch`;Sum=$1
  79. if test "$Sum" != "437"
  80. then echo original size 437, current size $Sum;fi
  81. fi
  82. if test -f dorebatch.c; then echo "File dorebatch.c exists"; else
  83. echo "x - extracting dorebatch.c (Text)"
  84. sed 's/^X//' << 'SHAR_EOF' > dorebatch.c &&
  85. X#define ZCAT "zcat"
  86. X#define RNEWSSTRING "#! rnews"
  87. X
  88. X#include <stdio.h>
  89. X
  90. Xint verbose;
  91. Xextern int errno;
  92. Xextern int sys_nerr;
  93. Xextern char *sys_errlist[];
  94. X
  95. X#define ERRNO_MSG    (errno < sys_nerr ? sys_errlist[errno] : "unknown errno")
  96. X
  97. Xmain(argc, argv)
  98. Xint argc;
  99. Xchar **argv;
  100. X{
  101. X    char zfile[BUFSIZ], command[BUFSIZ], dataline[BUFSIZ];
  102. X    char *infile = NULL, *outfile = NULL, *arttempfile;
  103. X    FILE *infp, *outfp, *arttempfp;
  104. X    int eofreached, optret, rnewslen, charcnt, artcnt, argerr;
  105. X    extern int optind;
  106. X    extern char *optarg;
  107. X
  108. X    /* Process command line */
  109. X    argerr = 0;
  110. X    while ((optret = getopt(argc, argv, "vi:o:")) != -1) {
  111. X    switch (optret) {
  112. X    case 'v':
  113. X        verbose = 1;
  114. X        break;
  115. X    case 'i':
  116. X        infile = optarg;
  117. X        break;
  118. X
  119. X    case 'o':
  120. X        outfile = optarg;
  121. X        break;
  122. X
  123. X    default:
  124. X        argerr++;
  125. X    }
  126. X    }
  127. X
  128. X    if(!infile || !outfile || argerr) {
  129. X    fprintf(stderr, "%s: usage: %s -i [infile] -o [outfile]\n",
  130. X            argv[0], argv[0]);
  131. X    exit(-1);
  132. X    }
  133. X
  134. X    /* Open data stream of infile uncompressed */
  135. X    strcpy(zfile, infile);
  136. X    strcat(zfile, ".Z");
  137. X    if(link(infile, zfile) == 1) {
  138. X    fprintf(stderr, "%s: link(%s, %s): %s\n", argv[0],
  139. X        infile, zfile, ERRNO_MSG);
  140. X    exit(errno);
  141. X    }
  142. X    sprintf(command, "%s %s\n", ZCAT, zfile);
  143. X    if((infp = popen(command, "r")) == NULL) {
  144. X    fprintf(stderr, "%s: popen(%s, \"r\") failed\n", argv[0], command);
  145. X    unlink(zfile);
  146. X    exit(errno?errno:-1);
  147. X    }
  148. X
  149. X    /* Open output file */
  150. X    if((outfp = fopen(outfile, "w+")) == NULL) {
  151. X    fprintf(stderr, "%s: fopen(%s, \"w+\") failed\n", argv[0], outfile);
  152. X    pclose(infp);
  153. X    unlink(zfile);
  154. X    exit(errno?errno:-1);
  155. X    }
  156. X
  157. X    /* Get file name for temp file to hold each article */
  158. X    arttempfile = tmpnam(NULL);
  159. X
  160. X    /* Verify first line is rnews */
  161. X    if(fgets(dataline, BUFSIZ, infp) == NULL) {
  162. X    fprintf(stderr, "%s: premature end of file %s\n", argv[0], infile);
  163. X    pclose(infp);
  164. X    unlink(zfile);
  165. X    exit(errno?errno:-1);
  166. X    }
  167. X    rnewslen = strlen(RNEWSSTRING);
  168. X    if(strncmp(dataline, RNEWSSTRING, rnewslen)) {
  169. X    fprintf(stderr, "%s: file %s not a compressed news archive\n",
  170. X        argv[0], infile);
  171. X    pclose(infp);
  172. X    unlink(zfile);
  173. X    exit(errno?errno:-1);
  174. X    }
  175. X    eofreached = 0;
  176. X    artcnt = 0;
  177. X
  178. X    if(verbose)
  179. X    printf("input = %s output = %s\n", infile, outfile);
  180. X
  181. X    /* Process each article in batch */
  182. X    while(!eofreached) {
  183. X
  184. X    /* Open temp file for storing next article */
  185. X    if((arttempfp = fopen(arttempfile, "w+")) == NULL) {
  186. X        fprintf(stderr, "%s: fopen(%s, \"w+\") failed\n", argv[0],
  187. X            arttempfile);
  188. X        pclose(infp);
  189. X        unlink(zfile);
  190. X        exit(errno?errno:-1);
  191. X    }
  192. X
  193. X    /* Read a line from pipe. If oef or end of article,
  194. X     * write rnews line with count on output file,
  195. X     * copy article (in tempfile) to output file and quite loop.
  196. X     * Otherwise append this next line of article to end
  197. X     * of temp file and continue with next line from pipe.
  198. X     */
  199. X    while (1) {
  200. X        if(fgets(dataline, BUFSIZ, infp) == NULL)
  201. X        eofreached = 1;
  202. X
  203. X        if(eofreached||(!strncmp(dataline, RNEWSSTRING, rnewslen))) {
  204. X
  205. X        charcnt = ftell(arttempfp);
  206. X        rewind(arttempfp);
  207. X        fprintf(outfp, "%s %d\n", RNEWSSTRING, charcnt);
  208. X        if(verbose)
  209. X            printf("article %d charcnt %d\n", artcnt, charcnt);
  210. X
  211. X        while(fgets(dataline, BUFSIZ, arttempfp)) {
  212. X            if(fputs(dataline, outfp) == EOF) {
  213. X            fprintf(stderr, "%s: fputs to %s failed\n",
  214. X                argv[0], outfile);
  215. X            pclose(infp);
  216. X            unlink(zfile);
  217. X            fclose(arttempfp);
  218. X            unlink(arttempfile);
  219. X            fclose(outfp);
  220. X            exit(errno?errno:-1);
  221. X            }
  222. X            charcnt -= strlen(dataline);
  223. X        } 
  224. X        if(charcnt)
  225. X            fprintf(stderr, "%s: error in count by %d\n", argv[0],
  226. X                charcnt);
  227. X
  228. X        fclose(arttempfp);
  229. X        artcnt++;
  230. X        break;
  231. X
  232. X        } else 
  233. X        fputs(dataline, arttempfp);
  234. X    }
  235. X    }
  236. X
  237. X    pclose(infp);
  238. X    unlink(zfile);
  239. X    unlink(arttempfile);
  240. X    fclose(outfp);
  241. X    exit(0);
  242. X}
  243. SHAR_EOF
  244. chmod 0644 dorebatch.c || echo "restore of dorebatch.c fails"
  245. set `wc -c dorebatch.c`;Sum=$1
  246. if test "$Sum" != "3789"
  247. then echo original size 3789, current size $Sum;fi
  248. fi
  249. exit 0
  250. -- 
  251. Rob Bernardo                    Mt. Diablo Software Solutions
  252. email: rob@mtdiablo.Concord.CA.US        phone: (415) 827-4301
  253.