Skip to content
Snippets Groups Projects
  • kaashoek's avatar
    i/o redirection in sh · 8b58e810
    kaashoek authored
    better parsing of sh commands (copied from jos sh)
    cat: read from 1 if no args
    sbrk system call, but untested
    getpid system call
    moved locks in keyboard intr, but why do we get intr w. null characters from keyboard?
    8b58e810
cat.c 567 B
#include "types.h"
#include "stat.h"
#include "user.h"

char buf[513];

void
rfile(int fd)
{
  int cc;

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

int
main(int argc, char *argv[])
{
  int fd, i;

  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();
    }
    rfile(fd);
    close(fd);
  }
  }

  exit();
}