run-term-eof.c 1019 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <ccan/rbuf/rbuf.h>
  2. /* Include the C files directly. */
  3. #include <ccan/rbuf/rbuf.c>
  4. #include <ccan/tap/tap.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <stdlib.h>
  9. int main(void)
  10. {
  11. struct rbuf in;
  12. char buf[4096], *p;
  13. int fd = open("test/run-term-eof.c", O_RDONLY), len;
  14. /* This is how many tests you plan to run */
  15. plan_tests(6);
  16. /* Grab ourselves for comparison. */
  17. len = read(fd, buf, sizeof(buf));
  18. buf[len] = '\0';
  19. lseek(fd, SEEK_SET, 0);
  20. /* We have exact-size buffer, which causes problems adding term. */
  21. rbuf_init(&in, fd, malloc(len), len);
  22. p = rbuf_read_str(&in, 64, NULL); /* At symbol does not appear. */
  23. ok1(errno == ENOMEM);
  24. ok1(!p);
  25. /* This should succeed... */
  26. p = rbuf_read_str(&in, 64, realloc);
  27. ok1(p);
  28. ok1(strcmp(p, buf) == 0);
  29. free(in.buf);
  30. /* Try again. */
  31. lseek(fd, SEEK_SET, 0);
  32. rbuf_init(&in, fd, malloc(len), len);
  33. p = rbuf_read_str(&in, 64, realloc);
  34. ok1(p);
  35. ok1(strcmp(p, buf) == 0);
  36. free(in.buf);
  37. return exit_status();
  38. }