logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

truncate.c (2733B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #include "../lib/bitmasks.h" // FIELD_CLR
  6. #include "../lib/truncation.h" // parse_size
  7. #include <errno.h>
  8. #include <fcntl.h> // open
  9. #include <stdbool.h>
  10. #include <stdio.h> // fprintf
  11. #include <stdlib.h> // abort
  12. #include <string.h> // strerror
  13. #include <sys/stat.h>
  14. #include <unistd.h> // getopt
  15. const char *argv0 = "truncate";
  16. static void
  17. usage(void)
  18. {
  19. fprintf(stderr, "Usage: truncate [-co] [-r ref_file] [-s size] file...\n");
  20. }
  21. int
  22. main(int argc, char *argv[])
  23. {
  24. int open_flags = O_WRONLY | O_CREAT | O_NONBLOCK;
  25. bool size_set = false;
  26. struct truncation tr = {
  27. .op = OP_SET,
  28. .size = 0,
  29. };
  30. char *ref_file = NULL;
  31. for(int c = -1; (c = getopt(argc, argv, ":cr:s:")) != -1;)
  32. {
  33. switch(c)
  34. {
  35. case 'c':
  36. FIELD_CLR(open_flags, O_CREAT);
  37. break;
  38. case 'r':
  39. if(size_set)
  40. {
  41. fprintf(stderr, "truncate: error: Truncation size can only be set once\n");
  42. usage();
  43. return 1;
  44. }
  45. ref_file = optarg;
  46. size_set = true;
  47. break;
  48. case 's':
  49. if(size_set)
  50. {
  51. fprintf(stderr, "truncate: error: Truncation size can only be set once\n");
  52. usage();
  53. return 1;
  54. }
  55. if(parse_size(optarg, &tr) < 0)
  56. {
  57. usage();
  58. return 1;
  59. }
  60. size_set = true;
  61. break;
  62. case ':':
  63. fprintf(stderr, "truncate: error: Missing operand for option: '-%c'\n", optopt);
  64. usage();
  65. return 1;
  66. case '?':
  67. fprintf(stderr, "truncate: error: Unrecognised option: '-%c'\n", optopt);
  68. usage();
  69. return 1;
  70. default:
  71. abort();
  72. }
  73. }
  74. argc -= optind;
  75. argv += optind;
  76. if(argc < 1) usage();
  77. if(!size_set)
  78. {
  79. fprintf(stderr,
  80. "truncate: error: target size wasn't set, you need to pass one of -d / -r / -s\n");
  81. return 1;
  82. }
  83. if(ref_file != NULL)
  84. {
  85. struct stat ref_stats;
  86. if(stat(optarg, &ref_stats) < 0)
  87. {
  88. fprintf(stderr,
  89. "truncate: error: Couldn't get status for file '%s': %s\n",
  90. optarg,
  91. strerror(errno));
  92. return 1;
  93. }
  94. tr.op = OP_SET;
  95. tr.size = ref_stats.st_size;
  96. }
  97. for(int argi = 0; argi < argc; argi++)
  98. {
  99. char *arg = argv[argi];
  100. // same flags to open as touch(1)
  101. int fd = open(arg, open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  102. if(fd < 0)
  103. {
  104. fprintf(stderr, "truncate: error: Failed to open '%s': %s\n", arg, strerror(errno));
  105. return 1;
  106. }
  107. if(apply_truncation(fd, tr, arg) < 0) return 1;
  108. if(close(fd) < 0)
  109. {
  110. fprintf(stderr, "truncate: error: Failed closing fd for '%s': %s\n", arg, strerror(errno));
  111. return 1;
  112. }
  113. }
  114. return 0;
  115. }