root/user/cat.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cat
  2. main

   1 #include "kernel/types.h"
   2 #include "kernel/fcntl.h"
   3 #include "user/user.h"
   4 
   5 char buf[512];
   6 
   7 void
   8 cat(int fd)
   9 {
  10   int n;
  11 
  12   while((n = read(fd, buf, sizeof(buf))) > 0) {
  13     if (write(1, buf, n) != n) {
  14       fprintf(2, "cat: write error\n");
  15       exit(1);
  16     }
  17   }
  18   if(n < 0){
  19     fprintf(2, "cat: read error\n");
  20     exit(1);
  21   }
  22 }
  23 
  24 int
  25 main(int argc, char *argv[])
  26 {
  27   int fd, i;
  28 
  29   if(argc <= 1){
  30     cat(0);
  31     exit(0);
  32   }
  33 
  34   for(i = 1; i < argc; i++){
  35     if((fd = open(argv[i], O_RDONLY)) < 0){
  36       fprintf(2, "cat: cannot open %s\n", argv[i]);
  37       exit(1);
  38     }
  39     cat(fd);
  40     close(fd);
  41   }
  42   exit(0);
  43 }

/* [<][>][^][v][top][bottom][index][help] */