truncate.c 948 B

12345678910111213141516171819202122232425262728
  1. #include "common.h"
  2. void file_truncate(const char* file_path_1, const char* file_path_2, size_t size) {
  3. if (truncate(file_path_1, size) != 0)
  4. fatal_error("Failed to truncate file %s to %zu: %s\n", file_path_1, size, strerror(errno));
  5. printf("truncate(%s) to %zu OK\n", file_path_1, size);
  6. int fd = open_output_fd(file_path_2, /*rdwr=*/false);
  7. printf("open(%s) output OK\n", file_path_2);
  8. if (ftruncate(fd, size) != 0)
  9. fatal_error("Failed to ftruncate file %s to %zu: %s\n", file_path_2, size, strerror(errno));
  10. printf("ftruncate(%s) to %zu OK\n", file_path_2, size);
  11. close_fd(file_path_2, fd);
  12. printf("close(%s) output OK\n", file_path_2);
  13. }
  14. int main(int argc, char* argv[]) {
  15. if (argc < 4)
  16. fatal_error("Usage: %s <file_path_1> <file_path_2> <size>\n", argv[0]);
  17. setup();
  18. size_t size = strtoul(argv[3], NULL, 10);
  19. file_truncate(argv[1], argv[2], size);
  20. return 0;
  21. }