home *** CD-ROM | disk | FTP | other *** search
- /*
- * DOSCron.C &k2S&l8D
- */
- #include <StdLib.H>
- #include <StdIO.H>
- #include <ConIO.H>
- #include <Time.H>
- #include <DOS.H>
-
- #ifdef NO_BATCH
- #define cBanner "DOSCron\t\t\t\t<DIRECT MODE>\t\t\tVersion 1.1"
- #pragma comment ( exestr, "DOSCron Version 1.1 <DIRECT MODE>"\
- " compiled on " __DATE__ " at " __TIME__ )
- #else
- #define cBanner "BATCron\t\t\t\t<BATCH MODE> \t\t\tVersion 1.1"
- #pragma comment ( exestr, "BATCron Version 1.1 <BATCH MODE>"\
- " compiled on " __DATE__ " at " __TIME__ )
- #endif
-
-
- int Days[7];
- char *cDays[] = { "Sun",
- "Mon",
- "Tue",
- "Wed",
- "Thu",
- "Fri",
- "Sat"
- };
-
- int OutTime (int, int),
- GetKey (void);
-
- unsigned SetCursorType (unsigned);
-
-
- void CurrentStatus (void);
-
- /*
- * Changes the size of the current text cursor. It returns the size of the
- * cursor before it was changed.
- *
- * Unsigned x;
- * x = SetCursorType (0x2000) // Makes the cursor disappear
- * SetCursorType (x) // Restores the original cursor
- */
-
- unsigned
- SetCursorType (unsigned uiNewShape)
- {
- union REGS regs;
- unsigned uiOldShape;
- regs.h.ah = 0x03;
- regs.h.bh = 0x00;
- int86 (0x10, ®s, ®s);
- uiOldShape = regs.x.cx;
- regs.h.ah = 0x01;
- regs.x.cx = uiNewShape;
- int86 (0x10, ®s, ®s);
- return uiOldShape;
- }
-
- /*
- * Displays the current time on the screen overwriting the previous display
- */
- void
- CurrentStatus (void)
- {
- time_t ltime;
-
- time (<ime);
- printf ("\rCurrent time is %8.8s.", ctime (<ime) + 11);
-
- return;
- }
-
- /*
- * Returns 1 if the current hour is equal to (h), the current minute
- * is equal to (m) and the day of the week is equal to 1. If any of
- * these are false it returns 0
- */
- int
- OutTime (int h, int m)
- {
- struct tm *t;
- time_t tl;
-
- time (&tl);
- t = localtime (&tl);
- if (*(Days+t->tm_wday) == 0)
- {
- return 0;
- }
- return (h == t->tm_hour && m == t->tm_min);
- }
-
- /*
- * Returns 0 if no keystoke is in the keyboard buffer. Returns the ASCII
- * value (SCAN code) of a standard key or the EXTENDED value as a negative
- * if a function key or control key was pressed. Alt/Ctrl/Shift[Key] are
- * returned as their value times -1.
- */
- int
- GetKey (void)
- {
- int x;
- if (!kbhit ())
- {
- return 0;
- }
- return ((x = getch ()) == 0x00) ? getch () * -1 : x;
- }
-
- int
- main (int argc, char **argv)
- {
- int iHour, iMinute, iMinArgs, iStatus = 0;
- unsigned uiCursor, BLANK_CURSOR = 0x2000;
- register riCount;
- #ifdef NO_BATCH
- iMinArgs = 4;
- #else
- iMinArgs = 3;
- #endif
- if (argc < iMinArgs) /* if insufficient number of command line arguments */
- {
- printf ("%s\nSyntax: ");
- #ifdef NO_BATCH
- printf ("DOSCRON <Hour> <Minute> <File Name> "\
- "[Days]\n\tHour is in military time. ");
- #else
- printf ("BATCRON <Hour> <Minute> [Days]\n\tHour "\
- "is in military time. ");
- #endif
- printf ("Valid values are 0 to 23.\n\t" \
- "Valid values for Minute are 0 to 59\n\t");
- #ifdef NO_BATCH
- printf ("<File Name> is the file to execute.\n\t");
- #endif
- printf ("You may optionally pass a [Days] " \
- "parameter. If none is passed then\n\t"
- "\tDOSCron will function 7 days a week. If a value is passed\n\t"
- "\tthen DOSCron will only function on the days passed to it.\n\t"
- "\tThe following are valid [Days] codes:\n\t"
- "\t\tMonday = 1\tTuesday = 2\tWednesday = 3\n\t"
- "\t\tThursday = 4\tFriday = 5\tSaturday = 6\n\t"
- "\t\tSunday = 7\n\t");
- #ifdef NO_BATCH
- printf ("\tDOSCron 7 0 TEST 12345 causes " \
- "DOSCron to run TEST on weekdays\n\t");
- #else
- printf ("\tBATCron 7 0 12345 causes DOSCron " \
- "to function only on weekdays\n\t");
- #endif
- printf ("\t\tand not release on Saturday and " \
- "Sunday.\n\n\tPressing [Esc] will abort " \
- "DOSCron setting the DOS ERRORLEVEL to 1.\n\tNormal " \
- "termination sets the DOS ERRORLEVEL to 0", cBanner);
- return 1;
- }
- if (argc == iMinArgs) /* No [Days] parameter was passed */
- {
- for (riCount = 0; riCount < 7; riCount++)
- {
- *(Days+riCount) = 1; /* load all Days[] with 1s */
- }
- }
- else
- {
- for (riCount = 0; riCount < 7; riCount++)
- {
- *(Days+riCount) = 0; /* load all Days[] with 0s */
- }
- for (riCount = 0; argv[iMinArgs][riCount]; riCount++)
- { /* load selected Days[] with 1s */
- switch (argv[iMinArgs][riCount])
- {
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6': *(Days+(argv[iMinArgs][riCount]-'0')) = 1;
- break;
- case '7': *(Days) = 1;
- break;
- default : printf ("Invalid [Days] parameter " \
- "[%s] (%c)", *(argv+iMinArgs),
- argv[iMinArgs][riCount]);
- return 1;
- }
- }
- }
-
- iHour = atoi (*(argv + 1)); /* convert hour argument to an int */
- iMinute = atoi (*(argv + 2)); /* convert minute argument to an int */
-
- /*
- * Validate hour argument
- */
- if (iHour < 0 || iHour > 23 || (iHour == 0 && **(argv + 1) != '0'))
- {
- printf ("Invalid Hour ( %s )", *(argv + 1));
- return 1;
- }
- /*
- * Validate minute argument
- */
- if (iMinute < 0 || iMinute > 59 || (iMinute == 0 && **(argv + 2) != '0'))
- {
- printf ("Invalid Minute ( %s )", *(argv + 2));
- return 1;
- }
-
- uiCursor = SetCursorType (BLANK_CURSOR); /* hide the cursor */
- /*
- * display time and dates of execution
- */
- printf ("%s\nWaiting for %02d:%02d ( ", cBanner, iHour, iMinute);
- for (riCount = 0; riCount < 7; riCount++)
- { if (*(Days+riCount))
- {
- printf ("%s ", *(cDays+riCount)); /* show if selected */
- }
- }
- printf (")\n");
- /*
- * loop until the requested day and time is reached or [Esc] is pressed
- */
- do
- {
- CurrentStatus (); /* display current time */
- if (GetKey () == 0x1b) /* abort if [Esc] is pressed */
- {
- printf ("\n[Esc] pressed. DOSCron aborted.\n");
- iStatus = 1;
- }
- #ifdef NO_BATCH
- if (OutTime (iHour, iMinute))
- {
- SetCursorType (uiCursor);
- system (*(argv+3));
- SetCursorType (BLANK_CURSOR);
- while (OutTime (iHour, iMinute))
- {
- printf ("\rWaiting for %02d:%02d " \
- "to expire", iHour, iMinute);
- if (GetKey () == 0x1b) /* abort if [Esc] */
- {
- printf ("\n[Esc] pressed. DOSCron aborted.\n");
- iStatus = 1;
- break;
- }
- }
- printf ("\n");
- }
- } while (!iStatus);
- #else
- } while (!OutTime (iHour, iMinute) && !iStatus);
- #endif
-
- SetCursorType (uiCursor); /* resore the original cursor shape */
- return iStatus; /* set DOS ERRORLEVEL on exit */
- /* 0 = Normal */
- /* 1 = Aborted */
- }
-