00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <windows.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <limits.h>
00029
00030
00031
00032
00033
00034
00035 long
00036 myatol(char *s, int base)
00037 {
00038 long result = 0;
00039 char ch;
00040 int sign = 1;
00041 if (base == 0)
00042 base = 10;
00043 if (base != 10 && base != 16)
00044 return LONG_MAX;
00045 while ((ch = *s++) != '\0') {
00046 if (ch == '-') {
00047 sign = -sign;
00048 }
00049 else if (ch >= '0' && ch <= '9') {
00050 result = result * base + (ch - '0');
00051 }
00052 else if (ch == 'x' || ch == 'X') {
00053
00054 base = 16;
00055 }
00056 else if (base == 16 && ch >= 'a' && ch <= 'f') {
00057 result = result * base + (ch - 'a' + 10);
00058 }
00059 else if (base == 16 && ch >= 'A' && ch <= 'F') {
00060 result = result * base + (ch - 'A' + 10);
00061 }
00062 else {
00063 if (sign > 1)
00064 return LONG_MAX;
00065 else
00066 return LONG_MIN;
00067 }
00068 }
00069 return sign * result;
00070 }
00071
00072 void
00073 usage_exit()
00074 {
00075 fprintf(stderr, "Usage: kill [ -sig ] pid\n");
00076 fprintf(stderr, " for win32, sig must be or 0, 15 (TERM)\n");
00077 exit(EXIT_FAILURE);
00078 }
00079
00080 int
00081 main(int argc, char **argv)
00082 {
00083 HANDLE hProcess ;
00084 DWORD accessflag;
00085 long pid;
00086 int sig = 15;
00087
00088 if (argc > 2) {
00089 if (argv[1][0] != '-')
00090 usage_exit();
00091
00092 if (strcmp(argv[1], "-TERM") == 0)
00093 sig = 15;
00094 else {
00095
00096
00097
00098 sig = atoi(&argv[1][1]);
00099 if (sig < 0)
00100 usage_exit();
00101 }
00102 argc--;
00103 argv++;
00104 }
00105 if (argc < 2)
00106 usage_exit();
00107
00108 pid = myatol(argv[1], 10);
00109
00110 if (pid == LONG_MAX || pid == LONG_MIN)
00111 usage_exit();
00112
00113 if (sig == 0)
00114 accessflag = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
00115 else
00116 accessflag = STANDARD_RIGHTS_REQUIRED | PROCESS_TERMINATE;
00117 hProcess = OpenProcess(accessflag, FALSE, pid);
00118 if (hProcess == NULL) {
00119 fprintf(stderr, "dbkill: %s: no such process\n", argv[1]);
00120 exit(EXIT_FAILURE);
00121 }
00122 if (sig == 0)
00123 exit(EXIT_SUCCESS);
00124 if (!TerminateProcess(hProcess, 99)) {
00125 DWORD err = GetLastError();
00126 fprintf(stderr,
00127 "dbkill: cannot kill process: error %d (0x%lx)\n", err, err);
00128 exit(EXIT_FAILURE);
00129 }
00130 return EXIT_SUCCESS;
00131 }