chmod.c 12 KB

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