stat.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include "common.h"
  2. void file_stat(const char* file_path, bool writable) {
  3. struct stat st;
  4. const char* type = writable ? "output" : "input";
  5. if (stat(file_path, &st) != 0)
  6. fatal_error("Failed to stat file %s: %s\n", file_path, strerror(errno));
  7. printf("stat(%s) %s 1 OK: %zu\n", file_path, type, st.st_size);
  8. int fd = writable ? open_output_fd(file_path, /*rdwr=*/false) : open_input_fd(file_path);
  9. printf("open(%s) %s 2 OK\n", file_path, type);
  10. if (stat(file_path, &st) != 0)
  11. fatal_error("Failed to stat file %s: %s\n", file_path, strerror(errno));
  12. printf("stat(%s) %s 2 OK: %zu\n", file_path, type, st.st_size);
  13. if (fstat(fd, &st) != 0)
  14. fatal_error("Failed to fstat file %s: %s\n", file_path, strerror(errno));
  15. printf("fstat(%s) %s 2 OK: %zu\n", file_path, type, st.st_size);
  16. close_fd(file_path, fd);
  17. printf("close(%s) %s 2 OK\n", file_path, type);
  18. }
  19. int main(int argc, char* argv[]) {
  20. if (argc < 3)
  21. fatal_error("Usage: %s <input_path> <output_path>\n", argv[0]);
  22. setup();
  23. file_stat(argv[1], false);
  24. file_stat(argv[2], true);
  25. return 0;
  26. }