seek_tell.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "common.h"
  2. #define EXTEND_SIZE 4097
  3. void seek_input_fd(const char* path) {
  4. int f = open_input_fd(path);
  5. printf("open(%s) input OK\n", path);
  6. seek_fd(path, f, 0, SEEK_SET);
  7. printf("seek(%s) input start OK\n", path);
  8. seek_fd(path, f, 0, SEEK_END);
  9. printf("seek(%s) input end OK\n", path);
  10. off_t pos = tell_fd(path, f);
  11. printf("tell(%s) input end OK: %zd\n", path, pos);
  12. seek_fd(path, f, -pos, SEEK_END); // rewind
  13. printf("seek(%s) input rewind OK\n", path);
  14. pos = tell_fd(path, f);
  15. printf("tell(%s) input start OK: %zd\n", path, pos);
  16. close_fd(path, f);
  17. printf("close(%s) input OK\n", path);
  18. }
  19. void seek_input_stdio(const char* path) {
  20. FILE* f = open_input_stdio(path);
  21. printf("fopen(%s) input OK\n", path);
  22. seek_stdio(path, f, 0, SEEK_SET);
  23. printf("fseek(%s) input start OK\n", path);
  24. seek_stdio(path, f, 0, SEEK_END);
  25. printf("fseek(%s) input end OK\n", path);
  26. off_t pos = tell_stdio(path, f);
  27. printf("ftell(%s) input end OK: %zd\n", path, pos);
  28. seek_stdio(path, f, -pos, SEEK_END); // rewind
  29. printf("fseek(%s) input rewind OK\n", path);
  30. pos = tell_stdio(path, f);
  31. printf("ftell(%s) input start OK: %zd\n", path, pos);
  32. close_stdio(path, f);
  33. printf("fclose(%s) input OK\n", path);
  34. }
  35. void seek_output_fd(const char* path) {
  36. uint8_t buf[EXTEND_SIZE + 1] = {1};
  37. int f = open_output_fd(path, /*rdwr=*/true);
  38. printf("open(%s) output OK\n", path);
  39. seek_fd(path, f, 0, SEEK_SET);
  40. printf("seek(%s) output start OK\n", path);
  41. seek_fd(path, f, 0, SEEK_END);
  42. printf("seek(%s) output end OK\n", path);
  43. off_t pos = tell_fd(path, f);
  44. printf("tell(%s) output end OK: %zd\n", path, pos);
  45. seek_fd(path, f, EXTEND_SIZE, SEEK_CUR); // extend
  46. printf("seek(%s) output end 2 OK\n", path);
  47. write_fd(path, f, buf, 1);
  48. seek_fd(path, f, -EXTEND_SIZE - 1, SEEK_CUR); // rewind to former end
  49. printf("seek(%s) output end 3 OK\n", path);
  50. read_fd(path, f, buf, EXTEND_SIZE + 1);
  51. for (size_t i=0; i<EXTEND_SIZE + 1; i++) {
  52. if (i == EXTEND_SIZE) {
  53. if (buf[i] != 1)
  54. fatal_error("invalid last byte\n");
  55. } else {
  56. if (buf[i] != 0)
  57. fatal_error("extended buffer not zeroed\n");
  58. }
  59. }
  60. pos = tell_fd(path, f);
  61. printf("tell(%s) output end 2 OK: %zd\n", path, pos);
  62. close_fd(path, f);
  63. printf("close(%s) output OK\n", path);
  64. }
  65. void seek_output_stdio(const char* path) {
  66. uint8_t buf[EXTEND_SIZE + 1] = {1};
  67. FILE* f = open_output_stdio(path, /*rdwr=*/true);
  68. printf("fopen(%s) output OK\n", path);
  69. seek_stdio(path, f, 0, SEEK_SET);
  70. printf("fseek(%s) output start OK\n", path);
  71. seek_stdio(path, f, 0, SEEK_END);
  72. printf("fseek(%s) output end OK\n", path);
  73. off_t pos = tell_stdio(path, f);
  74. printf("ftell(%s) output end OK: %zd\n", path, pos);
  75. seek_stdio(path, f, EXTEND_SIZE, SEEK_CUR); // extend
  76. printf("fseek(%s) output end 2 OK\n", path);
  77. write_stdio(path, f, buf, 1);
  78. seek_stdio(path, f, -EXTEND_SIZE - 1, SEEK_CUR); // rewind to former end
  79. printf("fseek(%s) output end 3 OK\n", path);
  80. read_stdio(path, f, buf, EXTEND_SIZE + 1);
  81. for (size_t i=0; i<EXTEND_SIZE + 1; i++) {
  82. if (i == EXTEND_SIZE) {
  83. if (buf[i] != 1)
  84. fatal_error("invalid last byte\n");
  85. } else {
  86. if (buf[i] != 0)
  87. fatal_error("extended buffer not zeroed\n");
  88. }
  89. }
  90. pos = tell_stdio(path, f);
  91. printf("ftell(%s) output end 2 OK: %zd\n", path, pos);
  92. close_stdio(path, f);
  93. printf("fclose(%s) output OK\n", path);
  94. }
  95. int main(int argc, char* argv[]) {
  96. if (argc < 4)
  97. fatal_error("Usage: %s <input_path> <output_path_1> <output_path_2>\n", argv[0]);
  98. setup();
  99. seek_input_fd(argv[1]);
  100. seek_input_stdio(argv[1]);
  101. seek_output_fd(argv[2]);
  102. seek_output_stdio(argv[3]);
  103. return 0;
  104. }