chmod.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* Implementation of the CHMOD intrinsic.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #if defined(HAVE_SYS_STAT_H)
  22. #include <sys/stat.h> /* For stat, chmod and umask. */
  23. /* INTEGER FUNCTION CHMOD (NAME, MODE)
  24. CHARACTER(len=*), INTENT(IN) :: NAME, MODE
  25. Sets the file permission "chmod" using a mode string.
  26. For MinGW, only _S_IWRITE and _S_IREAD are supported. To set those,
  27. only the user attributes are used.
  28. The mode string allows for the same arguments as POSIX's chmod utility.
  29. a) string containing an octal number.
  30. b) Comma separated list of clauses of the form:
  31. [<who-list>]<op>[<perm-list>|<permcopy>][<op>[<perm-list>|<permcopy>],...]
  32. <who> - 'u', 'g', 'o', 'a'
  33. <op> - '+', '-', '='
  34. <perm> - 'r', 'w', 'x', 'X', 's', t'
  35. If <op> is not followed by a perm-list or permcopy, '-' and '+' do not
  36. change the mode while '=' clears all file mode bits. 'u' stands for the
  37. user permissions, 'g' for the group and 'o' for the permissions for others.
  38. 'a' is equivalent to 'ugo'. '+' sets the given permission in addition to
  39. the ones of the file, '-' unsets the given permissions of the file, while
  40. '=' sets the file to that mode. 'r' sets the read, 'w' the write, and
  41. 'x' the execute mode. 'X' sets the execute bit if the file is a directory
  42. or if the user, group or other executable bit is set. 't' sets the sticky
  43. bit, 's' (un)sets the and/or S_ISUID/S_ISGID bit.
  44. Note that if <who> is omitted, the permissions are filtered by the umask.
  45. A return value of 0 indicates success, -1 an error of chmod() while 1
  46. indicates a mode parsing error. */
  47. static int
  48. chmod_internal (char *file, char *mode, gfc_charlen_type mode_len)
  49. {
  50. bool ugo[3];
  51. bool rwxXstugo[9];
  52. int set_mode, part;
  53. bool honor_umask, continue_clause = false;
  54. #ifndef __MINGW32__
  55. bool is_dir;
  56. #endif
  57. #ifdef HAVE_UMASK
  58. mode_t mode_mask;
  59. #endif
  60. mode_t file_mode, new_mode;
  61. struct stat stat_buf;
  62. if (mode_len == 0)
  63. return 1;
  64. if (mode[0] >= '0' && mode[0] <= '9')
  65. {
  66. unsigned fmode;
  67. if (sscanf (mode, "%o", &fmode) != 1)
  68. return 1;
  69. return chmod (file, (mode_t) fmode);
  70. }
  71. /* Read the current file mode. */
  72. if (stat (file, &stat_buf))
  73. return 1;
  74. file_mode = stat_buf.st_mode & ~S_IFMT;
  75. #ifndef __MINGW32__
  76. is_dir = stat_buf.st_mode & S_IFDIR;
  77. #endif
  78. #ifdef HAVE_UMASK
  79. /* Obtain the umask without distroying the setting. */
  80. mode_mask = 0;
  81. mode_mask = umask (mode_mask);
  82. (void) umask (mode_mask);
  83. #else
  84. honor_umask = false;
  85. #endif
  86. for (gfc_charlen_type i = 0; i < mode_len; i++)
  87. {
  88. if (!continue_clause)
  89. {
  90. ugo[0] = false;
  91. ugo[1] = false;
  92. ugo[2] = false;
  93. #ifdef HAVE_UMASK
  94. honor_umask = true;
  95. #endif
  96. }
  97. continue_clause = false;
  98. rwxXstugo[0] = false;
  99. rwxXstugo[1] = false;
  100. rwxXstugo[2] = false;
  101. rwxXstugo[3] = false;
  102. rwxXstugo[4] = false;
  103. rwxXstugo[5] = false;
  104. rwxXstugo[6] = false;
  105. rwxXstugo[7] = false;
  106. rwxXstugo[8] = false;
  107. part = 0;
  108. set_mode = -1;
  109. for (; i < mode_len; i++)
  110. {
  111. switch (mode[i])
  112. {
  113. /* User setting: a[ll]/u[ser]/g[roup]/o[ther]. */
  114. case 'a':
  115. if (part > 1)
  116. return 1;
  117. ugo[0] = true;
  118. ugo[1] = true;
  119. ugo[2] = true;
  120. part = 1;
  121. #ifdef HAVE_UMASK
  122. honor_umask = false;
  123. #endif
  124. break;
  125. case 'u':
  126. if (part == 2)
  127. {
  128. rwxXstugo[6] = true;
  129. part = 4;
  130. break;
  131. }
  132. if (part > 1)
  133. return 1;
  134. ugo[0] = true;
  135. part = 1;
  136. #ifdef HAVE_UMASK
  137. honor_umask = false;
  138. #endif
  139. break;
  140. case 'g':
  141. if (part == 2)
  142. {
  143. rwxXstugo[7] = true;
  144. part = 4;
  145. break;
  146. }
  147. if (part > 1)
  148. return 1;
  149. ugo[1] = true;
  150. part = 1;
  151. #ifdef HAVE_UMASK
  152. honor_umask = false;
  153. #endif
  154. break;
  155. case 'o':
  156. if (part == 2)
  157. {
  158. rwxXstugo[8] = true;
  159. part = 4;
  160. break;
  161. }
  162. if (part > 1)
  163. return 1;
  164. ugo[2] = true;
  165. part = 1;
  166. #ifdef HAVE_UMASK
  167. honor_umask = false;
  168. #endif
  169. break;
  170. /* Mode setting: =+-. */
  171. case '=':
  172. if (part > 2)
  173. {
  174. continue_clause = true;
  175. i--;
  176. part = 2;
  177. goto clause_done;
  178. }
  179. set_mode = 1;
  180. part = 2;
  181. break;
  182. case '-':
  183. if (part > 2)
  184. {
  185. continue_clause = true;
  186. i--;
  187. part = 2;
  188. goto clause_done;
  189. }
  190. set_mode = 2;
  191. part = 2;
  192. break;
  193. case '+':
  194. if (part > 2)
  195. {
  196. continue_clause = true;
  197. i--;
  198. part = 2;
  199. goto clause_done;
  200. }
  201. set_mode = 3;
  202. part = 2;
  203. break;
  204. /* Permissions: rwxXst - for ugo see above. */
  205. case 'r':
  206. if (part != 2 && part != 3)
  207. return 1;
  208. rwxXstugo[0] = true;
  209. part = 3;
  210. break;
  211. case 'w':
  212. if (part != 2 && part != 3)
  213. return 1;
  214. rwxXstugo[1] = true;
  215. part = 3;
  216. break;
  217. case 'x':
  218. if (part != 2 && part != 3)
  219. return 1;
  220. rwxXstugo[2] = true;
  221. part = 3;
  222. break;
  223. case 'X':
  224. if (part != 2 && part != 3)
  225. return 1;
  226. rwxXstugo[3] = true;
  227. part = 3;
  228. break;
  229. case 's':
  230. if (part != 2 && part != 3)
  231. return 1;
  232. rwxXstugo[4] = true;
  233. part = 3;
  234. break;
  235. case 't':
  236. if (part != 2 && part != 3)
  237. return 1;
  238. rwxXstugo[5] = true;
  239. part = 3;
  240. break;
  241. /* Trailing blanks are valid in Fortran. */
  242. case ' ':
  243. for (i++; i < mode_len; i++)
  244. if (mode[i] != ' ')
  245. break;
  246. if (i != mode_len)
  247. return 1;
  248. goto clause_done;
  249. case ',':
  250. goto clause_done;
  251. default:
  252. return 1;
  253. }
  254. }
  255. clause_done:
  256. if (part < 2)
  257. return 1;
  258. new_mode = 0;
  259. #ifdef __MINGW32__
  260. /* Read. */
  261. if (rwxXstugo[0] && (ugo[0] || honor_umask))
  262. new_mode |= _S_IREAD;
  263. /* Write. */
  264. if (rwxXstugo[1] && (ugo[0] || honor_umask))
  265. new_mode |= _S_IWRITE;
  266. #else
  267. /* Read. */
  268. if (rwxXstugo[0])
  269. {
  270. if (ugo[0] || honor_umask)
  271. new_mode |= S_IRUSR;
  272. if (ugo[1] || honor_umask)
  273. new_mode |= S_IRGRP;
  274. if (ugo[2] || honor_umask)
  275. new_mode |= S_IROTH;
  276. }
  277. /* Write. */
  278. if (rwxXstugo[1])
  279. {
  280. if (ugo[0] || honor_umask)
  281. new_mode |= S_IWUSR;
  282. if (ugo[1] || honor_umask)
  283. new_mode |= S_IWGRP;
  284. if (ugo[2] || honor_umask)
  285. new_mode |= S_IWOTH;
  286. }
  287. /* Execute. */
  288. if (rwxXstugo[2])
  289. {
  290. if (ugo[0] || honor_umask)
  291. new_mode |= S_IXUSR;
  292. if (ugo[1] || honor_umask)
  293. new_mode |= S_IXGRP;
  294. if (ugo[2] || honor_umask)
  295. new_mode |= S_IXOTH;
  296. }
  297. /* 'X' execute. */
  298. if (rwxXstugo[3]
  299. && (is_dir || (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
  300. new_mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
  301. /* 's'. */
  302. if (rwxXstugo[4])
  303. {
  304. if (ugo[0] || honor_umask)
  305. new_mode |= S_ISUID;
  306. if (ugo[1] || honor_umask)
  307. new_mode |= S_ISGID;
  308. }
  309. /* As original 'u'. */
  310. if (rwxXstugo[6])
  311. {
  312. if (ugo[1] || honor_umask)
  313. {
  314. if (file_mode & S_IRUSR)
  315. new_mode |= S_IRGRP;
  316. if (file_mode & S_IWUSR)
  317. new_mode |= S_IWGRP;
  318. if (file_mode & S_IXUSR)
  319. new_mode |= S_IXGRP;
  320. }
  321. if (ugo[2] || honor_umask)
  322. {
  323. if (file_mode & S_IRUSR)
  324. new_mode |= S_IROTH;
  325. if (file_mode & S_IWUSR)
  326. new_mode |= S_IWOTH;
  327. if (file_mode & S_IXUSR)
  328. new_mode |= S_IXOTH;
  329. }
  330. }
  331. /* As original 'g'. */
  332. if (rwxXstugo[7])
  333. {
  334. if (ugo[0] || honor_umask)
  335. {
  336. if (file_mode & S_IRGRP)
  337. new_mode |= S_IRUSR;
  338. if (file_mode & S_IWGRP)
  339. new_mode |= S_IWUSR;
  340. if (file_mode & S_IXGRP)
  341. new_mode |= S_IXUSR;
  342. }
  343. if (ugo[2] || honor_umask)
  344. {
  345. if (file_mode & S_IRGRP)
  346. new_mode |= S_IROTH;
  347. if (file_mode & S_IWGRP)
  348. new_mode |= S_IWOTH;
  349. if (file_mode & S_IXGRP)
  350. new_mode |= S_IXOTH;
  351. }
  352. }
  353. /* As original 'o'. */
  354. if (rwxXstugo[8])
  355. {
  356. if (ugo[0] || honor_umask)
  357. {
  358. if (file_mode & S_IROTH)
  359. new_mode |= S_IRUSR;
  360. if (file_mode & S_IWOTH)
  361. new_mode |= S_IWUSR;
  362. if (file_mode & S_IXOTH)
  363. new_mode |= S_IXUSR;
  364. }
  365. if (ugo[1] || honor_umask)
  366. {
  367. if (file_mode & S_IROTH)
  368. new_mode |= S_IRGRP;
  369. if (file_mode & S_IWOTH)
  370. new_mode |= S_IWGRP;
  371. if (file_mode & S_IXOTH)
  372. new_mode |= S_IXGRP;
  373. }
  374. }
  375. #endif /* __MINGW32__ */
  376. #ifdef HAVE_UMASK
  377. if (honor_umask)
  378. new_mode &= ~mode_mask;
  379. #endif
  380. if (set_mode == 1)
  381. {
  382. #ifdef __MINGW32__
  383. if (ugo[0] || honor_umask)
  384. file_mode = (file_mode & ~(_S_IWRITE | _S_IREAD))
  385. | (new_mode & (_S_IWRITE | _S_IREAD));
  386. #else
  387. /* Set '='. */
  388. if ((ugo[0] || honor_umask) && !rwxXstugo[6])
  389. file_mode = (file_mode & ~(S_ISUID | S_IRUSR | S_IWUSR | S_IXUSR))
  390. | (new_mode & (S_ISUID | S_IRUSR | S_IWUSR | S_IXUSR));
  391. if ((ugo[1] || honor_umask) && !rwxXstugo[7])
  392. file_mode = (file_mode & ~(S_ISGID | S_IRGRP | S_IWGRP | S_IXGRP))
  393. | (new_mode & (S_ISGID | S_IRGRP | S_IWGRP | S_IXGRP));
  394. if ((ugo[2] || honor_umask) && !rwxXstugo[8])
  395. file_mode = (file_mode & ~(S_IROTH | S_IWOTH | S_IXOTH))
  396. | (new_mode & (S_IROTH | S_IWOTH | S_IXOTH));
  397. #ifndef __VXWORKS__
  398. if (is_dir && rwxXstugo[5])
  399. file_mode |= S_ISVTX;
  400. else if (!is_dir)
  401. file_mode &= ~S_ISVTX;
  402. #endif
  403. #endif
  404. }
  405. else if (set_mode == 2)
  406. {
  407. /* Clear '-'. */
  408. file_mode &= ~new_mode;
  409. #if !defined( __MINGW32__) && !defined (__VXWORKS__)
  410. if (rwxXstugo[5] || !is_dir)
  411. file_mode &= ~S_ISVTX;
  412. #endif
  413. }
  414. else if (set_mode == 3)
  415. {
  416. file_mode |= new_mode;
  417. #if !defined (__MINGW32__) && !defined (__VXWORKS__)
  418. if (rwxXstugo[5] && is_dir)
  419. file_mode |= S_ISVTX;
  420. else if (!is_dir)
  421. file_mode &= ~S_ISVTX;
  422. #endif
  423. }
  424. }
  425. return chmod (file, file_mode);
  426. }
  427. extern int chmod_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
  428. export_proto(chmod_func);
  429. int
  430. chmod_func (char *name, char *mode, gfc_charlen_type name_len,
  431. gfc_charlen_type mode_len)
  432. {
  433. char *cname = fc_strdup (name, name_len);
  434. int ret = chmod_internal (cname, mode, mode_len);
  435. free (cname);
  436. return ret;
  437. }
  438. extern void chmod_i4_sub (char *, char *, GFC_INTEGER_4 *,
  439. gfc_charlen_type, gfc_charlen_type);
  440. export_proto(chmod_i4_sub);
  441. void
  442. chmod_i4_sub (char *name, char *mode, GFC_INTEGER_4 * status,
  443. gfc_charlen_type name_len, gfc_charlen_type mode_len)
  444. {
  445. int val;
  446. val = chmod_func (name, mode, name_len, mode_len);
  447. if (status)
  448. *status = val;
  449. }
  450. extern void chmod_i8_sub (char *, char *, GFC_INTEGER_8 *,
  451. gfc_charlen_type, gfc_charlen_type);
  452. export_proto(chmod_i8_sub);
  453. void
  454. chmod_i8_sub (char *name, char *mode, GFC_INTEGER_8 * status,
  455. gfc_charlen_type name_len, gfc_charlen_type mode_len)
  456. {
  457. int val;
  458. val = chmod_func (name, mode, name_len, mode_len);
  459. if (status)
  460. *status = val;
  461. }
  462. #endif