home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / newbatcha / nbatcher.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.7 KB  |  213 lines

  1. /*
  2.  *
  3.  *    nbatcher.c - where it really happens.
  4.  *
  5.  *    R.J. Esposito
  6.  *    Bell of Penna.
  7.  *    June 1986
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <time.h>
  15. #include "nbatcher.h"
  16.  
  17. batch_it ()
  18. {
  19.     struct stat    st;
  20.     FILE    *bfp, *afp;
  21.     char    fbuf[BUFSIZ], lckfile[40];
  22.     char    tbuf[80];
  23.     short    count;
  24.     int    c;
  25.  
  26.     if (chdir(BATCHDIR) < 0)
  27.         xerror ("can't chdir to %s\n", BATCHDIR);
  28.  
  29.     /* we create a lock file for two purposes,
  30.        first to make sure a previous nbatcher
  31.        didn't blowup and leave the lock file
  32.        laying around, and second to put the
  33.        remaining news article filenames when
  34.        we go over the max UUCP bytes and there's
  35.        still files remaining for batching.
  36.     */
  37.  
  38.     sprintf (lckfile, ".%s.lock", ep.site);
  39.     if (!access(lckfile, 0))
  40.         xerror ("lockfile already exists for %s\n", ep.site);
  41.  
  42.     if ((lfp=fopen(lckfile, "w")) == NULL)
  43.         xerror ("can't create lockfile for %s\n", ep.site);
  44.  
  45.     /* now that we've locked ourselves for this site,
  46.        lets carry on */
  47.  
  48.     if ((bfp=fopen(ep.site, "r")) == NULL)
  49.         xerror ("can't open %s/%s for reading\n", BATCHDIR, ep.site);
  50.  
  51.     if (tfile == NULL) {
  52.         tfile = mktemp("/tmp/bnewsXXXXXX");
  53.         if ((tfp=fopen(tfile, "w")) == NULL)
  54.             xerror ("can't open %s for writing\n", tfile);
  55.     }
  56.  
  57.     count = fcnt = scnt = 0;
  58.     cu_bytes = 0;
  59.     while ((fgets(fbuf, sizeof(fbuf), bfp)) != NULL) {
  60.         fbuf[strlen(fbuf)-1] = '\0';    /* remove the newline */
  61.         if ((afp=fopen(fbuf, "r")) == NULL) {
  62.            fprintf (stderr, "bypassing article %s: can't read it\n",
  63.                 fbuf);
  64.             continue;
  65.         }
  66.         if (fstat (fileno(afp), &st) < 0)
  67.              xerror ("fstat failed on %s\n", fbuf);
  68.         cu_bytes += st.st_size;
  69.  
  70.         /* if the max byte count is exceeded,
  71.            save the remaining files for later */
  72.  
  73.         if ((cu_bytes + n_bytes) > ep.m_bytes) {
  74.             fprintf (lfp, "%s\n", fbuf); /* put the '\n' back */
  75.             while ((fgets(fbuf, sizeof(fbuf), bfp)) != NULL)
  76.                 fputs (fbuf, lfp);
  77.             fclose (bfp);
  78.             fclose (lfp);
  79.             fclose (afp);
  80.             unlink (ep.site);
  81.             if (link(lckfile, ep.site) < 0)
  82.                xerror ("can't link lockfile to %s\n", ep.site);
  83.             unlink (lckfile);
  84.             chown (ep.site, NEWSUID, NEWSGID);
  85.             if (count)
  86.                 spoolit ();
  87.              if (cu_bytes - st.st_size)
  88.                  log_it (cu_bytes - st.st_size);
  89.             return;
  90.         }
  91.         sprintf (tbuf, "#! rnews %ld\n", st.st_size);
  92.         fputs (tbuf, tfp);
  93.         while ((c=getc(afp)) != EOF)
  94.             putc (c, tfp);
  95.         fclose (afp);
  96.  
  97.         if (++count == nfiles) {
  98.             spoolit ();
  99.             count = 0;
  100.         }
  101.         fcnt++;
  102.     }
  103.  
  104.     /* The final spool if less than nfiles
  105.        is encountered.  The zero out the
  106.        batchfile and unlink the lock file */
  107.  
  108.     spoolit ();
  109.     close (creat(ep.site, 0664));
  110.     chown (ep.site, NEWSUID, NEWSGID);
  111.     unlink (lckfile);
  112.  
  113.     /* here we log what we've done, and
  114.        if vflg is set, a copy to stdout
  115.        as well */
  116.  
  117.     log_it (0);
  118.     if (chdir(LIBDIR) < 0)
  119.         xerror ("can't chdir back to %s\n", LIBDIR);
  120.  
  121. }
  122.  
  123. spoolit ()
  124. {
  125.     struct stat    st;
  126.     char    cmd[BUFSIZ], cfile[80];
  127.     FILE    *pfp;
  128.     int    c;
  129.  
  130.     fclose (tfp);
  131.     stat (tfile, &st);
  132.  
  133.     /* if for some reason the temp file
  134.        is zero, just return */
  135.  
  136.     if (st.st_size == 0)
  137.         return;
  138.  
  139.     /* if ep.c_bits is set use COMPRESS to compress
  140.        the temp file first
  141.     */
  142.  
  143.     if (ep.c_bits) {
  144.         sprintf (cmd, "%s -b%d %s", COMPRESS, ep.c_bits, tfile);
  145.         if (system(cmd) != 0)
  146.             xerror ("system(%s) failed\n", cmd);
  147.  
  148.         strcpy (cfile, tfile);
  149.         strcat (cfile, ".Z");
  150.         if ((tfp=fopen(cfile, "r")) == NULL)
  151.             xerror ("can't open %s for reading\n", cfile);
  152.  
  153.         /* if ep.command has a specific command
  154.            for UUCP spooling, use it.  If not,
  155.            use UUX.
  156.         */
  157.  
  158.         if (ep.command[0] != '\0')
  159.             strcpy (cmd, ep.command);
  160.         else
  161.             sprintf (cmd, "%s %s!rnews", UUX, ep.site);
  162.  
  163.         /* now popen the command for writing
  164.            and send it the contents of tempfile */
  165.  
  166.         if ((pfp=popen(cmd, "w")) == NULL)
  167.             xerror ("popen failed on %s\n", cmd);
  168.  
  169.         /********************************************
  170.          * for version 2.10.3 and above,
  171.          * prepend `#! cunbatch'.
  172.          *
  173.          * NOTE: The remote site MUST be able to
  174.          *       except this format, or it will
  175.          *       be lost!!!
  176.          *******************************************/
  177.  
  178.         fputs ("#! cunbatch\n", pfp);
  179.         while ((c=getc(tfp)) !=  EOF)
  180.             putc (c, pfp);
  181.  
  182.         pclose (pfp);
  183.         fclose (tfp);
  184.         unlink (cfile);
  185.     } else {            /* regular batching here */
  186.         if ((tfp=fopen(tfile, "r")) == NULL)
  187.             xerror ("can't open %s for reading\n", tfile);
  188.  
  189.         /* if ep.command has a specific command
  190.            for UUCP spooling, use it.  If not,
  191.            use UUX.
  192.         */
  193.  
  194.         if (ep.command[0] != '\0')
  195.             strcpy (cmd, ep.command);
  196.         else
  197.             sprintf (cmd, "%s %s!rnews", UUX, ep.site);
  198.  
  199.         if ((pfp=popen(cmd, "w")) == NULL)
  200.             xerror ("popen failed on %s\n", cmd);
  201.  
  202.         while ((c=getc(tfp)) != EOF)
  203.             putc (c, pfp);
  204.  
  205.         pclose (pfp);
  206.         fclose (tfp);
  207.     }
  208.     if ((tfp=fopen(tfile, "w")) == NULL)
  209.         xerror ("can't re-open %s\n", tfile);
  210.  
  211.     scnt++;
  212. }
  213.