chmod.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <AK/Optional.h>
  2. #include <AK/StdLibExtras.h>
  3. #include <AK/kmalloc.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. /* the new mode will be computed using the boolean function(for each bit):
  9. |current mode|removal mask|applying mask|result |
  10. | 0 | 0 | 0 | 0 |
  11. | 0 | 0 | 1 | 1 |
  12. | 0 | 1 | 0 | 0 |
  13. | 0 | 1 | 1 | 1 | ---> find the CNF --> find the minimal CNF
  14. | 1 | 0 | 0 | 1 |
  15. | 1 | 0 | 1 | 1 |
  16. | 1 | 1 | 0 | 0 |
  17. | 1 | 1 | 1 | 1 |
  18. */
  19. class Mask {
  20. private:
  21. mode_t removal_mask; //the bits that will be removed
  22. mode_t applying_mask; //the bits that will be setted
  23. public:
  24. Mask()
  25. : removal_mask(0)
  26. , applying_mask(0)
  27. {
  28. }
  29. Mask& operator|=(const Mask& other)
  30. {
  31. removal_mask |= other.removal_mask;
  32. applying_mask |= other.applying_mask;
  33. return *this;
  34. }
  35. mode_t& get_removal_mask() { return removal_mask; }
  36. mode_t& get_applying_mask() { return applying_mask; }
  37. };
  38. Optional<Mask> string_to_mode(char access_scope, const char*& access_string);
  39. Optional<Mask> apply_permission(char access_scope, char permission, char operation);
  40. int main(int argc, char** argv)
  41. {
  42. if (argc < 3) {
  43. printf("usage: chmod <octal-mode> <path,...>\n"
  44. " chmod [[ugoa][+-=][rwx...],...] <path,...>\n");
  45. return 1;
  46. }
  47. Mask mask;
  48. /* compute a mask */
  49. if (argv[1][0] >= '0' && argv[1][0] <= '7') {
  50. if (sscanf(argv[1], "%ho", &mask.get_applying_mask()) != 1) {
  51. perror("sscanf");
  52. return 1;
  53. }
  54. mask.get_removal_mask() = ~mask.get_applying_mask();
  55. } else {
  56. const char* access_string = argv[1];
  57. while (*access_string != '\0') {
  58. Optional<Mask> tmp_mask;
  59. switch (*access_string) {
  60. case 'u':
  61. tmp_mask = string_to_mode('u', ++access_string);
  62. break;
  63. case 'g':
  64. tmp_mask = string_to_mode('g', ++access_string);
  65. break;
  66. case 'o':
  67. tmp_mask = string_to_mode('o', ++access_string);
  68. break;
  69. case 'a':
  70. tmp_mask = string_to_mode('a', ++access_string);
  71. break;
  72. case '=':
  73. case '+':
  74. case '-':
  75. tmp_mask = string_to_mode('a', access_string);
  76. break;
  77. case ',':
  78. ++access_string;
  79. continue;
  80. }
  81. if (!tmp_mask.has_value()) {
  82. fprintf(stderr, "chmod: invalid mode: %s\n", argv[1]);
  83. return 1;
  84. }
  85. mask |= tmp_mask.value();
  86. }
  87. }
  88. /* set the mask for each files' permissions */
  89. struct stat current_access;
  90. int i = 2;
  91. while (i < argc) {
  92. if (stat(argv[i], &current_access) != 0) {
  93. perror("stat");
  94. return 1;
  95. }
  96. /* found the minimal CNF by The Quine–McCluskey algorithm and use it */
  97. mode_t mode = mask.get_applying_mask()
  98. | (current_access.st_mode & ~mask.get_removal_mask());
  99. if (chmod(argv[i++], mode) != 0) {
  100. perror("chmod");
  101. }
  102. }
  103. return 0;
  104. }
  105. Optional<Mask> string_to_mode(char access_scope, const char*& access_string)
  106. {
  107. char operation = *access_string;
  108. if (operation != '+' && operation != '-' && operation != '=') {
  109. return {};
  110. }
  111. Mask mask;
  112. if (operation == '=') {
  113. switch (access_scope) {
  114. case 'u':
  115. mask.get_removal_mask() = (S_IRUSR | S_IWUSR | S_IXUSR);
  116. break;
  117. case 'g':
  118. mask.get_removal_mask() = (S_IRGRP | S_IWGRP | S_IXGRP);
  119. break;
  120. case 'o':
  121. mask.get_removal_mask() = (S_IROTH | S_IWOTH | S_IXOTH);
  122. break;
  123. case 'a':
  124. mask.get_removal_mask() = (S_IRUSR | S_IWUSR | S_IXUSR
  125. | S_IRGRP | S_IWGRP | S_IXGRP
  126. | S_IROTH | S_IWOTH | S_IXOTH);
  127. break;
  128. }
  129. operation = '+';
  130. }
  131. access_string++;
  132. while (*access_string != '\0' && *access_string != ',') {
  133. Optional<Mask> tmp_mask;
  134. tmp_mask = apply_permission(access_scope, *access_string, operation);
  135. if (!tmp_mask.has_value()) {
  136. return {};
  137. }
  138. mask |= tmp_mask.value();
  139. access_string++;
  140. }
  141. return mask;
  142. }
  143. Optional<Mask> apply_permission(char access_scope, char permission, char operation)
  144. {
  145. if (permission != 'r' && permission != 'w' && permission != 'x') {
  146. return {};
  147. }
  148. Mask mask;
  149. mode_t tmp_mask = 0;
  150. switch (access_scope) {
  151. case 'u':
  152. switch (permission) {
  153. case 'r':
  154. tmp_mask = S_IRUSR;
  155. break;
  156. case 'w':
  157. tmp_mask = S_IWUSR;
  158. break;
  159. case 'x':
  160. tmp_mask = S_IXUSR;
  161. break;
  162. }
  163. break;
  164. case 'g':
  165. switch (permission) {
  166. case 'r':
  167. tmp_mask = S_IRGRP;
  168. break;
  169. case 'w':
  170. tmp_mask = S_IWGRP;
  171. break;
  172. case 'x':
  173. tmp_mask = S_IXGRP;
  174. break;
  175. }
  176. break;
  177. case 'o':
  178. switch (permission) {
  179. case 'r':
  180. tmp_mask = S_IROTH;
  181. break;
  182. case 'w':
  183. tmp_mask = S_IWOTH;
  184. break;
  185. case 'x':
  186. tmp_mask = S_IXOTH;
  187. break;
  188. }
  189. break;
  190. case 'a':
  191. mask |= apply_permission('u', permission, operation).value();
  192. mask |= apply_permission('g', permission, operation).value();
  193. mask |= apply_permission('o', permission, operation).value();
  194. break;
  195. }
  196. if (operation == '+') {
  197. mask.get_applying_mask() |= tmp_mask;
  198. } else {
  199. mask.get_removal_mask() |= tmp_mask;
  200. }
  201. return mask;
  202. }