chmod.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* Chmod command -- for the Midnight Commander
  2. Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
  3. Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. */
  16. /** \file chmod.c
  17. * \brief Source: chmod command
  18. */
  19. #include <config.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include "lib/global.h"
  27. #include "lib/tty/tty.h"
  28. #include "lib/skin.h"
  29. #include "lib/vfs/vfs.h"
  30. #include "lib/strutil.h"
  31. #include "lib/util.h"
  32. #include "lib/widget.h"
  33. #include "midnight.h" /* current_panel */
  34. #include "chmod.h"
  35. /*** global variables ****************************************************************************/
  36. /*** file scope macro definitions ****************************************************************/
  37. #define PX 5
  38. #define PY 2
  39. #define FX 40
  40. #define FY 2
  41. #define BX 6
  42. #define BY 17
  43. #define TX 40
  44. #define TY 12
  45. #define PERMISSIONS 12
  46. #define BUTTONS 6
  47. #define B_MARKED B_USER
  48. #define B_ALL (B_USER+1)
  49. #define B_SETMRK (B_USER+2)
  50. #define B_CLRMRK (B_USER+3)
  51. /*** file scope type declarations ****************************************************************/
  52. /*** file scope variables ************************************************************************/
  53. static int single_set;
  54. static int mode_change, need_update;
  55. static int c_file, end_chmod;
  56. static mode_t and_mask, or_mask, c_stat;
  57. /* FIXME: these variables are superfluous, aren't they? (hint: str_trunc
  58. * returns a pointer to a static buffer, and label_new creates its own copy
  59. * of its argument)
  60. * --rillig, 2004-08-29 */
  61. static const char *c_fname, *c_fown, *c_fgrp;
  62. static WLabel *statl;
  63. static struct
  64. {
  65. mode_t mode;
  66. const char *text;
  67. int selected;
  68. WCheck *check;
  69. } check_perm[PERMISSIONS] =
  70. {
  71. /* *INDENT-OFF* */
  72. { S_IXOTH, N_("execute/search by others"), 0, 0 },
  73. { S_IWOTH, N_("write by others"), 0, 0 },
  74. { S_IROTH, N_("read by others"), 0, 0 },
  75. { S_IXGRP, N_("execute/search by group"), 0, 0 },
  76. { S_IWGRP, N_("write by group"), 0, 0 },
  77. { S_IRGRP, N_("read by group"), 0, 0 },
  78. { S_IXUSR, N_("execute/search by owner"), 0, 0 },
  79. { S_IWUSR, N_("write by owner"), 0, 0 },
  80. { S_IRUSR, N_("read by owner"), 0, 0 },
  81. { S_ISVTX, N_("sticky bit"), 0, 0 },
  82. { S_ISGID, N_("set group ID on execution"), 0, 0 },
  83. { S_ISUID, N_("set user ID on execution"), 0, 0 }
  84. /* *INDENT-ON* */
  85. };
  86. static struct
  87. {
  88. int ret_cmd, flags, y, x;
  89. const char *text;
  90. } chmod_but[BUTTONS] =
  91. {
  92. /* *INDENT-OFF* */
  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. /* *INDENT-ON* */
  100. };
  101. /*** file scope functions ************************************************************************/
  102. /* --------------------------------------------------------------------------------------------- */
  103. static void
  104. chmod_toggle_select (Dlg_head * h, int Id)
  105. {
  106. tty_setcolor (COLOR_NORMAL);
  107. check_perm[Id].selected ^= 1;
  108. dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
  109. tty_print_char ((check_perm[Id].selected) ? '*' : ' ');
  110. dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
  111. }
  112. /* --------------------------------------------------------------------------------------------- */
  113. static void
  114. chmod_refresh (Dlg_head * h)
  115. {
  116. common_dialog_repaint (h);
  117. tty_setcolor (COLOR_NORMAL);
  118. dlg_move (h, FY + 1, FX + 2);
  119. tty_print_string (_("Name"));
  120. dlg_move (h, FY + 3, FX + 2);
  121. tty_print_string (_("Permissions (Octal)"));
  122. dlg_move (h, FY + 5, FX + 2);
  123. tty_print_string (_("Owner name"));
  124. dlg_move (h, FY + 7, FX + 2);
  125. tty_print_string (_("Group name"));
  126. dlg_move (h, TY, TX);
  127. tty_print_string (_("Use SPACE to change"));
  128. dlg_move (h, TY + 1, TX);
  129. tty_print_string (_("an option, ARROW KEYS"));
  130. dlg_move (h, TY + 2, TX);
  131. tty_print_string (_("to move between options"));
  132. dlg_move (h, TY + 3, TX);
  133. tty_print_string (_("and T or INS to mark"));
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. static cb_ret_t
  137. chmod_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
  138. {
  139. char buffer[BUF_TINY];
  140. int id = dlg_get_current_widget_id (h) - BUTTONS + single_set * 2 - 1;
  141. switch (msg)
  142. {
  143. case DLG_ACTION:
  144. if (id >= 0)
  145. {
  146. c_stat ^= check_perm[id].mode;
  147. g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
  148. label_set_text (statl, buffer);
  149. chmod_toggle_select (h, id);
  150. mode_change = 1;
  151. }
  152. return MSG_HANDLED;
  153. case DLG_KEY:
  154. if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
  155. {
  156. chmod_toggle_select (h, id);
  157. if (parm == KEY_IC)
  158. dlg_one_down (h);
  159. return MSG_HANDLED;
  160. }
  161. return MSG_NOT_HANDLED;
  162. case DLG_DRAW:
  163. chmod_refresh (h);
  164. return MSG_HANDLED;
  165. default:
  166. return default_dlg_callback (h, sender, msg, parm, data);
  167. }
  168. }
  169. /* --------------------------------------------------------------------------------------------- */
  170. static Dlg_head *
  171. init_chmod (void)
  172. {
  173. int i;
  174. Dlg_head *ch_dlg;
  175. do_refresh ();
  176. end_chmod = c_file = need_update = 0;
  177. single_set = (current_panel->marked < 2) ? 2 : 0;
  178. ch_dlg =
  179. create_dlg (TRUE, 0, 0, 22 - single_set, 70, dialog_colors,
  180. chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
  181. for (i = 0; i < BUTTONS; i++)
  182. {
  183. if (i == 2 && single_set)
  184. break;
  185. else
  186. add_widget (ch_dlg,
  187. button_new (BY + chmod_but[i].y - single_set,
  188. BX + chmod_but[i].x,
  189. chmod_but[i].ret_cmd,
  190. chmod_but[i].flags, _(chmod_but[i].text), 0));
  191. }
  192. add_widget (ch_dlg, groupbox_new (FY, FX, 10, 25, _("File")));
  193. for (i = 0; i < PERMISSIONS; i++)
  194. {
  195. check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2, 0, _(check_perm[i].text));
  196. add_widget (ch_dlg, check_perm[i].check);
  197. }
  198. add_widget (ch_dlg, groupbox_new (PY, PX, PERMISSIONS + 2, 33, _("Permission")));
  199. return ch_dlg;
  200. }
  201. /* --------------------------------------------------------------------------------------------- */
  202. static void
  203. chmod_done (void)
  204. {
  205. if (need_update)
  206. update_panels (UP_OPTIMIZE, UP_KEEPSEL);
  207. repaint_screen ();
  208. }
  209. /* --------------------------------------------------------------------------------------------- */
  210. static char *
  211. next_file (void)
  212. {
  213. while (!current_panel->dir.list[c_file].f.marked)
  214. c_file++;
  215. return current_panel->dir.list[c_file].fname;
  216. }
  217. /* --------------------------------------------------------------------------------------------- */
  218. static void
  219. do_chmod (struct stat *sf)
  220. {
  221. sf->st_mode &= and_mask;
  222. sf->st_mode |= or_mask;
  223. if (mc_chmod (current_panel->dir.list[c_file].fname, sf->st_mode) == -1)
  224. message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
  225. current_panel->dir.list[c_file].fname, unix_error_string (errno));
  226. do_file_mark (current_panel, c_file, 0);
  227. }
  228. /* --------------------------------------------------------------------------------------------- */
  229. static void
  230. apply_mask (struct stat *sf)
  231. {
  232. char *fname;
  233. need_update = end_chmod = 1;
  234. do_chmod (sf);
  235. do
  236. {
  237. fname = next_file ();
  238. if (mc_stat (fname, sf) != 0)
  239. return;
  240. c_stat = sf->st_mode;
  241. do_chmod (sf);
  242. }
  243. while (current_panel->marked);
  244. }
  245. /* --------------------------------------------------------------------------------------------- */
  246. /*** public functions ****************************************************************************/
  247. /* --------------------------------------------------------------------------------------------- */
  248. void
  249. chmod_cmd (void)
  250. {
  251. char buffer[BUF_TINY];
  252. char *fname;
  253. int i;
  254. struct stat sf_stat;
  255. Dlg_head *ch_dlg;
  256. do
  257. { /* do while any files remaining */
  258. ch_dlg = init_chmod ();
  259. if (current_panel->marked)
  260. fname = next_file (); /* next marked file */
  261. else
  262. fname = selection (current_panel)->fname; /* single file */
  263. if (mc_stat (fname, &sf_stat) != 0)
  264. { /* get status of file */
  265. destroy_dlg (ch_dlg);
  266. break;
  267. }
  268. c_stat = sf_stat.st_mode;
  269. mode_change = 0; /* clear changes flag */
  270. /* set check buttons */
  271. for (i = 0; i < PERMISSIONS; i++)
  272. {
  273. check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
  274. check_perm[i].selected = 0;
  275. }
  276. /* Set the labels */
  277. c_fname = str_trunc (fname, 21);
  278. add_widget (ch_dlg, label_new (FY + 2, FX + 2, c_fname));
  279. c_fown = str_trunc (get_owner (sf_stat.st_uid), 21);
  280. add_widget (ch_dlg, label_new (FY + 6, FX + 2, c_fown));
  281. c_fgrp = str_trunc (get_group (sf_stat.st_gid), 21);
  282. add_widget (ch_dlg, label_new (FY + 8, FX + 2, c_fgrp));
  283. g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
  284. statl = label_new (FY + 4, FX + 2, buffer);
  285. add_widget (ch_dlg, statl);
  286. run_dlg (ch_dlg); /* retrieve an action */
  287. /* do action */
  288. switch (ch_dlg->ret_value)
  289. {
  290. case B_ENTER:
  291. if (mode_change)
  292. if (mc_chmod (fname, c_stat) == -1)
  293. message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
  294. fname, unix_error_string (errno));
  295. need_update = 1;
  296. break;
  297. case B_CANCEL:
  298. end_chmod = 1;
  299. break;
  300. case B_ALL:
  301. case B_MARKED:
  302. and_mask = or_mask = 0;
  303. and_mask = ~and_mask;
  304. for (i = 0; i < PERMISSIONS; i++)
  305. {
  306. if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
  307. {
  308. if (check_perm[i].check->state & C_BOOL)
  309. or_mask |= check_perm[i].mode;
  310. else
  311. and_mask &= ~check_perm[i].mode;
  312. }
  313. }
  314. apply_mask (&sf_stat);
  315. break;
  316. case B_SETMRK:
  317. and_mask = or_mask = 0;
  318. and_mask = ~and_mask;
  319. for (i = 0; i < PERMISSIONS; i++)
  320. {
  321. if (check_perm[i].selected)
  322. or_mask |= check_perm[i].mode;
  323. }
  324. apply_mask (&sf_stat);
  325. break;
  326. case B_CLRMRK:
  327. and_mask = or_mask = 0;
  328. and_mask = ~and_mask;
  329. for (i = 0; i < PERMISSIONS; i++)
  330. {
  331. if (check_perm[i].selected)
  332. and_mask &= ~check_perm[i].mode;
  333. }
  334. apply_mask (&sf_stat);
  335. break;
  336. }
  337. if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
  338. {
  339. do_file_mark (current_panel, c_file, 0);
  340. need_update = 1;
  341. }
  342. destroy_dlg (ch_dlg);
  343. }
  344. while (current_panel->marked && !end_chmod);
  345. chmod_done ();
  346. }
  347. /* --------------------------------------------------------------------------------------------- */