home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / xcron / xcron.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-03  |  8.8 KB  |  267 lines

  1. /*
  2.  * DOSCron.C &k2S&l8D
  3.  */
  4. #include <StdLib.H>
  5. #include <StdIO.H>
  6. #include <ConIO.H>
  7. #include <Time.H>
  8. #include <DOS.H>
  9.  
  10. #ifdef NO_BATCH
  11. #define cBanner       "DOSCron\t\t\t\t<DIRECT MODE>\t\t\tVersion 1.1"
  12. #pragma comment ( exestr, "DOSCron Version 1.1 <DIRECT MODE>"\
  13.                          " compiled on " __DATE__ " at " __TIME__ )
  14. #else
  15. #define cBanner       "BATCron\t\t\t\t<BATCH MODE> \t\t\tVersion 1.1"
  16. #pragma comment ( exestr, "BATCron Version 1.1 <BATCH MODE>"\
  17.                          " compiled on " __DATE__ " at " __TIME__ )
  18. #endif
  19.  
  20.  
  21. int             Days[7];
  22. char            *cDays[] = { "Sun",
  23.                              "Mon",
  24.                              "Tue",
  25.                              "Wed",
  26.                              "Thu",
  27.                              "Fri",
  28.                              "Sat"
  29.                 };
  30.  
  31. int             OutTime (int, int),
  32.                 GetKey (void);
  33.  
  34. unsigned        SetCursorType (unsigned);
  35.  
  36.  
  37. void            CurrentStatus (void);
  38.  
  39. /*
  40.  * Changes the size of the current text cursor.  It returns the size of the
  41.  * cursor before it was changed.
  42.  *
  43.  * Unsigned x;
  44.  * x = SetCursorType (0x2000)  // Makes the cursor disappear
  45.  * SetCursorType (x)           // Restores the original cursor
  46.  */
  47.  
  48. unsigned
  49. SetCursorType (unsigned uiNewShape)
  50. {
  51.     union REGS      regs;
  52.         unsigned        uiOldShape;
  53.     regs.h.ah = 0x03;
  54.     regs.h.bh = 0x00;
  55.     int86 (0x10, ®s, ®s);
  56.         uiOldShape = regs.x.cx;
  57.     regs.h.ah = 0x01;
  58.         regs.x.cx = uiNewShape;
  59.     int86 (0x10, ®s, ®s);
  60.         return uiOldShape;
  61. }
  62.  
  63. /*
  64.  * Displays the current time on the screen overwriting the previous display
  65.  */
  66. void
  67. CurrentStatus (void)
  68. {
  69.     time_t          ltime;
  70.  
  71.     time (<ime);
  72.     printf ("\rCurrent time is %8.8s.", ctime (<ime) + 11);
  73.  
  74.     return;
  75. }
  76.  
  77. /*
  78.  * Returns 1 if the current hour is equal to (h), the current minute
  79.  * is equal to (m) and the day of the week is equal to 1.  If any of
  80.  * these are false it returns 0
  81.  */
  82. int
  83. OutTime (int h, int m)
  84. {
  85.     struct tm      *t;
  86.     time_t          tl;
  87.  
  88.     time (&tl);
  89.     t = localtime (&tl);
  90.         if (*(Days+t->tm_wday) == 0)
  91.         {
  92.                 return 0;
  93.         }
  94.     return (h == t->tm_hour && m == t->tm_min);
  95. }
  96.  
  97. /*
  98.  * Returns 0 if no keystoke is in the keyboard buffer.  Returns the ASCII
  99.  * value (SCAN code) of a standard key or the EXTENDED value as a negative
  100.  * if a function key or control key was pressed. Alt/Ctrl/Shift[Key] are
  101.  * returned as their value times -1.
  102.  */
  103. int
  104. GetKey (void)
  105. {
  106.     int             x;
  107.     if (!kbhit ())
  108.     {
  109.         return 0;
  110.     }
  111.     return ((x = getch ()) == 0x00) ? getch () * -1 : x;
  112. }
  113.  
  114. int
  115. main (int argc, char **argv)
  116. {
  117.         int             iHour, iMinute, iMinArgs, iStatus = 0;
  118.         unsigned        uiCursor, BLANK_CURSOR = 0x2000;
  119.         register        riCount;
  120. #ifdef NO_BATCH
  121.         iMinArgs = 4;
  122. #else
  123.         iMinArgs = 3;
  124. #endif
  125.         if (argc < iMinArgs) /* if insufficient number of command line arguments */
  126.     {
  127.                 printf ("%s\nSyntax: ");
  128. #ifdef NO_BATCH
  129.                         printf ("DOSCRON <Hour> <Minute> <File Name> "\
  130.                                 "[Days]\n\tHour is in military time. ");
  131. #else
  132.                         printf ("BATCRON <Hour> <Minute> [Days]\n\tHour "\
  133.                                 "is in military time. ");
  134. #endif
  135.                         printf ("Valid values are 0 to 23.\n\t" \
  136.                                 "Valid values for Minute are 0 to 59\n\t");
  137. #ifdef NO_BATCH
  138.                         printf ("<File Name> is the file to execute.\n\t");
  139. #endif
  140.                         printf ("You may optionally pass a [Days] " \
  141.                                 "parameter.  If none is passed then\n\t"
  142.                         "\tDOSCron will function 7 days a week.  If a value is passed\n\t"
  143.                         "\tthen DOSCron will only function on the days passed to it.\n\t"
  144.                         "\tThe following are valid [Days] codes:\n\t"
  145.                         "\t\tMonday = 1\tTuesday = 2\tWednesday = 3\n\t"
  146.                         "\t\tThursday = 4\tFriday = 5\tSaturday = 6\n\t"
  147.                         "\t\tSunday = 7\n\t");
  148. #ifdef NO_BATCH
  149.                         printf ("\tDOSCron 7 0 TEST 12345 causes " \
  150.                                 "DOSCron to run TEST on weekdays\n\t");
  151. #else
  152.                         printf ("\tBATCron 7 0 12345 causes DOSCron " \
  153.                                 "to function only on weekdays\n\t");
  154. #endif
  155.                         printf ("\t\tand not release on Saturday and " \
  156.                                 "Sunday.\n\n\tPressing [Esc] will abort " \
  157.                         "DOSCron setting the DOS ERRORLEVEL to 1.\n\tNormal " \
  158.                         "termination sets the DOS ERRORLEVEL to 0", cBanner);
  159.         return 1;
  160.     }
  161.         if (argc == iMinArgs)   /* No [Days] parameter was passed */
  162.         {
  163.                 for (riCount = 0; riCount < 7; riCount++)
  164.                 {
  165.                         *(Days+riCount) = 1; /* load all Days[] with 1s */
  166.                 }
  167.         }
  168.         else
  169.         {
  170.                 for (riCount = 0; riCount < 7; riCount++)
  171.                 {
  172.                         *(Days+riCount) = 0; /* load all Days[] with 0s */
  173.                 }
  174.                 for (riCount = 0; argv[iMinArgs][riCount]; riCount++)
  175.                 {    /* load selected Days[] with 1s */
  176.                         switch (argv[iMinArgs][riCount])
  177.                         {
  178.                                 case '1':
  179.                                 case '2':
  180.                                 case '3':
  181.                                 case '4':
  182.                                 case '5':
  183.                                 case '6': *(Days+(argv[iMinArgs][riCount]-'0')) = 1;
  184.                                           break;
  185.                                 case '7': *(Days) = 1;
  186.                                           break;
  187.                                 default : printf ("Invalid [Days] parameter " \
  188.                                                   "[%s] (%c)", *(argv+iMinArgs),
  189.                                                   argv[iMinArgs][riCount]);
  190.                                           return 1;
  191.                         }
  192.                 }
  193.         }
  194.  
  195.         iHour = atoi (*(argv + 1));    /* convert hour argument to an int */
  196.         iMinute = atoi (*(argv + 2));  /* convert minute argument to an int */
  197.  
  198.         /*
  199.          * Validate hour argument
  200.          */
  201.     if (iHour < 0 || iHour > 23 || (iHour == 0 && **(argv + 1) != '0'))
  202.     {
  203.         printf ("Invalid Hour ( %s )", *(argv + 1));
  204.         return 1;
  205.     }
  206.         /*
  207.          * Validate minute argument
  208.          */
  209.     if (iMinute < 0 || iMinute > 59 || (iMinute == 0 && **(argv + 2) != '0'))
  210.     {
  211.         printf ("Invalid Minute ( %s )", *(argv + 2));
  212.         return 1;
  213.     }
  214.  
  215.         uiCursor = SetCursorType (BLANK_CURSOR); /* hide the cursor */
  216.         /*
  217.          * display time and dates of execution
  218.          */
  219.         printf ("%s\nWaiting for %02d:%02d ( ", cBanner, iHour, iMinute);
  220.         for (riCount = 0; riCount < 7; riCount++)
  221.         {       if (*(Days+riCount))
  222.                 {
  223.                         printf ("%s ", *(cDays+riCount)); /* show if selected */
  224.                 }
  225.         }
  226.         printf (")\n");
  227.         /*
  228.          * loop until the requested day and time is reached or [Esc] is pressed
  229.          */
  230.     do
  231.     {
  232.                 CurrentStatus ();      /* display current time */
  233.                 if (GetKey () == 0x1b) /* abort if [Esc] is pressed */
  234.         {
  235.                         printf ("\n[Esc] pressed. DOSCron aborted.\n");
  236.             iStatus = 1;
  237.         }
  238. #ifdef NO_BATCH
  239.                 if (OutTime (iHour, iMinute))
  240.                 {
  241.                         SetCursorType (uiCursor);
  242.                         system (*(argv+3));
  243.                         SetCursorType (BLANK_CURSOR);
  244.                         while (OutTime (iHour, iMinute))
  245.                         {
  246.                                 printf ("\rWaiting for %02d:%02d " \
  247.                                         "to expire", iHour, iMinute);
  248.                                 if (GetKey () == 0x1b) /* abort if [Esc] */
  249.                                 {
  250.                                         printf ("\n[Esc] pressed. DOSCron aborted.\n");
  251.                                         iStatus = 1;
  252.                                         break;
  253.                                 }
  254.                         }
  255.                         printf ("\n");
  256.                 }
  257.         } while (!iStatus);
  258. #else
  259.     } while (!OutTime (iHour, iMinute) && !iStatus);
  260. #endif
  261.  
  262.         SetCursorType (uiCursor);  /* resore the original cursor shape */
  263.         return iStatus;            /* set DOS ERRORLEVEL on exit */
  264.                                    /* 0 = Normal  */
  265.                                    /* 1 = Aborted */
  266. }
  267.