home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / spe.zip / spe.c < prev    next >
C/C++ Source or Header  |  1997-03-19  |  5KB  |  178 lines

  1. /*****************************************************************************
  2.     SetPriority with exec (SPE)
  3.  
  4.     Author:
  5.         J S Worthington
  6.         98B Waiwhetu Road
  7.         Lower Hutt
  8.         New Zealand
  9.         Phone:
  10.             +64-4-569-6764 (home)
  11.             +64-4-389-8909 (work)
  12.         Fax:
  13.             +64-4-389-9901 (work)
  14.         Web:
  15.             http://www.digitech.co.nz (work)
  16.         Email:
  17.             stephen@inisant.actrix.gen.nz (home)
  18.             stephen@digitech.co.nz (work)
  19.  
  20.     Based on the sp.c source code in sp102.zip by Jens Glathe:
  21.         Angepaßt an OS/2 Version 2.x von Jens Glathe, 3.9.1993
  22.         Erweiterung für das Beeinflussen von DOS-Boxen: 6.9.1993,16/17.9.1993
  23.         letzte Änderung: Jens Glathe 15.05.1994
  24.         SetPriority Version 1.02 (c) 1993, 1994 Jens Glathe, Scott Dudley
  25.  
  26.     I wrote this because the original sp program uses system() to execute the
  27.     program.  This causes a command processor to be used to run the program,
  28.     and the command processor remains in use (but not executing) until the
  29.     executed program exits.  This is a waste of resources.  By using exec(),
  30.     this version only leaves the executed program running.
  31. *****************************************************************************/
  32.  
  33. #define INCL_DOSPROCESS
  34. #define INCL_NOPM
  35. #include <os2.h>
  36.  
  37. #include <ctype.h>
  38. #include <errno.h>
  39. #include <process.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42.  
  43. /*****************************************************************************
  44.     Macros.
  45. *****************************************************************************/
  46.  
  47. #define array_size(array) \
  48.     (sizeof (array) / sizeof (array[0]))
  49.  
  50. #define for_all(ptr, array) \
  51.     for (ptr = array; ptr < &array[array_size(array)]; ++ptr)
  52.  
  53.  
  54. /*****************************************************************************
  55.     Type definitions.
  56. *****************************************************************************/
  57.  
  58. /* Priority class table entry. */
  59. typedef struct {
  60.     char
  61.         command_char;            /* The command line character that selects the
  62.                                         required priority class. */
  63.     USHORT
  64.         priority_class;        /* The priority class. */
  65.     char
  66.         *description;            /* String describing the priority class. */
  67. } PRIORITY_CLASS;
  68.  
  69.  
  70. /*****************************************************************************
  71.     Static data.
  72. *****************************************************************************/
  73.  
  74. /* The priority class table. */
  75. static PRIORITY_CLASS priority_class_table[] = {
  76.     {'n', PRTYC_NOCHANGE, "no change in"},
  77.     {'i', PRTYC_IDLETIME, "idle"},
  78.     {'r', PRTYC_REGULAR, "regular"},
  79.     {'t', PRTYC_TIMECRITICAL, "time critical"},
  80.     {'f', PRTYC_FOREGROUNDSERVER, "foreground"},
  81. };
  82.  
  83. /* Pointer to the current priority class. */
  84. static PRIORITY_CLASS
  85.     *pcp;
  86.  
  87. static char
  88.     priority_char;            /* Command line character used to select the priority
  89.                                     class. */
  90.  
  91. static LONG
  92.     priority_delta = 0;
  93.  
  94. static APIRET
  95.     rc;                        /* OS/2 API return code. */
  96.  
  97.  
  98. /*****************************************************************************
  99.     Error - display the usage message and exit.
  100. *****************************************************************************/
  101.  
  102. void usage(void) {
  103.     printf(
  104.         "Usage:\n"
  105.         "   spe <class>[delta] <program> [args...]\n\n"
  106.         "   <class>:  n=no change, i=idle, r=regular, f=foreground,"
  107.             "t=time critical\n"
  108.         "   [delta]:  Any number from -31 to 31\n"
  109.       "   <program>: Name of the program to be run.  This should include\n"
  110.       "      the extension and can include the path.\n"
  111.       "   [args...]: The command line arguments for the <program>.\n"
  112.         "\n"
  113.       "   Note that there is no whitespace between the <class> and the\n"
  114.       "   optional [delta].\n"
  115.     );
  116.     exit(1);
  117. }
  118.  
  119.  
  120. /*****************************************************************************
  121.     Main program.
  122. *****************************************************************************/
  123.  
  124. int main(
  125.     int
  126.         argc,
  127.     char
  128.         *argv[]
  129. ) {
  130.     if (argc < 3) {
  131.         usage();
  132.     }
  133.  
  134.     priority_char = (char)tolower(*argv[1]);
  135.  
  136.     for_all(pcp, priority_class_table) {
  137.         if (pcp->command_char == priority_char) {
  138.             break;
  139.         }
  140.     }
  141.     if (pcp >= &priority_class_table[array_size(priority_class_table)]) {
  142.         usage();
  143.     }
  144.  
  145.     priority_delta = atoi(argv[1] + 1);
  146.  
  147.     if (priority_delta < -31 || priority_delta > 31) {
  148.         usage();
  149.     }
  150.  
  151.     printf(
  152.         "SPE: Running %s at %s priority, delta %ld\n",
  153.         argv[2],
  154.         pcp->description,
  155.         priority_delta
  156.     );
  157.  
  158.     if (
  159.         (rc = DosSetPriority(
  160.             PRTYS_PROCESSTREE,
  161.             pcp->priority_class,
  162.             priority_delta,
  163.             0UL
  164.         )) != 0
  165.     ) {
  166.         printf("SPE: DosSetPriority() rc = %lu\n", rc);
  167.         return rc;
  168.     }
  169.  
  170.     (void)execv(argv[2], &argv[2]);
  171.  
  172.     /* Since execv() will not return unless it found an error and could not
  173.         execute the program, if we get here we must have an error. */
  174.  
  175.     printf("SPE: execv() errno = %d\n", errno);
  176.     return errno;
  177. }
  178.