File: kill.c

package info (click to toggle)
combat 0.8.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,404 kB
  • sloc: tcl: 10,461; sh: 2,535; makefile: 154; ansic: 73
file content (98 lines) | stat: -rwxr-xr-x 1,856 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * ----------------------------------------------------------------------
 * A "kill" command.
 * ----------------------------------------------------------------------
 */

#include <tcl.h>

#if defined (__WIN32)

#include <windows.h>

static
int
tclkill (ClientData clientData,
	 Tcl_Interp * interp,
	 int objc,
	 Tcl_Obj * CONST objv[])
{
  int pid, res;
  HANDLE h;

  if (objc != 2) {
    Tcl_WrongNumArgs (interp, 1, objv, "pid");
    return TCL_ERROR;
  }

  if ((res = Tcl_GetIntFromObj (interp, objv[1], &pid)) != TCL_OK) {
    return res;
  }

  if (!(h = OpenProcess (PROCESS_TERMINATE, 0, pid))) {
    Tcl_AppendResult (interp, "invalid pid or access denied", NULL);
    return TCL_ERROR;
  }

  if (TerminateProcess (h, 1) == 0) {
    Tcl_AppendResult (interp, "TerminateProcess failed", NULL);
    return TCL_ERROR;
  }

  CloseHandle (h);

  Tcl_SetObjResult (interp, Tcl_NewObj());
  return TCL_OK;
}

#else

#include <sys/types.h>
#include <signal.h>

static
int
tclkill (ClientData clientData,
	 Tcl_Interp * interp,
	 int objc,
	 Tcl_Obj * CONST objv[])
{
  int pid, res;

  if (objc != 2) {
    Tcl_WrongNumArgs (interp, 1, objv, "pid");
    return TCL_ERROR;
  }

  if ((res = Tcl_GetIntFromObj (interp, objv[1], &pid)) != TCL_OK) {
    return res;
  }

  if (kill ((pid_t) pid, SIGTERM) != 0) {
    Tcl_AppendResult (interp, "invalid pid or access denied", NULL);
    return TCL_ERROR;
  }

  Tcl_SetObjResult (interp, Tcl_NewObj());
  return TCL_OK;
}

#endif

int
Kill_Init (Tcl_Interp * interp)
{
#ifdef USE_TCL_STUBS
  if (Tcl_InitStubs (interp, TCL_VERSION, 0) == NULL) {
    return TCL_ERROR;
  }
#else
  if (Tcl_PkgRequire (interp, "Tcl", TCL_VERSION, 0) == NULL) {
    return TCL_ERROR;
  }
#endif

  Tcl_CreateObjCommand (interp, "kill", tclkill, NULL, NULL);
  Tcl_PkgProvide (interp, "kill", "1.0");
  return TCL_OK;
}