Ejemplo n.º 1
0
int main(int argc, char **argv)
{
  ph_stream_t *stm;
  char namebuf[128];
  int fd;
  char buf[BUFSIZ];
  uint64_t amount;
  int len;

  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(18);

  strcpy(namebuf, "/tmp/phenomXXXXXX");
  fd = ph_mkostemp(namebuf, 0);
  diag("opened %s -> %d", namebuf, fd);
  unlink(namebuf);

  stm = ph_stm_fd_open(fd, 0, PH_STM_BUFSIZE);
  ph_stm_write(stm, "lemon\n", 6, &amount);
  is(amount, 6);

  // Shouldn't see it yet
  is(0, pread(fd, buf, sizeof(buf), 0));

  // Should see it now
  ph_stm_flush(stm);
  is(6, pread(fd, buf, sizeof(buf), 0));

  ok(!memcmp("lemon\n", buf, 6), "right content");
  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  memset(buf, 0, sizeof(buf));

  ok(ph_stm_read(stm, buf, 3, &amount), "read ok");
  ok(amount == 3, "amount is %" PRIu64, amount);
  ok(!memcmp("lem", buf, 3), "got prefix");

  ok(ph_stm_read(stm, buf, 3, &amount), "read ok");
  ok(amount == 3, "amount is %" PRIu64, amount);
  ok(!memcmp("on\n", buf, 3), "got remainder");

  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  len = ph_stm_printf(stm, "testing %d %s!", 1, "two");
  ok(14 == len, "printed len %d", len);
  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  memset(buf, 0, sizeof(buf));
  ok(ph_stm_read(stm, buf, 14, &amount), "read ok");
  ok(amount == 14, "len was %" PRIu64, amount);
  ok(!memcmp("testing 1 two!", buf, 14), "got formatted");

  ok(ph_stm_close(stm), "closed");

  return exit_status();
}
Ejemplo n.º 2
0
ph_stream_t *ph_stm_file_open(const char *filename, int oflags, int mode)
{
  int fd;
  ph_stream_t *stm;

#ifdef O_LARGEFILE
  oflags |= O_LARGEFILE;
#endif

  fd = open(filename, oflags, mode);
  if (fd < 0) {
    return NULL;
  }

  stm = ph_stm_fd_open(fd, 0, PH_STM_BUFSIZE);
  if (!stm) {
    close(fd);
    errno = ENOMEM;
  }

  return stm;
}