home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dosono.zip / DOSONOS2.CPP next >
Text File  |  1993-05-08  |  6KB  |  209 lines

  1. // file tim_exec.cpp timed program execution with automatic
  2. // killing of program after a specified number of hours, minutes and seconds.
  3.  
  4. #define INCL_DOSPROCESS
  5. #define INCL_DOSSESMGR
  6. #define INCL_NOPMAPI
  7. #include <os2.h>
  8.  
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <conio.h>
  14.  
  15. void usage(char * p)
  16. {
  17.     printf(
  18.         "TIM_EXEC, DOS on OS/2 timed task run/terminate program, by William Herrera.\n"
  19.         "Version 1.01 of 5 May 1992. May be freely distributed and used.\n"
  20.         "Usage:\n"
  21.         "  %s progname -d[days] -h[hours] -m[minutes] -s[seconds] -a[Arguments]\n"
  22.         "\nExample:\n"
  23.         "  %s doit -h2 -m13 -s49 -a\"c: d:\"\n"
  24.         "    will run the MSDOS program doit with arguments c: d:\n"
  25.         "      for 2 hours, 13 minutes, 49 seconds.\n\n"
  26.         "Note: To execute an OS/2 file, use a program name of \"\"\n"
  27.         "  and put \"/c progname args\" for arguments, as in\n"
  28.         "    %s \"\" -m10 -a\"/c doit.cmd args\"\n"
  29.     , p, p, p);
  30. }
  31.  
  32. int main(int argc, char * argv[])
  33. {
  34.     long days = 0;
  35.     long hours = 0;
  36.     long minutes = 0;
  37.     long seconds = 0;
  38.     long duration = 0;
  39.     PID pidChild;
  40.     STARTDATA   StartData;    /* Start session data structure */
  41.     ULONG       SessID;       /* Session ID (returned) */
  42.     char       PgmTitle[200]; /* Program title string */
  43.     char       PgmName[128];  /* Program pathname string */
  44.     char       PgmArgs[128];
  45.     char       ObjBuf[100];  /* Object buffer */
  46.     APIRET rc;
  47.     static char * title = "Timed Execution of Process";
  48.  
  49.     int i = argc;
  50.     if(i < 2)
  51.     {
  52.         usage(argv[0]);
  53.         return 1;
  54.     }
  55.     strcpy(PgmName, argv[1]);
  56.  
  57.     for(i = 2; i < argc; ++i)
  58.     {
  59.         char buf[256];
  60.         char * p = buf;
  61.         strcpy(p, argv[i]);
  62.         if(*p == '-' || *p == '/')
  63.         {
  64.             ++p;
  65.             switch(toupper(*p++))
  66.             {
  67.             case '?':
  68.                 usage(argv[0]);
  69.                 return 0;
  70.             case 'A':
  71.                 strcpy(PgmArgs, p);
  72.                 break;
  73.             case 'H':
  74.                 hours = atoi(p);
  75.                 break;
  76.             case 'D':
  77.                 days = atoi(p);
  78.                 break;
  79.             case 'M':
  80.                 minutes = atoi(p);
  81.                 break;
  82.             case 'S':
  83.                 seconds = atoi(p);
  84.                 break;
  85.             default:
  86.                 usage(argv[0]);
  87.                 return 1;
  88.                 break;
  89.             }
  90.         }
  91.     }
  92.  
  93.     duration = days * 24;
  94.     duration += hours;     // now in hours
  95.     duration *= 60;
  96.     duration += minutes;  // now in minutes
  97.     duration *= 60;
  98.     duration += seconds;  // now in seconds
  99.     duration *= 1000;     // now in milliseconds
  100.  
  101.     StartData.Length = sizeof(STARTDATA);
  102.                            /* Length of STARTDATA structure */
  103.  
  104.     StartData.Related = SSF_RELATED_CHILD;
  105.                            /* Independent session */
  106.  
  107.     StartData.FgBg = SSF_FGBG_BACK;
  108.                            /* Start session in background */
  109.  
  110.     StartData.TraceOpt = SSF_TRACEOPT_NONE;
  111.                            /* Don't trace session */
  112.  
  113.     sprintf(PgmTitle, "%s -- %s", title, PgmName);
  114.     StartData.PgmTitle = PgmTitle;
  115.                            /* Session Title string */
  116.  
  117.     StartData.PgmName = PgmName;
  118.                            /* Program path-name string */
  119.  
  120.     StartData.PgmInputs = PgmArgs;
  121.                            /* Input arguments passed to the program */
  122.  
  123.     StartData.TermQ = 0;   /* Assume no termination queue */
  124.  
  125.     StartData.Environment = 0;
  126.                            /* Assume no environment string */
  127.  
  128.     StartData.InheritOpt = SSF_INHERTOPT_SHELL;
  129.                            /* Inherit environment and open */
  130.                            /*   file handles from shell   */
  131.  
  132.     StartData.SessionType = SSF_TYPE_DEFAULT;
  133.                            /* Allow the Shell to establish */
  134.                            /*   the session type           */
  135.  
  136.     StartData.IconFile = 0;
  137.                            /* Assume no specific icon file */
  138.                            /*   is provided                */
  139.  
  140.     StartData.PgmHandle = 0;
  141.                            /* Do not use the installation file */
  142.  
  143.     StartData.PgmControl = SSF_CONTROL_VISIBLE
  144.                              | SSF_CONTROL_MAXIMIZE;
  145.                            /* Start the program as visible */
  146.                            /*   and maximized              */
  147.  
  148.     StartData.InitXPos = 30;
  149.     StartData.InitYPos = 40;
  150.     StartData.InitXSize = 200;    /* Initial window coordinates */
  151.     StartData.InitYSize = 140;    /*   and size                 */
  152.  
  153.     StartData.Reserved = 0;
  154.                            /* Reserved, must be zero */
  155.  
  156.     StartData.ObjectBuffer = ObjBuf;
  157.                            /* Object buffer to hold DosExecPgm */
  158.                            /*   failure causes                 */
  159.  
  160.     StartData.ObjectBuffLen = 100;
  161.                            /* Size of object buffer */
  162.  
  163.     printf(
  164.             "\nBeginning run of %s.\n"
  165.             "Program to be run:  '%s'.\n"
  166.             "Argument list:      '%s'.\n"
  167.             "Allowable run time:\n"
  168.             "  %d days,\n"
  169.             "  %d hours,\n"
  170.             "  %d minutes, and\n"
  171.             "  %d seconds.\n"
  172.             "To see the program running, press Ctrl-Esc and choose the task entitled\n"
  173.             "  '%s'.\nTo leave the child process running indefinitely, press Esc key.\n"
  174.             , argv[0], argv[1], PgmArgs, days, hours, minutes, seconds, PgmTitle);
  175.  
  176.     rc = DosStartSession(&StartData, &SessID, &pidChild);
  177.                           /* On successful return, the variable  */
  178.                           /*   SessID contains the session ID    */
  179.                           /*   of the new session, and the       */
  180.                           /*   variable PID contains the process */
  181.                           /*   ID of the new process             */
  182.  
  183.     if (rc != 0)
  184.       {
  185.         printf("DosStartSession error: return code = %ld", rc);
  186.         return 3;
  187.       }
  188.  
  189.     long n = duration / 960;
  190.     while(n-- > 0)
  191.     {
  192.         DosSleep(959);
  193.         if(kbhit() && getch() == 27)
  194.         {
  195.             printf("\nSession '%s' will run indefinitely.\n"
  196.                 "  To terminate, press Ctrl-C.\n",
  197.                 PgmTitle);
  198.             while(1)
  199.                 DosSleep(10000);
  200.         }
  201.     }
  202.  
  203.     DosKillProcess(DKP_PROCESSTREE, pidChild);
  204.  
  205.     return 0;
  206. }
  207.  
  208.  
  209.