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

char buf[513];

kaashoek's avatar
kaashoek committed
void
rfile(int fd)
kaashoek's avatar
kaashoek committed
  int cc;
kaashoek's avatar
kaashoek committed
  while((cc = read(fd, buf, sizeof(buf) - 1)) > 0){
    buf[cc] = '\0';
    puts(buf);
  }
  if(cc < 0){
    puts("cat: read error\n");
    exit();
  }
kaashoek's avatar
kaashoek committed
}

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