XINU
xsh_kill.c
Go to the documentation of this file.
1 /* xsh_kill.c - xsh_kill */
2 
3 #include <xinu.h>
4 #include <string.h>
5 #include <stdio.h>
6 
7 /*------------------------------------------------------------------------
8  * xsh_kill - obtain and print the current month, day, year, and time
9  *------------------------------------------------------------------------
10  */
11 shellcmd xsh_kill(int nargs, char *args[]) {
12 
13  int32 retval; /* return value */
14  pid32 pid; /* ID of process to kill */
15  char ch; /* next character of argument */
16  char *chptr; /* walks along argument string */
17 
18  /* Output info for '--help' argument */
19 
20  if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
21  printf("Usage: %s PID\n\n", args[0]);
22  printf("Description:\n");
23  printf("\tterminates a process\n");
24  printf("Options:\n");
25  printf("\tPID \tthe ID of a process to terminate\n");
26  printf("\t--help\tdisplay this help and exit\n");
27  return OK;
28  }
29 
30  /* Check argument count */
31 
32  if (nargs != 2) {
33  fprintf(stderr, "%s: incorrect argument\n", args[0]);
34  fprintf(stderr, "Try '%s --help' for more information\n",
35  args[0]);
36  return SYSERR;
37  }
38 
39  /* compute process ID from argument string */
40 
41  chptr = args[1];
42  ch = *chptr++;
43  pid = 0;
44  while(ch != NULLCH) {
45  if ( (ch < '0') || (ch > '9') ) {
46  fprintf(stderr, "%s: non-digit in process ID\n",
47  args[0]);
48  return 1;
49  }
50  pid = 10*pid + (ch - '0');
51  ch = *chptr++;
52  }
53  if (pid == 0) {
54  fprintf(stderr, "%s: cannot kill the null process\n",
55  args[0]);
56  return 1;
57  }
58 
59  retval = kill(pid);
60  if (retval == SYSERR) {
61  fprintf(stderr, "%s: cannot kill process %d\n",
62  args[0], pid);
63  return 1;
64  }
65  return 0;
66 }
int32 strncmp(const char *, const char *, int32)
全てのシステムヘッダファイルをインクルードする。
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
#define stderr
Definition: stdio.h:17
#define OK
処理が成功した場合
Definition: kernel.h:77
int32 printf(const char *,...)
Definition: printf.c:13
syscall kill(pid32)
指定のプロセスを終了させ、システムから終了させたプロセス情報を取り除く。
Definition: kill.c:31
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
int32 pid32
プロセスID
Definition: kernel.h:26
int32 shellcmd
シェルコール関数 返り値の型
Definition: kernel.h:51
int32 fprintf(int, char *,...)
Definition: fprintf.c:14
shellcmd xsh_kill(int nargs, char *args[])
Definition: xsh_kill.c:11
#define NULLCH
NULL文字(NULL終端)
Definition: kernel.h:70