chmod.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* Chmod command -- for the Midnight Commander
  2. Copyright (C) 1994 Radek Doulik
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <config.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <errno.h> /* For errno on SunOS systems */
  19. /* Needed for the extern declarations of integer parameters */
  20. #include <sys/types.h>
  21. #include <sys/param.h>
  22. #include <sys/stat.h>
  23. #include <grp.h>
  24. #include <pwd.h>
  25. #ifdef HAVE_UNISTD_H
  26. # include <unistd.h>
  27. #endif
  28. #include "tty.h"
  29. #include "global.h"
  30. #include "win.h"
  31. #include "color.h"
  32. #include "dlg.h"
  33. #include "widget.h"
  34. #include "dialog.h" /* For do_refresh() */
  35. #include "dir.h"
  36. #include "panel.h" /* Needed for the externs */
  37. #include "file.h"
  38. #include "main.h"
  39. #include "chmod.h"
  40. #include "achown.h"
  41. #include "chown.h"
  42. #include "../vfs/vfs.h"
  43. static int single_set;
  44. struct Dlg_head *ch_dlg;
  45. #define PX 5
  46. #define PY 2
  47. #define FX 40
  48. #define FY 2
  49. #define BX 6
  50. #define BY 17
  51. #define TX 40
  52. #define TY 12
  53. #define PERMISSIONS 12
  54. #define BUTTONS 6
  55. #define B_MARKED B_USER
  56. #define B_ALL B_USER+1
  57. #define B_SETMRK B_USER+2
  58. #define B_CLRMRK B_USER+3
  59. int mode_change, need_update;
  60. int c_file, end_chmod;
  61. umode_t and_mask, or_mask, c_stat;
  62. char *c_fname, *c_fown, *c_fgrp, *c_fperm;
  63. int c_fsize;
  64. static WLabel *statl;
  65. static int normal_color;
  66. static int title_color;
  67. static int selection_color;
  68. struct {
  69. mode_t mode;
  70. char *text;
  71. int selected;
  72. WCheck *check;
  73. } check_perm[PERMISSIONS] =
  74. {
  75. { S_IXOTH, N_("execute/search by others"), 0, 0, },
  76. { S_IWOTH, N_("write by others"), 0, 0, },
  77. { S_IROTH, N_("read by others"), 0, 0, },
  78. { S_IXGRP, N_("execute/search by group"), 0, 0, },
  79. { S_IWGRP, N_("write by group"), 0, 0, },
  80. { S_IRGRP, N_("read by group"), 0, 0, },
  81. { S_IXUSR, N_("execute/search by owner"), 0, 0, },
  82. { S_IWUSR, N_("write by owner"), 0, 0, },
  83. { S_IRUSR, N_("read by owner"), 0, 0, },
  84. { S_ISVTX, N_("sticky bit"), 0, 0, },
  85. { S_ISGID, N_("set group ID on execution"), 0, 0, },
  86. { S_ISUID, N_("set user ID on execution"), 0, 0, },
  87. };
  88. struct {
  89. int ret_cmd, flags, y, x;
  90. char *text;
  91. } chmod_but[BUTTONS] =
  92. {
  93. { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
  94. { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
  95. { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
  96. { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
  97. { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
  98. { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
  99. };
  100. #ifdef HAVE_X
  101. static void chmod_toggle_select (void)
  102. {
  103. }
  104. #else
  105. static void chmod_toggle_select (void)
  106. {
  107. int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
  108. attrset (normal_color);
  109. check_perm[Id].selected ^= 1;
  110. dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
  111. addch ((check_perm[Id].selected) ? '*' : ' ');
  112. dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
  113. }
  114. static void chmod_refresh (void)
  115. {
  116. attrset (COLOR_NORMAL);
  117. dlg_erase (ch_dlg);
  118. draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
  119. draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
  120. draw_box (ch_dlg, FY, FX, 10, 25);
  121. dlg_move (ch_dlg, FY + 1, FX + 2);
  122. addstr (_("Name"));
  123. dlg_move (ch_dlg, FY + 3, FX + 2);
  124. addstr (_("Permissions (Octal)"));
  125. dlg_move (ch_dlg, FY + 5, FX + 2);
  126. addstr (_("Owner name"));
  127. dlg_move (ch_dlg, FY + 7, FX + 2);
  128. addstr (_("Group name"));
  129. attrset (title_color);
  130. dlg_move (ch_dlg, 1, 28);
  131. addstr (_(" Chmod command "));
  132. dlg_move (ch_dlg, PY, PX + 1);
  133. addstr (_(" Permission "));
  134. dlg_move (ch_dlg, FY, FX + 1);
  135. addstr (_(" File "));
  136. attrset (selection_color);
  137. dlg_move (ch_dlg, TY, TX);
  138. addstr (_("Use SPACE to change"));
  139. dlg_move (ch_dlg, TY + 1, TX);
  140. addstr (_("an option, ARROW KEYS"));
  141. dlg_move (ch_dlg, TY + 2, TX);
  142. addstr (_("to move between options"));
  143. dlg_move (ch_dlg, TY + 3, TX);
  144. addstr (_("and T or INS to mark"));
  145. }
  146. #endif
  147. static int chmod_callback (Dlg_head *h, int Par, int Msg)
  148. {
  149. char buffer [BUF_TINY];
  150. switch (Msg) {
  151. case DLG_ACTION:
  152. if (Par >= BUTTONS - single_set * 2){
  153. c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
  154. g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
  155. label_set_text (statl, buffer);
  156. chmod_toggle_select ();
  157. mode_change = 1;
  158. }
  159. break;
  160. case DLG_KEY:
  161. if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
  162. ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
  163. chmod_toggle_select ();
  164. if (Par == KEY_IC)
  165. dlg_one_down (ch_dlg);
  166. return 1;
  167. }
  168. break;
  169. #ifndef HAVE_X
  170. case DLG_DRAW:
  171. chmod_refresh ();
  172. break;
  173. #endif
  174. }
  175. return 0;
  176. }
  177. static void init_chmod (void)
  178. {
  179. int i;
  180. do_refresh ();
  181. end_chmod = c_file = need_update = 0;
  182. single_set = (cpanel->marked < 2) ? 2 : 0;
  183. if (use_colors){
  184. normal_color = COLOR_NORMAL;
  185. title_color = COLOR_HOT_NORMAL;
  186. selection_color = COLOR_NORMAL;
  187. } else {
  188. normal_color = NORMAL_COLOR;
  189. title_color = SELECTED_COLOR;
  190. selection_color = SELECTED_COLOR;
  191. }
  192. ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
  193. chmod_callback, "[Chmod]", "chmod", DLG_CENTER);
  194. x_set_dialog_title (ch_dlg, _("Chmod command"));
  195. #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
  196. chmod_but[i].ret_cmd, chmod_but[i].flags, _(chmod_but[i].text), 0, 0, NULL
  197. for (i = 0; i < BUTTONS; i++) {
  198. if (i == 2 && single_set)
  199. break;
  200. else
  201. add_widget (ch_dlg, button_new (XTRACT (i)));
  202. }
  203. #define XTRACT2(i) 0, _(check_perm [i].text), NULL
  204. for (i = 0; i < PERMISSIONS; i++) {
  205. check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
  206. XTRACT2 (i));
  207. add_widget (ch_dlg, check_perm[i].check);
  208. }
  209. }
  210. int stat_file (char *filename, struct stat *st)
  211. {
  212. if (mc_stat (filename, st))
  213. return 0;
  214. return 1;
  215. }
  216. static void chmod_done (void)
  217. {
  218. if (need_update)
  219. update_panels (UP_OPTIMIZE, UP_KEEPSEL);
  220. repaint_screen ();
  221. }
  222. static char *next_file (void)
  223. {
  224. while (!cpanel->dir.list[c_file].f.marked)
  225. c_file++;
  226. return cpanel->dir.list[c_file].fname;
  227. }
  228. static void do_chmod (struct stat *sf)
  229. {
  230. sf->st_mode &= and_mask;
  231. sf->st_mode |= or_mask;
  232. if (mc_chmod (cpanel->dir.list [c_file].fname, sf->st_mode) == -1)
  233. message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
  234. cpanel->dir.list [c_file].fname, unix_error_string (errno));
  235. do_file_mark (cpanel, c_file, 0);
  236. }
  237. static void apply_mask (struct stat *sf)
  238. {
  239. char *fname;
  240. need_update = end_chmod = 1;
  241. do_chmod (sf);
  242. do {
  243. fname = next_file ();
  244. if (!stat_file (fname, sf))
  245. return;
  246. c_stat = sf->st_mode;
  247. do_chmod (sf);
  248. } while (cpanel->marked);
  249. }
  250. void chmod_cmd (void)
  251. {
  252. char buffer [BUF_TINY];
  253. char *fname;
  254. int i;
  255. struct stat sf_stat;
  256. #if 0
  257. /* Don't do things like this: you do not want to enumerate all
  258. filesystems that can not support chmod, here. */
  259. if (!vfs_current_is_local ()) {
  260. if (vfs_current_is_extfs ()) {
  261. message (1, _(" Oops... "),
  262. _(" I can't run the Chmod command on an extfs "));
  263. return;
  264. } else if (vfs_current_is_tarfs ()) {
  265. message (1, _(" Oops... "),
  266. _(" I can't run the Chmod command on a tarfs "));
  267. return;
  268. }
  269. }
  270. #endif
  271. do { /* do while any files remaining */
  272. init_chmod ();
  273. if (cpanel->marked)
  274. fname = next_file (); /* next marked file */
  275. else
  276. fname = selection (cpanel)->fname; /* single file */
  277. if (!stat_file (fname, &sf_stat)){ /* get status of file */
  278. destroy_dlg (ch_dlg);
  279. break;
  280. }
  281. c_stat = sf_stat.st_mode;
  282. mode_change = 0; /* clear changes flag */
  283. /* set check buttons */
  284. for (i = 0; i < PERMISSIONS; i++){
  285. check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
  286. check_perm[i].selected = 0;
  287. }
  288. /* Set the labels */
  289. c_fname = name_trunc (fname, 21);
  290. add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
  291. c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
  292. add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
  293. c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
  294. add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
  295. g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
  296. statl = label_new (FY+4, FX+2, buffer, NULL);
  297. add_widget (ch_dlg, statl);
  298. run_dlg (ch_dlg); /* retrieve an action */
  299. /* do action */
  300. switch (ch_dlg->ret_value){
  301. case B_ENTER:
  302. if (mode_change)
  303. if (mc_chmod (fname, c_stat) == -1)
  304. message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
  305. fname, unix_error_string (errno));
  306. need_update = 1;
  307. break;
  308. case B_CANCEL:
  309. end_chmod = 1;
  310. break;
  311. case B_ALL:
  312. case B_MARKED:
  313. and_mask = or_mask = 0;
  314. and_mask = ~and_mask;
  315. for (i = 0; i < PERMISSIONS; i++) {
  316. if (check_perm[i].selected || ch_dlg->ret_value == B_ALL) {
  317. if (check_perm[i].check->state & C_BOOL)
  318. or_mask |= check_perm[i].mode;
  319. else
  320. and_mask &= ~check_perm[i].mode;
  321. }
  322. }
  323. apply_mask (&sf_stat);
  324. break;
  325. case B_SETMRK:
  326. and_mask = or_mask = 0;
  327. and_mask = ~and_mask;
  328. for (i = 0; i < PERMISSIONS; i++) {
  329. if (check_perm[i].selected)
  330. or_mask |= check_perm[i].mode;
  331. }
  332. apply_mask (&sf_stat);
  333. break;
  334. case B_CLRMRK:
  335. and_mask = or_mask = 0;
  336. and_mask = ~and_mask;
  337. for (i = 0; i < PERMISSIONS; i++) {
  338. if (check_perm[i].selected)
  339. and_mask &= ~check_perm[i].mode;
  340. }
  341. apply_mask (&sf_stat);
  342. break;
  343. }
  344. if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
  345. do_file_mark (cpanel, c_file, 0);
  346. need_update = 1;
  347. }
  348. destroy_dlg (ch_dlg);
  349. } while (cpanel->marked && !end_chmod);
  350. chmod_done ();
  351. }
  352. void ch1_cmd (int id)
  353. {
  354. if (advanced_chfns)
  355. chown_advanced_cmd ();
  356. else
  357. chmod_cmd ();
  358. }
  359. void ch2_cmd (int id)
  360. {
  361. if (advanced_chfns)
  362. chown_advanced_cmd ();
  363. else
  364. chown_cmd ();
  365. }