home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / primcuts.zip / kill.c next >
Text File  |  2000-03-22  |  2KB  |  76 lines

  1. /*---------------------------------------------------------------------------*\
  2.  *    Title: Kill                                                            *
  3.  * Filename: kill.c                                                          *
  4.  *     Date: 1999-04-16                                                      *
  5.  *   Author: Jan M. Danielsson                                               *
  6.  *                                                                           *
  7.  *  Purpose:                                                                 *
  8.  *                                                                           *
  9.  * ToDo:                                                                     *
  10.  *   - Read PID from stdin.                                                  *
  11.  *                                                                           *
  12.  * Known bugs:                                                               *
  13.  *   - None, yet                                                             *
  14. \*---------------------------------------------------------------------------*/
  15. #pragma strings(readonly)
  16.  
  17. #define INCL_DOSPROCESS
  18.  
  19. #include <os2.h>
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24.  
  25. unsigned long hex2long(char *str);
  26.  
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.    APIRET   rc;
  31.    short    i = 1;
  32.    PID      pid;
  33.  
  34.    if(argc < 2)
  35.    {
  36.       puts("Usage: Kill <pid> [pid] ...");
  37.       puts("Note: The 'pid' must be specified in hex (same as pstat uses).");
  38.       return 1;
  39.    }
  40.  
  41.    while(i < argc)
  42.    {
  43.       pid = hex2long(argv[i]);
  44.       if((rc = DosKillProcess(1, pid)) != 0)
  45.       {
  46.          printf("Error: DosKillProcess() returned %u\n", rc);
  47.       }
  48.       else
  49.       {
  50.          printf("Process %04x killed successfully.\n", pid);
  51.       }
  52.       i++;
  53.    }
  54.  
  55.    return 0;
  56. }
  57.  
  58. unsigned long hex2long(char *str)
  59. {
  60.    unsigned long i = 0;
  61.    unsigned long j = 0;
  62.  
  63.    while(*str && isxdigit(*str))
  64.    {
  65.       i = *str++ - '0';
  66.  
  67.       if(9 < i)
  68.       {
  69.          i -= 7;
  70.       }
  71.       j <<= 4;
  72.       j |= (i & 0x0f);
  73.     }
  74.     return j;
  75. }
  76.