XINU
Functions
xsh_kill.c File Reference
#include <xinu.h>
#include <string.h>
#include <stdio.h>
Include dependency graph for xsh_kill.c:

Go to the source code of this file.

Functions

shellcmd xsh_kill (int nargs, char *args[])
 

Function Documentation

◆ xsh_kill()

shellcmd xsh_kill ( int  nargs,
char *  args[] 
)

Definition at line 11 of file xsh_kill.c.

References fprintf(), kill(), NULLCH, OK, printf(), stderr, strncmp(), and SYSERR.

11  {
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 fprintf(int, char *,...)
Definition: fprintf.c:14
#define NULLCH
NULL文字(NULL終端)
Definition: kernel.h:70
Here is the call graph for this function: