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

  1. /*
  2.  *
  3.  *    checkwork.c - look to see if there's any work
  4.  *              to do for a site.
  5.  *
  6.  *    R.J. Esposito
  7.  *    Bell of Penna.
  8.  *    June 1986
  9.  *
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <sys/utsname.h>
  16. #include <sys/dir.h>
  17. #include <ctype.h>
  18. #include <time.h>
  19. #include "nbatcher.h"
  20.  
  21. work_to_do ()
  22. {
  23.     register char    *p;
  24.     struct tm    *localtime(), *tp;
  25.     struct stat    st;
  26.     char    buf[BUFSIZ];
  27.     long    time(), clock;
  28.     int    hour;
  29.     short    num, upper, lower;
  30.  
  31.     sprintf (buf, "%s/%s", BATCHDIR, ep.site);
  32.  
  33.     if (stat(buf, &st) < 0)
  34.         xerror ("bad stat on %s\n", buf);
  35.  
  36.     /* if the size of the batch file is
  37.        zero, return FALSE
  38.     */
  39.  
  40.     if (st.st_size == 0)
  41.         return (FALSE);
  42.  
  43.     /* now see if it time to do anything */
  44.  
  45.     clock = time ((long *)0);
  46.     tp = localtime (&clock);
  47.     hour = tp->tm_hour;
  48.  
  49.     p = (char *) ep.hour;
  50.  
  51.     if (*p == '*')        /* match any hour */
  52.         return (check_uucp());
  53.  
  54.     if (strncmp(p, "off", 3) == 0)    /* just what it says, off */
  55.         return (FALSE);
  56.  
  57.     /* parse thru hour field to see if
  58.        this is the hour to do work */
  59.  
  60.     num = 0;
  61.     do {
  62.         num = num*10 + (*p - '0');
  63.     } while (isdigit(*++p));
  64.     if (num == hour)
  65.         return (check_uucp());
  66.  
  67.     if (*p == '-') {
  68.         lower = num;
  69.         p++;
  70.         num = 0;
  71.         do {
  72.             num = num*10 + (*p - '0');
  73.         } while (isdigit(*++p));
  74.         upper = num;
  75.  
  76.         if (lower < upper) {    /* normal hour range */
  77.             if (hour >= lower && hour <= upper)
  78.                 return (check_uucp());
  79.         } else if (lower > upper) {    /* 24 hr. cycle thru */
  80.             if (hour >= lower || hour <= upper)
  81.                 return (TRUE);
  82.         } else
  83.             return (FALSE);
  84.     }
  85.  
  86.     if (*p == ',') {
  87.         p++;
  88.         while (*p) {
  89.             num = 0;
  90.             do {
  91.                 num = num*10 + (*p - '0');
  92.             } while (isdigit(*++p));
  93.             if (num == hour)
  94.                 return (check_uucp());
  95.             p++;
  96.         }
  97.     }
  98.  
  99.     return (FALSE);
  100. }
  101.  
  102. /*    If check_uucp cannot find the remote site
  103.  *    directory, just bypass the byte counting
  104.  *    routine.  This is necessary because the
  105.  *    uucpcleanup daemon, on some sites, removes
  106.  *    the site directory when there's nothing there.
  107.  */
  108.  
  109. check_uucp()
  110. {
  111.     struct utsname    utsn;
  112.     struct direct    dp;
  113.     struct stat    st;
  114.     FILE    *dfp;
  115.     char    u_name[9], buf[80];
  116.     short    prefix_len;
  117.  
  118.     if (uname(&utsn) < 0)
  119.         xerror ("can't get local nodename\n");
  120.  
  121.     sprintf (buf, "%s/%s", UUCPDIR, ep.site);
  122.     if (chdir(buf) < 0) {
  123.        fprintf (stderr, "nbatcher: can't chdir to %s - bypassing UUCP check\n", buf);
  124.        return (TRUE);
  125.     }
  126.  
  127.     if ((dfp=fopen(".", "r")) == NULL) {
  128.        fprintf (stderr, "nbatcher: fopen error on %s - bypassing UUCP check\n", UUCPDIR);
  129.        return (TRUE);
  130.     }
  131.  
  132.     sprintf (buf, "D.%s", utsn.nodename);
  133.     prefix_len = (short) strlen(buf);
  134.     n_bytes = 0;
  135.     while ((fread((char *)&dp, sizeof(dp), 1, dfp)) == 1) {
  136.         if (dp.d_ino == 0 || dp.d_name[0] == '.')
  137.             continue;
  138.         if (strncmp(dp.d_name, buf, prefix_len))
  139.             continue;
  140.         if (stat(dp.d_name, &st) < 0) {
  141.           fprintf (stderr, "nbatcher: bad stat on UUCP_file %s - bypassing\n", dp.d_name);
  142.           continue;
  143.         }
  144.         n_bytes += st.st_size;
  145.         if (n_bytes > ep.m_bytes) {
  146.             fclose (dfp);
  147.             return (FALSE);
  148.         }
  149.     }
  150.     fclose (dfp);
  151.     if (chdir(LIBDIR) < 0)
  152.         xerror ("can't chdir back to %s\n", LIBDIR);
  153.  
  154.     return (TRUE);
  155. }
  156.