home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XT.ZIP / UTIL / UUPOLL.C < prev   
Encoding:
C/C++ Source or Header  |  1992-11-20  |  32.5 KB  |  901 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    Program:    uupoll.c              22 September 1989             */
  3. /*    Author:     Andrew H. Derbyshire                                */
  4. /*                108 Decatur St                                      */
  5. /*                Arlington, MA 02174                                 */
  6. /*    Internet:   help@kew.com                                        */
  7. /*    Function:   Performs autopoll functions for UUCICO              */
  8. /*    Language:   Borland C++ 2.0                                     */
  9. /*    Usage:      uupoll [-r 0] [-f hhmm] [-i hhmm|0400 ]             */
  10. /*                       [-d hhmm] [-e hhmm]                          */
  11. /*                       [-a hhmm] [-x debug] [-s systems]            */
  12. /*                                                                    */
  13. /*                Where:                                              */
  14. /*                                                                    */
  15. /*                -r 0     specifies that UUCICO is to run            */
  16. /*                         into passive mode when waiting to          */
  17. /*                         poll out                                   */
  18. /*                                                                    */
  19. /*                -r 1     specifies that UUCICO will not run         */
  20. /*                         in passive mode while waiting to           */
  21. /*                         poll out, but polling out will             */
  22. /*                         occur.                                     */
  23. /*                                                                    */
  24. /*                -f hhmm  is the first time in the day that          */
  25. /*                         UUCICO is to poll out.  If omitted,        */
  26. /*                         polling begins after the interval          */
  27. /*                         specified with -i.                         */
  28. /*                                                                    */
  29. /*                -i hhmm  the interval the UUCICO is to poll         */
  30. /*                         out at.  If omitted, a period of 4         */
  31. /*                         hours will be used.                        */
  32. /*                                                                    */
  33. /*                -d hhmm  Terminate polling after hhmm.              */
  34. /*                         Default is not to terminate.               */
  35. /*                                                                    */
  36. /*                -e hhmm  Terminate polling at hhmm                  */
  37. /*                         Default is not to terminate.               */
  38. /*                                                                    */
  39. /*                -a hhmm  Automatically poll actively using the      */
  40. /*                         system name "any" after any                */
  41. /*                         successful inbound poll if hhmm have       */
  42. /*                         past since last poll.  hhmm may be         */
  43. /*                         0000.                                      */
  44. /*                                                                    */
  45. /*                In addition, the following flags will be passed     */
  46. /*                to UUCICO:                                          */
  47. /*                                                                    */
  48. /*                -s system      system name to poll.  By default,    */
  49. /*                               UUCICO will be invoked with          */
  50. /*                               '-s all' followed by '-s any'.       */
  51. /*                                                                    */
  52. /*                -x n           debug level.   The default level     */
  53. /*                               is 1.                                */
  54. /*--------------------------------------------------------------------*/
  55.  
  56. /*--------------------------------------------------------------------*/
  57. /*                          RCS Information                           */
  58. /*--------------------------------------------------------------------*/
  59.  
  60. /*
  61.  *    $Id: UUPOLL.C 1.3 1992/11/20 12:41:01 ahd Exp $
  62.  *
  63.  *    $Log: UUPOLL.C $
  64.  * Revision 1.3  1992/11/20  12:41:01  ahd
  65.  * Fix TZ change bug
  66.  *
  67.  * Revision 1.2  1992/11/15  04:45:46  ahd
  68.  * Correct polling for days time zone changes
  69.  *
  70.  * Revision 1.1  1992/11/15  04:29:22  ahd
  71.  * Initial revision
  72.  *
  73.  * Revision 1.1  1992/04/27  00:30:13  ahd
  74.  * Initial revision
  75.  *
  76.  */
  77.  
  78. static const char rscid[] =
  79.          "$Id: UUPOLL.C 1.3 1992/11/20 12:41:01 ahd Exp $";
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*                        System include file                         */
  83. /*--------------------------------------------------------------------*/
  84.  
  85. #include <ctype.h>
  86. #include <dos.h>
  87. #include <limits.h>
  88. #include <process.h>
  89. #include <stdio.h>
  90. #include <stdlib.h>
  91. #include <string.h>
  92. #include <time.h>
  93. #include <conio.h>
  94. #include <direct.h>
  95. #include <signal.h>           /* Ctrl-Break handler               */
  96.  
  97. #ifdef __TURBOC__
  98. #include <alloc.h>
  99. unsigned _stklen = 2048;            /* Reduce memory usage           */
  100. unsigned _heaplen = 2048;           /* Reduce memory usage           */
  101. #else
  102. #ifndef FAMILYAPI
  103. static int setcbrk(char state);
  104. #endif
  105. #endif
  106.  
  107. /*--------------------------------------------------------------------*/
  108. /*                    UUPC/extended include files                     */
  109. /*--------------------------------------------------------------------*/
  110.  
  111. #include "getopt.h"
  112. #include "lib.h"
  113. #include "timestmp.h"
  114. #include "ssleep.h"
  115. #include "arpadate.h"
  116. #include "safeio.h"
  117. #include "dater.h"
  118.  
  119. /*--------------------------------------------------------------------*/
  120. /*                        Typedefs and macros                         */
  121. /*--------------------------------------------------------------------*/
  122.  
  123. typedef int hhmm;
  124.  
  125. /*--------------------------------------------------------------------*/
  126. /*                  Prototypes and global variables                   */
  127. /*--------------------------------------------------------------------*/
  128.  
  129. void   Catcher( void );
  130.  static active(char *Rmtname, int debuglevel, const char *logname);
  131. static void    busywork( time_t next);
  132. static int     execute( char *command );
  133. static time_t  nextpoll( hhmm first, hhmm interval );
  134. static boolean     notanumber( char *number);
  135. static void    usage( char *name );
  136.  
  137.  static int passive( time_t next,
  138.                      int debuglevel,
  139.                      const char *logname,
  140.                      const char *modem );
  141.  
  142. static hhmm    firstpoll(hhmm interval);
  143. static void    uuxqt( int debuglevel);
  144. static time_t LifeSpan( time_t duration, time_t stoptime );
  145.  
  146. static time_t now;            /* Current time, updated at start of
  147.                                  program and by busywork() and
  148.                                  execute()                           */
  149.  
  150. currentfile();
  151.  
  152. /*--------------------------------------------------------------------*/
  153. /*    m a i n                                                         */
  154. /*                                                                    */
  155. /*    main program                                                    */
  156. /*--------------------------------------------------------------------*/
  157.  
  158.  main( int argc , char *argv[] )
  159.  {
  160.  
  161.    int option;
  162.    hhmm first = - 1;
  163.    hhmm interval = -1;
  164.    hhmm stoptime = -1;
  165.    hhmm duration = -1;
  166.    hhmm autowait = -1;
  167.    hhmm cleanup  = -1;
  168.    time_t cleannext = LONG_MAX;
  169.  
  170.    time_t exittime;
  171.    int nopassive = 2;
  172.    boolean done = FALSE;
  173.    char *Rmtname = NULL;
  174.    char *CleanCommand = "uuclean";
  175.    char *logname = NULL;
  176.    char *modem   = NULL;
  177.    int returncode = 0;
  178.  
  179. #ifndef FAMILYAPI
  180.    boolean cbrk;
  181. #endif
  182.  
  183.    banner(argv);
  184.  
  185.    if (!configure( B_UUPOLL ))
  186.       panic();
  187.  
  188.    tzset();
  189.    time( &now );
  190.  
  191.  
  192.     if( signal( SIGINT, Catcher ) == SIG_ERR )
  193.     {
  194.         fprintf( stderr, "uupoll: Couldn't set SIGINT\n" );
  195.         panic();
  196.     }
  197.  
  198.    while((option = getopt(argc, argv, "m:a:c:C:d:e:f:l:i:s:r:x:")) != EOF)
  199.    switch(option)
  200.    {
  201.  
  202. /*--------------------------------------------------------------------*/
  203. /*       Automatically poll "any" after an incoming phone call        */
  204. /*--------------------------------------------------------------------*/
  205.  
  206.       case 'a':
  207.          if (notanumber(optarg))
  208.             usage( argv[0] );
  209.          autowait = (hhmm) hhmm2sec( atoi(optarg) );
  210.          break;
  211.  
  212. /*--------------------------------------------------------------------*/
  213. /*                         First time to poll                         */
  214. /*--------------------------------------------------------------------*/
  215.  
  216.       case 'f':
  217.          if (notanumber(optarg))
  218.             usage( argv[0] );
  219.          first = atoi(optarg);
  220.          if ( interval == -1 )
  221.             interval = 400;
  222.          break;
  223.  
  224. /*--------------------------------------------------------------------*/
  225. /*                         First time to clean                        */
  226. /*--------------------------------------------------------------------*/
  227.  
  228.       case 'c':
  229.          if (notanumber(optarg))
  230.             usage( argv[0] );
  231.          cleanup = atoi(optarg);
  232.          break;
  233.  
  234. /*--------------------------------------------------------------------*/
  235. /*                    Clean-up command to execute                     */
  236. /*--------------------------------------------------------------------*/
  237.  
  238.       case 'C':
  239.          CleanCommand = optarg;
  240.          break;
  241.  
  242. /*--------------------------------------------------------------------*/
  243. /*                          UUCICO log file                           */
  244. /*--------------------------------------------------------------------*/
  245.  
  246.       case 'l':
  247.          logname = optarg;
  248.          break;
  249.  
  250.  
  251. /*--------------------------------------------------------------------*/
  252. /*                            Input modem                             */
  253. /*--------------------------------------------------------------------*/
  254.  
  255.       case 'm':
  256.          modem = optarg;
  257.          break;
  258.  
  259. /*--------------------------------------------------------------------*/
  260. /*                          Interval to poll                          */
  261. /*--------------------------------------------------------------------*/
  262.  
  263.       case 'i':
  264.          if (notanumber(optarg))
  265.             usage( argv[0] );
  266.          interval = atoi(optarg);
  267.          nopassive = min(1,nopassive);
  268.          break;
  269.  
  270. /*--------------------------------------------------------------------*/
  271. /*                      Duration to poll                              */
  272. /*--------------------------------------------------------------------*/
  273.  
  274.       case 'e':
  275.          if (notanumber(optarg))
  276.             usage( argv[0] );
  277.          stoptime = atoi(optarg);
  278.          break;
  279.  
  280. /*--------------------------------------------------------------------*/
  281. /*                      Time to exit                                  */
  282. /*--------------------------------------------------------------------*/
  283.  
  284.       case 'd':
  285.          if (notanumber(optarg))
  286.             usage( argv[0] );
  287.          duration = atoi(optarg);
  288.          break;
  289.  
  290.  
  291. /*--------------------------------------------------------------------*/
  292. /*                        System name to poll                         */
  293. /*--------------------------------------------------------------------*/
  294.  
  295.       case 's':
  296.          Rmtname = strdup(optarg);
  297.          break;
  298.  
  299. /*--------------------------------------------------------------------*/
  300. /*                            Debug level                             */
  301. /*--------------------------------------------------------------------*/
  302.  
  303.       case 'x':
  304.          if (notanumber(optarg))
  305.             usage( argv[0] );
  306.          debuglevel = atoi(optarg);
  307.          break;
  308.  
  309. /*--------------------------------------------------------------------*/
  310. /*                       Passive polling option                       */
  311. /*--------------------------------------------------------------------*/
  312.  
  313.       case 'r':
  314.          if (notanumber(optarg))
  315.             usage( argv[0] );
  316.          nopassive = atoi(optarg);
  317.          break;
  318.  
  319. /*--------------------------------------------------------------------*/
  320. /*                                Help                                */
  321. /*--------------------------------------------------------------------*/
  322.  
  323.       default:
  324.       case '?':
  325.          usage( argv[0] );
  326.    } /* switch */
  327.  
  328. /*--------------------------------------------------------------------*/
  329. /*             Terminate with error if too many arguments             */
  330. /*--------------------------------------------------------------------*/
  331.  
  332.    if (optind != argc)
  333.    {
  334.       puts("Extra parameters on command line.");
  335.       usage( argv[0] );
  336.    }
  337.  
  338. /*--------------------------------------------------------------------*/
  339. /* Terminate if neither active polling nor passive polling requested  */
  340. /*--------------------------------------------------------------------*/
  341.  
  342.    if ( nopassive == 2 && (first < 0))
  343.    {
  344.       puts("Must specify -r 0, -f hhmm, or -i hhmm");
  345.       usage( argv[0] );
  346.    }
  347.  
  348. /*--------------------------------------------------------------------*/
  349. /*                        Compute time to exit                        */
  350. /*--------------------------------------------------------------------*/
  351.  
  352.    exittime = LifeSpan( duration, stoptime );
  353.  
  354. /*--------------------------------------------------------------------*/
  355. /*                   Comput first time to poll out                    */
  356. /*--------------------------------------------------------------------*/
  357.  
  358.    if ( (interval > 0) && (first < 0))
  359.       first = firstpoll(interval);
  360.  
  361. /*--------------------------------------------------------------------*/
  362. /*            If running under MS-DOS, enable Cntrl-Break.            */
  363. /*--------------------------------------------------------------------*/
  364.  
  365. #ifndef FAMILYAPI
  366. #ifdef __TURBOC__
  367.  
  368.    cbrk = getcbrk();                /* Get original Cntrl-Break setting */
  369.  
  370.    if (!cbrk)
  371.       setcbrk(1);                   /* Turn it on to allow abort        */
  372.  
  373. #else /*dmw*/
  374.  
  375.    cbrk = setcbrk(1);      /* Turn it on to allow abort; get previous state */
  376.    printf("BREAK ON has been set\n");
  377.  
  378. #endif
  379. #endif
  380.  
  381. /*--------------------------------------------------------------------*/
  382. /*          Determine first time to perform clean-up, if any          */
  383. /*--------------------------------------------------------------------*/
  384.  
  385.       if (cleanup >= 0)
  386.       {
  387.          cleannext = nextpoll(cleanup, 2400);
  388.          printf("Automatic cleanup via \"%s\" scheduled for %s",
  389.                   CleanCommand, ctime(&cleannext));
  390.       }
  391.  
  392. /*--------------------------------------------------------------------*/
  393. /*                       Beginning of main loop                       */
  394. /*--------------------------------------------------------------------*/
  395.  
  396.    while ( !done && (exittime > now))
  397.    {
  398.       time_t next = LONG_MAX;
  399.       time_t autonext  = now + autowait;
  400.       time_t wait = 10;      /* Time to wait after first panic()    */
  401.       int returncode = 0;
  402.  
  403. /*--------------------------------------------------------------------*/
  404. /*        Determine length of passive poll or wasting of time         */
  405. /*--------------------------------------------------------------------*/
  406.  
  407.       if (first >= 0)
  408.          next = nextpoll(first,interval);
  409.       else
  410.          next = exittime;
  411.  
  412. /*--------------------------------------------------------------------*/
  413. /*              Disable OS/2 undelete support if desired              */
  414. /*--------------------------------------------------------------------*/
  415.  
  416.    if ( !bflag[ F_UNDELETE ] )
  417.       putenv( "DELDIR=");
  418.  
  419. /*--------------------------------------------------------------------*/
  420. /*    Begin passive polling (with optional active calls system        */
  421. /*    "any") until next active poll or exit time is reached.          */
  422. /*--------------------------------------------------------------------*/
  423.  
  424.       while ((now < next) && ! done )
  425.       {
  426.  
  427.          if ( cleannext < now )
  428.          {
  429.             printf("Performing auto-clean with command: %s",
  430.                      CleanCommand );
  431.             if (system( CleanCommand ))
  432.                printerr( CleanCommand );
  433.             cleannext = nextpoll(cleanup,  2400);
  434.          }
  435.  
  436.          if (nopassive)
  437.             busywork(next < cleannext ? next : cleannext);
  438.          else {
  439.             time_t spin;
  440.             returncode = passive(next < cleannext ? next : cleannext ,
  441.                                  debuglevel, logname , modem );
  442.  
  443.             if (returncode == 69 )  /* Error in UUCICO?              */
  444.             {                       /* Yes --> Allow time to fix it  */
  445.                spin = now + wait;   /* Figure next wait              */
  446.                wait *= 2 ;          /* Double wait for next time     */
  447.                busywork( spin > next ? next : spin );
  448.                                     /* But only wait till next poll  */
  449.             } /* if (returncode == 69 ) */
  450.             else {
  451.                wait = 10;
  452.  
  453.                if ((returncode == 0) && (autowait != -1) &&
  454.                    (now > autonext) && (now < next))
  455.                {
  456.                   returncode = active("any",debuglevel, logname);
  457.                   autonext = now + autowait;
  458.                } /* if */
  459.  
  460.             } /* else */
  461.  
  462.             if ( (now > exittime) && (now < next))
  463.                done = TRUE;
  464.             else if ( returncode == 100 )
  465.                done = TRUE;
  466.  
  467.          } /* else */
  468.  
  469.       } /* while */
  470.  
  471. /*--------------------------------------------------------------------*/
  472. /*                      Actively poll if needed                       */
  473. /*--------------------------------------------------------------------*/
  474.  
  475.       if ( ! done && (first >= 0) )
  476.       {
  477.          returncode = active(Rmtname,debuglevel,logname);
  478.  
  479.          if ( returncode == 100 )
  480.             done = TRUE;
  481.  
  482.       } /* if ( ! done && (first >= 0) ) */
  483.  
  484.    } /* while */
  485.  
  486. /*--------------------------------------------------------------------*/
  487. /*                          End of main loop                          */
  488. /*--------------------------------------------------------------------*/
  489.  
  490.    uuxqt( debuglevel );          /* One last call to UUXQT                 */
  491.  
  492. #ifndef FAMILYAPI
  493.    if (!cbrk)
  494.       setcbrk(0);                /* Restore original Cntrl-Break setting   */
  495. #endif
  496.  
  497.    return(returncode);
  498.  } /* main */
  499.  
  500. /*--------------------------------------------------------------------*/
  501. /*    L i f e  S p a n                                                */
  502. /*                                                                    */
  503. /*    Compute time to run program                                     */
  504. /*--------------------------------------------------------------------*/
  505.  
  506. static time_t LifeSpan( time_t duration, time_t stoptime )
  507. {
  508.    time_t exittime = LONG_MAX;
  509.    struct tm  *time_record;
  510.    time_t quit;
  511.  
  512. /*--------------------------------------------------------------------*/
  513. /*             Compute relative quitting time, if desired             */
  514. /*--------------------------------------------------------------------*/
  515.  
  516.    if (duration != -1)
  517.       exittime = hhmm2sec( duration ) + (now / 60L) * 60L;
  518.  
  519. /*--------------------------------------------------------------------*/
  520. /*    Compute absolute quitting time, if desired.  Must terminate     */
  521. /*    before midnight unless relative time was given as well, in      */
  522. /*    which case it must terminate before relative time would         */
  523. /*    expire.                                                         */
  524. /*--------------------------------------------------------------------*/
  525.  
  526.    if (stoptime != -1)
  527.    {
  528.  
  529. /*--------------------------------------------------------------------*/
  530. /*               Compute the absolute termination time                */
  531. /*--------------------------------------------------------------------*/
  532.  
  533.       time_record = localtime(&now);
  534.       time_record->tm_sec = 0;
  535.       time_record->tm_min = (int) (stoptime % 100);
  536.       time_record->tm_hour= (int) (stoptime / 100);
  537.       quit = mktime(time_record);
  538.  
  539.       if ( quit < now )             /* Number should be in future    */
  540.          quit += hhmm2sec( 2400 );
  541.  
  542. /*--------------------------------------------------------------------*/
  543. /*              Compute default relative time, if needed              */
  544. /*--------------------------------------------------------------------*/
  545.  
  546.       if ( duration == -1 )
  547.       {
  548.          time_record->tm_min = 0;
  549.          time_record->tm_hour= 24;
  550.          exittime = mktime(time_record);
  551.       }
  552.  
  553. /*--------------------------------------------------------------------*/
  554. /*                  Determine if we should use time                   */
  555. /*--------------------------------------------------------------------*/
  556.  
  557.       if ( exittime < quit )
  558.       {
  559.  
  560.          printf("*** Exiting ***\tProgram was to execute until %s",
  561.                      ctime(&quit));
  562.  
  563.          printf("\t\twhich would exceed retirement at %s",
  564.                      ctime(&exittime));
  565.          exit(10);
  566.  
  567.       } /* if ( exittime < now ) */
  568.       else
  569.          exittime = quit;
  570.  
  571.    } /* else if (stoptime != -1) */
  572.  
  573.    if (exittime != LONG_MAX)
  574.       printf("Will terminate upon completion of first event after %s",
  575.               ctime(&exittime));
  576.  
  577.    return exittime;
  578.  
  579. } /* LifeSpan */
  580.  
  581. /*--------------------------------------------------------------------*/
  582. /*    a c t i v e                                                     */
  583. /*                                                                    */
  584. /*    Perform an active (outgoing) poll of other hosts                */
  585. /*--------------------------------------------------------------------*/
  586.  
  587.  static active(char *Rmtname, int debuglevel, const char *logname)
  588.  {
  589.    int result;
  590.  
  591.    if (Rmtname == NULL)             /* Default?                         */
  592.    {                                /* Yes --> do -s all and -s any     */
  593.       if (active("all",debuglevel, logname ) < 100)
  594.          return active("any",debuglevel, logname);
  595.       else
  596.          return 100;
  597.    }
  598.    else {
  599.       char buf[128];
  600.       sprintf(buf,"uucico -r 1 -s %s -x %d",Rmtname,debuglevel);
  601.  
  602.       if ( logname != NULL )
  603.          strcat( strcat( buf, " -l ") , logname );
  604.       result = execute(buf);
  605.       if ( result == 0 )
  606.          uuxqt( debuglevel );
  607.  
  608.       return result;
  609.    }
  610.  } /* active */
  611.  
  612. /*--------------------------------------------------------------------*/
  613. /*    b u s y w o r k                                                 */
  614. /*                                                                    */
  615. /*    Waits for next time to poll without answering the telephone.    */
  616. /*    Maybe we should at least beep on the hour?  :-)                 */
  617. /*--------------------------------------------------------------------*/
  618.  
  619. static void busywork( time_t next)
  620. {
  621.    time_t naptime;
  622.    time_t hours, minutes, seconds;
  623.  
  624.    naptime = next - now;
  625.  
  626.    hours   = (naptime / 3600) % 24;    /* Get pretty time to display... */
  627.    minutes = (naptime / 60) % 60;
  628.    seconds = naptime % 60;
  629.  
  630.    printf("Going to sleep for %02ld:%02ld:%02ld, next poll is %s",
  631.              hours, minutes, seconds, ctime(&next) );
  632.    ssleep( naptime );
  633.  
  634.    time( & now );
  635. }
  636.  
  637.  
  638. /*--------------------------------------------------------------------*/
  639. /*    e x e c u t e                                                   */
  640. /*                                                                    */
  641. /*    Executes a command via a spawn() system call.  This avoids      */
  642. /*    the storage overhead of COMMAND.COM and returns the actual      */
  643. /*    return code from the command executed.                          */
  644. /*                                                                    */
  645. /*    Note that this does not allow quoted command arguments, which   */
  646. /*    not a problem for the intended argv[0]s of UUCICO.              */
  647. /*--------------------------------------------------------------------*/
  648.  
  649.  static int execute( char *command )
  650.  {
  651.    char *argv[20];
  652.    int argc = 0;
  653.    int result;
  654. #ifdef DEBUG
  655.    FILE *stream = NULL;
  656. #endif
  657.  
  658.    printf("Executing command: %s\n",command);
  659. #ifdef DEBUG                  /* ahd */
  660.    stream = fopen("UUPOLL.LOG","a");
  661.    if (stream == NULL)
  662.    {
  663.       printerr("UUPOLL.LOG");
  664.       panic();
  665.    } /* if */
  666.    fprintf(stream, "%s: %s\n",arpadate(), command);
  667.    fclose(stream);
  668. #endif /* DEBUG */
  669.  
  670.    argv[argc] = strtok(command," \t");
  671.  
  672.    while ( argv[argc++] != NULL )
  673.       argv[argc] = strtok( NULL," \t");
  674.  
  675.    result = spawnvp(P_WAIT , argv[0] , argv );
  676.  
  677.    if ( result < 0 )
  678.    {
  679.       printerr( argv[0] );
  680.       printf("\a\nCommand \"%s\" failed completely.\n\a", argv[0]);
  681.       panic();
  682.    }
  683.  
  684.    time( & now );
  685.  
  686.    return result;
  687. }
  688.  
  689. /*--------------------------------------------------------------------*/
  690. /*    n e x t p o l l                                                 */
  691. /*                                                                    */
  692. /*    Returns next time to poll in seconds                            */
  693. /*                                                                    */
  694. /*    modified 14 October 1990 By Ed Keith.                           */
  695. /*    modified 4 November 1990 by Drew Derbyshire.                    */
  696. /*--------------------------------------------------------------------*/
  697.  
  698. static time_t nextpoll( hhmm first, hhmm interval )
  699. {
  700.    time_t sfirst;
  701.    time_t sinterval = hhmm2sec( interval );
  702.    time_t today;
  703.    time_t tomorrow;
  704.    struct tm  *time_record;   /* Ed K. 10/14/1990 */
  705.  
  706.    time_record = localtime(&now); /* Ed K. 10/14/1990 */
  707.    time_record->tm_sec = 0;   /* Ed K. 10/14/1990 */
  708.    time_record->tm_min = 0;   /* Ed K. 10/14/1990 */
  709.    time_record->tm_hour= 0;   /* Ed K. 10/14/1990 */
  710.    today = mktime(time_record);
  711.  
  712. /*--------------------------------------------------------------------*/
  713. /*    We could just add hhmm2sec(2400) (24 hours) except this         */
  714. /*    doesn't work during days when we do a daylight savings          */
  715. /*    shift.  So we let the run time library compute midnight.        */
  716. /*--------------------------------------------------------------------*/
  717.  
  718.    time_record->tm_hour = 23;   /* Advance to midnight     */
  719.    time_record->tm_min  = 59;
  720.    tomorrow = mktime(time_record) + 60;   /* Add a minute to 23:59   */
  721.  
  722.    sfirst = today + hhmm2sec(first);
  723.  
  724.    while (sfirst < now)
  725.       sfirst += sinterval;
  726.  
  727. /*--------------------------------------------------------------------*/
  728. /*    Since we restart the polling of each day anew, reset the        */
  729. /*    next poll time based if it is after midnight.  Note the         */
  730. /*    funny double compare, which handles the stricter of the two     */
  731. /*    tests for "tomorrow".                                           */
  732. /*--------------------------------------------------------------------*/
  733.  
  734.    if ((sfirst > tomorrow) || (sfirst > (today + hhmm2sec(2400))))
  735.       sfirst = tomorrow + hhmm2sec(first);
  736.  
  737.    return sfirst;
  738.  
  739. } /* nextpoll */
  740.  
  741.  
  742. /*--------------------------------------------------------------------*/
  743. /*    f i r s t p o l l                                               */
  744. /*                                                                    */
  745. /*    Determine first time to poll if not specified                   */
  746. /*--------------------------------------------------------------------*/
  747.  
  748. static hhmm firstpoll(hhmm interval)
  749. {
  750.    struct tm  *time_record;
  751.    time_t sfirst;
  752.    hhmm first;
  753.  
  754.    time_record = localtime(&now);
  755.    sfirst = ((time_t) time_record->tm_hour * 3600L +
  756.              (time_t) time_record->tm_min * 60L);
  757.    sfirst  = sfirst % hhmm2sec(interval);
  758.    first = (hhmm) ((sfirst / 3600L) * 100L + (sfirst % 3600L) / 60L);
  759.  
  760.    printf("First polling time computed to be %-2.2d:%-2.2d\n",
  761.          first / 100, first % 100);
  762.  
  763.    return first;
  764.  
  765. } /* firstpoll */
  766.  
  767. /*--------------------------------------------------------------------*/
  768. /*    n o t a n u m b e r                                             */
  769. /*                                                                    */
  770. /*    Examines string, returns true if non-numeric                    */
  771. /*--------------------------------------------------------------------*/
  772.  
  773.  static boolean notanumber( char *start)
  774.  {
  775.    char *number = start;
  776.    while (*number != '\0')
  777.    {
  778.       if (!isdigit(*number))
  779.       {
  780.          printf("Parameter must be numeric, was %s\n",start);
  781.          return TRUE;
  782.       }
  783.       number++;
  784.    }
  785.    return FALSE;
  786.  } /* notanumber */
  787.  
  788.  
  789. /*--------------------------------------------------------------------*/
  790. /*    p a s s i v e                                                   */
  791. /*                                                                    */
  792. /*    Invoke UUCICO in passive mode until next active poll (if any).  */
  793. /*--------------------------------------------------------------------*/
  794.  
  795.  static int passive( time_t next,
  796.                      int debuglevel,
  797.                      const char *logname,
  798.                      const char *modem )
  799.  {
  800.    char buf[128];             /* Buffer for execute() commands          */
  801.    time_t seconds = (next - now + 59);
  802.    time_t minutes;
  803.    int result;
  804.  
  805.    if ( seconds > INT_MAX)
  806.       seconds = (INT_MAX / 3600) * 3600;
  807.  
  808.    minutes = seconds / 60;
  809.  
  810.    sprintf(buf,"uucico -r 0 -x %d -d %02ld%02ld",
  811.                debuglevel,
  812.                minutes / 60, minutes % 60);
  813.  
  814.    if ( logname != NULL )
  815.       strcat( strcat( buf, " -l ") , logname );
  816.  
  817.    if ( modem != NULL )
  818.       strcat( strcat( buf, " -m ") , modem );
  819.  
  820.    result = execute(buf);
  821.    if ( result == 0 )
  822.       uuxqt( debuglevel );
  823.  
  824.    return result;
  825.  
  826.  } /* passive */
  827.  
  828. /*--------------------------------------------------------------------*/
  829. /*    u u x q t                                                       */
  830. /*                                                                    */
  831. /*    Execute the UUXQT program to run files received by UUCICO       */
  832. /*--------------------------------------------------------------------*/
  833.  
  834.  static void uuxqt( int debuglevel)
  835.  {
  836.    int result;
  837.    char buf[128];             /* Buffer for execute() commands          */
  838.  
  839.    sprintf(buf,"uuxqt -x %d", debuglevel);
  840.    result = execute(buf);
  841.  
  842.    if ( result != 0 )
  843.    {
  844.       printf("UUXQT failed with a return code of %d\n",result);
  845.       panic();
  846.    } /* if ( result != 0 ) */
  847.  
  848.  } /* uuxqt */
  849.  
  850. /*--------------------------------------------------------------------*/
  851. /*    C a t c h e r                                                   */
  852. /*                                                                    */
  853. /*    Catch Ctrl-Break                                                */
  854. /*--------------------------------------------------------------------*/
  855.  
  856.  void Catcher( void )
  857.  {
  858.  
  859.     safeout("uupoll: Program aborted by user\r\n");
  860.  
  861.     _exit(100);
  862.  } /* Catcher */
  863.  
  864. /*--------------------------------------------------------------------*/
  865. /*    u s a g e                                                       */
  866. /*                                                                    */
  867. /*    Report correct usage of the program and then exit.              */
  868. /*--------------------------------------------------------------------*/
  869.  
  870.  static void usage( char *name )
  871.  {
  872.    printf("Usage:\t%s"
  873.           "\t[-a hhmm] [-d hhmm | -e hhmm] [-f hhmm] [-i hhmm]\n"
  874.           "\t\t[-l logname] [-c hhmm] [-C command]\n"
  875.           "\t\t[-r 0 | 1] [-s system] [-x n]\n",name);
  876.    exit(4);
  877.  }
  878.  
  879. #ifndef FAMILYAPI
  880. #ifndef __TURBOC__
  881. /*--------------------------------------------------------------------*/
  882. /*    s e t c b r k                                                   */
  883. /*                                                                    */
  884. /*    Enable Cntrl-Break                                              */
  885. /*                                                                    */
  886. /*    Written by Dave Watts                                           */
  887. /*--------------------------------------------------------------------*/
  888.  
  889. static int setcbrk(char state)
  890. {
  891.    union REGS inregs, outregs;
  892.  
  893.    inregs.x.ax = 0x3302;
  894.    inregs.h.dl = state;
  895.    intdos(&inregs, &outregs);
  896.  
  897.    return outregs.h.dl;
  898. }
  899. #endif
  900. #endif
  901.