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

Go to the source code of this file.

Functions

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

Function Documentation

◆ xsh_cat()

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

Definition at line 11 of file xsh_cat.c.

References close(), EOF, fprintf(), getc(), NAMESPACE, NULLCH, open(), printf(), putc(), stderr, stdin, stdout, strncmp(), and SYSERR.

12 {
13  int32 i; /* index into proctabl */
14  int32 nextch; /* character read from file */
15  did32 descr; /* descriptor for a file */
16  char *argptr; /* pointer to next arg string */
17 
18 
19  /* For argument '--help', emit help about the 'cat' command */
20 
21  if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
22  printf("Use: %s [file...]\n\n", args[0]);
23  printf("Description:\n");
24  printf("\twrites contents of files or stdin to stdout\n");
25  printf("Options:\n");
26  printf("\tfile...\tzero or more file names\n");
27  printf("\t--help\t display this help and exit\n");
28  return 0;
29  }
30 
31  if (nargs == 1) {
32  nextch = getc(stdin);
33  while (nextch != EOF) {
34  putc(stdout, nextch);
35  nextch = getc(stdin);
36  }
37  return 0;
38  }
39  for (i = 1; i < nargs; i++) {
40  argptr = args[i];
41  if ( (argptr[0] == '-') && (argptr[1] == NULLCH) ) {
42  descr = stdin;
43  } else {
44  descr = open(NAMESPACE, argptr, "ro");
45  if (descr == (did32)SYSERR) {
46  fprintf(stderr, "%s: cannot open file %s\n",
47  args[0], argptr);
48  return 1;
49  }
50  }
51  nextch = getc(descr);
52  while (nextch != EOF) {
53  putc(stdout, nextch);
54  nextch = getc(descr);
55  }
56  close(descr);
57  }
58  return 0;
59 }
int32 strncmp(const char *, const char *, int32)
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
syscall close(did32)
Definition: close.c:9
#define stderr
Definition: stdio.h:17
#define EOF
ファイルの終端(End of File)に達した場合(読み込み処理に用いる)
Definition: kernel.h:81
#define stdout
Definition: stdio.h:16
int32 printf(const char *,...)
Definition: printf.c:13
syscall getc(did32)
Definition: getc.c:9
syscall putc(did32, char)
デバイスへ文字1Byteを送信する。
Definition: putc.c:18
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
#define stdin
Definition: stdio.h:15
int32 did32
デバイスID
Definition: kernel.h:28
int32 fprintf(int, char *,...)
Definition: fprintf.c:14
syscall open(did32, char *, char *)
Definition: open.c:9
#define NAMESPACE
Definition: conf.h:35
#define NULLCH
NULL文字(NULL終端)
Definition: kernel.h:70
Here is the call graph for this function: