new object $file: $root;

var $root inited = 1;
var $file stat = 0;

public method .files() {
  arg path;

  return (> files(path) <);
};

public method .stat() {
  arg @path;

  if (path) {
    return (> fstat(@path) <);
  } else {
    return stat;
  }
};

public method .open() {
  arg path, @mode;
  var obj, stat;

  stat = (| .fstat(path) |);
  if (!stat || stat[1][1] == "4") {
    .debug(path);
    throw(~filenf, "File not found");
  }
  obj = .spawn();
  (> obj._fopen(path, stat, @mode) <);
  return obj;
};

public method ._fopen() {
  arg path, _stat, @mode;

  stat = _stat;
  (> fopen(path, @mode) <);
};

public method .read() {
  return (> fread() <);
};

public method .close() {
  (| fclose() |);
  .destroy();
};

public method .slurp() {
  var data;

  data = `[];
  while ((| data += fread() |) != ~eof);
  return data;
};

public method .readlines() {
  var line, lines;

  lines = [];
  while ((| line = fread() |) != ~eof) {
    lines = [@lines, line];
  }
  return lines;
};