home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / prirty.zip / priority.c next >
C/C++ Source or Header  |  1994-09-11  |  7KB  |  178 lines

  1. /************************************************************************/
  2. /* Priority is a quick-and-dirty priority-launcher for OS/2 2.x.        */
  3. /* This code was written by M. Kimes and is hereby released to the      */
  4. /* public domain by the author -- no rights reserved.                   */
  5. /*                                                                      */
  6. /* C/Set2 1.0:      icc /Rn /O+ /Gs+ /Gm- /W3 /Kb /Sa priority.c        */
  7. /* C++ Tools 2.1:   icc /Rn /O+ /Ol+ /Gu+ /Gs+ /W3 /Kb /Sd+ /Gi+        */
  8. /*                      /Sa /Gm- priority.c                             */
  9. /*                                                                      */
  10. /* To get command line help, type "PRIORITY /?" at a command line.      */
  11. /*                                                                      */
  12. /* Undocumented "switches" are activated by adding a text character to  */
  13. /* the end of the first argument:  D = Debug mode, S = shutup           */
  14. /* Example:  "priority 1D 0 somefile.exe"                               */
  15. /*                                                                      */
  16. /************************************************************************/
  17.  
  18. #define INCL_DOS
  19. #define INCL_VIO
  20.  
  21. #include <os2.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26.  
  27. BOOL shutup = FALSE;  /* don't speak */
  28.  
  29.  
  30. void _System pprintf (const char *string,...) {
  31.  
  32.   /* use VioWrtTTY so we don't interfere with any redirection */
  33.  
  34.   static char     buffer[1024];
  35.   static va_list  ap;
  36.   static int      len;
  37.  
  38.   if(!shutup) {
  39.     va_start(ap,string);
  40.     len = vsprintf(buffer,string,ap);
  41.     va_end(ap);
  42.     VioWrtTTY(buffer,len,0);
  43.   }
  44. }
  45.  
  46. int main (int argc,char *argv[]) {
  47.  
  48.   static   TIB          *tib;
  49.   static   PIB          *pib;
  50.   static   SHORT         class,delta;
  51.   static   unsigned long apptype;
  52.   static   char         *leader = NULL,runme[1100],object[25],*p;
  53.   register char         *pp,*ppp;
  54.   register int           x;
  55.   static   RESULTCODES   rt;
  56.  
  57.   if(argc >= 3 && argv[1][strlen(argv[1]) - 1] == 'S')
  58.     shutup = TRUE;
  59.  
  60.   if(!shutup && !DosGetInfoBlocks(&tib,&pib)) {  /* get current priority */
  61.     class = (tib->tib_ptib2->tib2_ulpri & (~255)) >> 8;
  62.     /* note:  doesn't seem to return negative deltas (shows as 0) */
  63.     delta = tib->tib_ptib2->tib2_ulpri & 255;
  64.     pprintf("\r\nCurrent priority is %d:%d (%ld)",class,delta,
  65.             tib->tib_ptib2->tib2_ulpri);
  66.   }
  67.   if(argc >= 3) {  /* get priority arguments first */
  68.     class = atoi(argv[1]);
  69.     delta = atoi(argv[2]);
  70.     if(class < PRTYC_IDLETIME || class > PRTYC_FOREGROUNDSERVER) {
  71.       shutup = FALSE;
  72.       pprintf("\r\n**Error in priority class %d",class);
  73.     }
  74.     if(delta < PRTYD_MINIMUM || delta > PRTYD_MAXIMUM) {
  75.       shutup = FALSE;
  76.       pprintf("\r\n**Error in priority delta %d",delta);
  77.     }
  78.     DosSetPriority(PRTYS_PROCESS,class,delta,0L);
  79.     if(!shutup && !DosGetInfoBlocks(&tib,&pib)) {
  80.       class = (tib->tib_ptib2->tib2_ulpri & (~255)) >> 8;
  81.       delta = tib->tib_ptib2->tib2_ulpri & 255;
  82.       pprintf("\r\nPriority set to %d:%d (%ld)",class,delta,
  83.              tib->tib_ptib2->tib2_ulpri);
  84.     }
  85.     /* see if we need to run through command interpreter */
  86.     if(!argv[3] || !strchr(argv[3],'.') || DosQueryAppType(argv[3],&apptype) ||
  87.        (!(apptype & FAPPTYP_NOTWINDOWCOMPAT) &&
  88.        !(apptype & FAPPTYP_WINDOWCOMPAT) && !(apptype & FAPPTYP_BOUND) &&
  89.        apptype != FAPPTYP_32BIT)) {
  90.       if(DosScanEnv("COMSPEC",(char **)&leader) || !leader || !*leader)
  91.         leader = "CMD.EXE"; /* guess */
  92.     }
  93.     *runme = 0;
  94.     p = runme;
  95.     if(leader) {  /* prepend command interpreter we found or guessed at */
  96.       strcpy(p,leader);
  97.       p += strlen(p) + 1;
  98.       if(argv[3]) {
  99.         strcpy(p,"/C ");
  100.         p += strlen(p);
  101.       }
  102.       x = 3;
  103.     }
  104.     else {        /* else just the program itself */
  105.       strcpy(p,argv[3]);
  106.       p += strlen(p) + 1;
  107.       x = 4;
  108.     }
  109.     for(;x < argc;x++) {
  110.       /* see if we need to add any double-quotes to protect arguments
  111.          containing spaces.  also strip leading/trailing spaces.  this
  112.          code is intentionally sloppy; since it doesn't need to remember
  113.          the allocated variables to free them later, it doesn't bother. */
  114.       ppp = malloc(strlen(argv[x]) + 3);
  115.       if(ppp) {
  116.         ppp++;
  117.         strcpy(ppp,argv[x]);
  118.         while(*ppp && (*ppp == ' ' || *ppp == '\t')) /* strip leading spaces */
  119.           ppp++;
  120.         if(*ppp) {
  121.           pp = ppp + (strlen(ppp) - 1);   /* strip trailing spaces */
  122.           while(pp > ppp && (*pp == ' ' || *pp == '\t')) {
  123.             *pp = 0;
  124.             pp--;
  125.           }
  126.         }
  127.         if(strchr(ppp,' ')) {  /* need double quotes */
  128.           ppp--;
  129.           *ppp = '\"';
  130.           strcat(ppp,"\"");
  131.         }
  132.         argv[x] = ppp;
  133.       }
  134.       if(*argv[x]) {
  135.         strcpy(p,argv[x]);
  136.         p += strlen(p);
  137.         if(x < argc - 1) {  /* add space between arguments */
  138.           *p = ' ';
  139.           p++;
  140.         }
  141.       }
  142.     }
  143.     p = 0;
  144.     /* check for "debug" mode */
  145.     if(argv[1][strlen(argv[1]) - 1] == 'D') {
  146.       p = runme;
  147.       pprintf("\r\nCL: \"%s\"",p);
  148.       p += strlen(p) + 1;
  149.       if(*p)
  150.         pprintf(" \"%s\"",p);
  151.       *object = 0;
  152.       pprintf("\r\nDosExecPgm returned: %ld%s%s%s",
  153.              DosExecPgm(object,24L,EXEC_ASYNC,runme,NULL,&rt,runme),
  154.              (*object) ? " (" : "",(*object) ? object : "",
  155.              (*object) ? ")" : "");
  156.     }
  157.     else
  158.       DosExecPgm(object,24L,EXEC_ASYNC,runme,NULL,&rt,runme);
  159.   }
  160.   else {  /* usage */
  161.     shutup = FALSE;
  162.     pprintf("\r\n\r\n Priority is a public domain launch-with-priority program by M. Kimes\r\n"
  163.             " Compiled: %s  %s\r\n"
  164.             "\r\nUsage:  Priority [<class> <delta> [<command> [args...]]]\r\n"
  165.             "  <class> is from 1 to 4 (3 is a higher priority then 4; otherwise, the\r\n"
  166.             "          higher the number, the higher the priority).\r\n"
  167.             "  <delta> is from -31 to 31; the higher the number, the higher the\r\n"
  168.             "          priority within the <class>.\r\n\r\n"
  169.             "Example:  priority 1 15 someprog.exe some args \"arg with spaces\"\r\n\r\n"
  170.             " Note that 2:0 is the normal priority.  Be careful with priority class 3;\r\n"
  171.             " not even the OS/2 kernal can preempt a program running at 3:31.  Class 1\r\n"
  172.             " is useful for running programs during \"idle\" CPU cycles.  Class 4 can be\r\n"
  173.             " used sparingly to boost timing-critical programs.\r\n",__TIME__,__DATE__);
  174.   }
  175.   pprintf("\r\n");
  176.   return 0;
  177. }
  178.