Skip to content
Snippets Groups Projects
cat.c 513 B
Newer Older
kaashoek's avatar
kaashoek committed
#include "types.h"
#include "stat.h"
#include "user.h"

rsc's avatar
 
rsc committed
char buf[512];
kaashoek's avatar
kaashoek committed
void
rsc's avatar
rsc committed
cat(int fd)
rsc's avatar
 
rsc committed
  int n;
rsc's avatar
 
rsc committed
  while((n = read(fd, buf, sizeof(buf))) > 0)
    write(1, buf, n);
  if(n < 0){
    printf(1, "cat: read error\n");
    exit();
  }
kaashoek's avatar
kaashoek committed
}

int
main(int argc, char *argv[])
{
  int fd, i;
rsc's avatar
rsc committed
  if(argc <= 1){
rsc's avatar
rsc committed
    cat(0);
rsc's avatar
 
rsc committed
    exit();
kaashoek's avatar
kaashoek committed
  }
rsc's avatar
 
rsc committed
  for(i = 1; i < argc; i++){
    if((fd = open(argv[i], 0)) < 0){
      printf(1, "cat: cannot open %s\n", argv[i]);
      exit();
    }
rsc's avatar
rsc committed
    cat(fd);
rsc's avatar
 
rsc committed
    close(fd);
  }
  exit();
}