fixed doxygen.cfg, excluded directory /tests/ from doxygen path's
[midnight-commander.git] / src / filemanager / chmod.c
blobc2e435cbe7daeca5244b98b48882829fc176e74e
1 /*
2 Chmod command -- for the Midnight Commander
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
5 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file chmod.c
25 * \brief Source: chmod command
28 #include <config.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/skin.h"
39 #include "lib/vfs/vfs.h"
40 #include "lib/strutil.h"
41 #include "lib/util.h"
42 #include "lib/widget.h"
43 #include "lib/keybind.h" /* CK_Cancel */
45 #include "midnight.h" /* current_panel */
46 #include "chmod.h"
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 #define PX 3
53 #define PY 2
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)
60 /*** file scope type declarations ****************************************************************/
62 /*** file scope variables ************************************************************************/
64 static gboolean single_set;
66 static gboolean mode_change, need_update, end_chmod;
67 static int c_file;
69 static mode_t and_mask, or_mask, c_stat;
71 static WLabel *statl;
72 static WGroupbox *file_gb;
74 static struct
76 mode_t mode;
77 const char *text;
78 gboolean selected;
79 WCheck *check;
80 } check_perm[] =
82 /* *INDENT-OFF* */
83 { S_IXOTH, N_("execute/search by others"), FALSE, NULL },
84 { S_IWOTH, N_("write by others"), FALSE, NULL },
85 { S_IROTH, N_("read by others"), FALSE, NULL },
86 { S_IXGRP, N_("execute/search by group"), FALSE, NULL },
87 { S_IWGRP, N_("write by group"), FALSE, NULL },
88 { S_IRGRP, N_("read by group"), FALSE, NULL },
89 { S_IXUSR, N_("execute/search by owner"), FALSE, NULL },
90 { S_IWUSR, N_("write by owner"), FALSE, NULL },
91 { S_IRUSR, N_("read by owner"), FALSE, NULL },
92 { S_ISVTX, N_("sticky bit"), FALSE, NULL },
93 { S_ISGID, N_("set group ID on execution"), FALSE, NULL },
94 { S_ISUID, N_("set user ID on execution"), FALSE, NULL }
95 /* *INDENT-ON* */
98 static const unsigned int check_perm_num = G_N_ELEMENTS (check_perm);
99 static int check_perm_len = 0;
101 static const char *file_info_labels[] = {
102 N_("Name:"),
103 N_("Permissions (octal):"),
104 N_("Owner name:"),
105 N_("Group name:")
108 static const unsigned int file_info_labels_num = G_N_ELEMENTS (file_info_labels);
109 static int file_info_labels_len = 0;
111 static struct
113 int ret_cmd;
114 int flags;
115 int y; /* vertical position relatively to dialog bottom boundary */
116 int len;
117 const char *text;
118 } chmod_but[] =
120 /* *INDENT-OFF* */
121 { B_CANCEL, NORMAL_BUTTON, 3, 0, N_("&Cancel") },
122 { B_ENTER, DEFPUSH_BUTTON, 3, 0, N_("&Set") },
123 { B_CLRMRK, NORMAL_BUTTON, 5, 0, N_("C&lear marked") },
124 { B_SETMRK, NORMAL_BUTTON, 5, 0, N_("S&et marked") },
125 { B_MARKED, NORMAL_BUTTON, 6, 0, N_("&Marked all") },
126 { B_ALL, NORMAL_BUTTON, 6, 0, N_("Set &all") }
127 /* *INDENT-ON* */
130 static const unsigned int chmod_but_num = G_N_ELEMENTS (chmod_but);
132 /*** file scope functions ************************************************************************/
133 /* --------------------------------------------------------------------------------------------- */
135 static void
136 chmod_i18n (void)
138 static gboolean i18n = FALSE;
139 unsigned int i;
140 int len;
142 if (i18n)
143 return;
145 i18n = TRUE;
147 #ifdef ENABLE_NLS
148 for (i = 0; i < check_perm_num; i++)
149 check_perm[i].text = _(check_perm[i].text);
151 for (i = 0; i < file_info_labels_num; i++)
152 file_info_labels[i] = _(file_info_labels[i]);
154 for (i = 0; i < chmod_but_num; i++)
155 chmod_but[i].text = _(chmod_but[i].text);
156 #endif /* ENABLE_NLS */
158 for (i = 0; i < check_perm_num; i++)
160 len = str_term_width1 (check_perm[i].text);
161 check_perm_len = max (check_perm_len, len);
164 check_perm_len += 1 + 3 + 1; /* mark, [x] and space */
166 for (i = 0; i < file_info_labels_num; i++)
168 len = str_term_width1 (file_info_labels[i]) + 2; /* spaces around */
169 file_info_labels_len = max (file_info_labels_len, len);
172 for (i = 0; i < chmod_but_num; i++)
174 chmod_but[i].len = str_term_width1 (chmod_but[i].text) + 3; /* [], spaces and w/o & */
175 if (chmod_but[i].flags == DEFPUSH_BUTTON)
176 chmod_but[i].len += 2; /* <> */
180 /* --------------------------------------------------------------------------------------------- */
182 static void
183 chmod_toggle_select (Dlg_head * h, int Id)
185 tty_setcolor (COLOR_NORMAL);
186 check_perm[Id].selected = !check_perm[Id].selected;
188 dlg_move (h, PY + check_perm_num - Id, PX + 1);
189 tty_print_char (check_perm[Id].selected ? '*' : ' ');
190 dlg_move (h, PY + check_perm_num - Id, PX + 3);
193 /* --------------------------------------------------------------------------------------------- */
195 static void
196 chmod_refresh (Dlg_head * h)
198 int y = file_gb->widget.y + 1;
199 int x = file_gb->widget.x + 2;
201 common_dialog_repaint (h);
203 tty_setcolor (COLOR_NORMAL);
205 tty_gotoyx (y, x);
206 tty_print_string (file_info_labels[0]);
207 tty_gotoyx (y + 2, x);
208 tty_print_string (file_info_labels[1]);
209 tty_gotoyx (y + 4, x);
210 tty_print_string (file_info_labels[2]);
211 tty_gotoyx (y + 6, x);
212 tty_print_string (file_info_labels[3]);
215 /* --------------------------------------------------------------------------------------------- */
217 static cb_ret_t
218 chmod_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
220 char buffer[BUF_TINY];
221 int id;
223 id = dlg_get_current_widget_id (h) - (chmod_but_num - (single_set ? 4 : 0)) - 1;
225 switch (msg)
227 case DLG_ACTION:
228 /* close dialog due to SIGINT (ctrl-g) */
229 if (sender == NULL && parm == CK_Cancel)
230 return MSG_NOT_HANDLED;
232 /* handle checkboxes */
233 if (id >= 0)
235 c_stat ^= check_perm[id].mode;
236 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
237 label_set_text (statl, buffer);
238 chmod_toggle_select (h, id);
239 mode_change = TRUE;
240 return MSG_HANDLED;
243 return MSG_NOT_HANDLED;
245 case DLG_KEY:
246 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
248 chmod_toggle_select (h, id);
249 if (parm == KEY_IC)
250 dlg_one_down (h);
251 return MSG_HANDLED;
253 return MSG_NOT_HANDLED;
255 case DLG_DRAW:
256 chmod_refresh (h);
257 return MSG_HANDLED;
259 default:
260 return default_dlg_callback (h, sender, msg, parm, data);
264 /* --------------------------------------------------------------------------------------------- */
266 static Dlg_head *
267 init_chmod (const char *fname, const struct stat *sf_stat)
269 Dlg_head *ch_dlg;
270 int lines, cols;
271 int perm_gb_len;
272 int file_gb_len;
273 unsigned int i;
274 const char *c_fname, *c_fown, *c_fgrp;
275 char buffer[BUF_TINY];
277 single_set = (current_panel->marked < 2);
278 perm_gb_len = check_perm_len + 2;
279 file_gb_len = file_info_labels_len + 2;
280 cols = str_term_width1 (fname) + 2 + 1;
281 file_gb_len = max (file_gb_len, cols);
283 lines = single_set ? 20 : 23;
284 cols = perm_gb_len + file_gb_len + 1 + 6;
286 if (cols > COLS)
288 /* shrink the right groupbox */
289 cols = COLS;
290 file_gb_len = cols - (perm_gb_len + 1 + 6);
293 ch_dlg =
294 create_dlg (TRUE, 0, 0, lines, cols, dialog_colors,
295 chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
297 for (i = 0; i < chmod_but_num; i++)
299 add_widget (ch_dlg,
300 button_new (lines - chmod_but[i].y, ch_dlg->cols / 2 + 1,
301 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0));
303 i++;
305 add_widget (ch_dlg,
306 button_new (lines - chmod_but[i].y, ch_dlg->cols / 2 - chmod_but[i].len,
307 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0));
309 if (single_set)
310 break;
313 file_gb = groupbox_new (PY, PX + perm_gb_len + 1, check_perm_num + 2, file_gb_len, _("File"));
314 add_widget (ch_dlg, file_gb);
316 for (i = 0; i < check_perm_num; i++)
318 check_perm[i].check = check_new (PY + (check_perm_num - i), PX + 2,
319 (c_stat & check_perm[i].mode) != 0 ? 1 : 0,
320 check_perm[i].text);
321 add_widget (ch_dlg, check_perm[i].check);
324 add_widget (ch_dlg, groupbox_new (PY, PX, check_perm_num + 2, perm_gb_len, _("Permission")));
326 /* Set the labels */
327 /* Do this at end to have a widget id in a simple way */
328 lines = PY + 2;
329 cols = PX + perm_gb_len + 3;
330 c_fname = str_trunc (fname, file_gb_len - 3);
331 add_widget (ch_dlg, label_new (lines, cols, c_fname));
332 c_fown = str_trunc (get_owner (sf_stat->st_uid), file_gb_len - 3);
333 add_widget (ch_dlg, label_new (lines + 4, cols, c_fown));
334 c_fgrp = str_trunc (get_group (sf_stat->st_gid), file_gb_len - 3);
335 add_widget (ch_dlg, label_new (lines + 6, cols, c_fgrp));
336 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
337 statl = label_new (lines + 2, cols, buffer);
338 add_widget (ch_dlg, statl);
340 return ch_dlg;
343 /* --------------------------------------------------------------------------------------------- */
345 static void
346 chmod_done (void)
348 if (need_update)
349 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
350 repaint_screen ();
353 /* --------------------------------------------------------------------------------------------- */
355 static char *
356 next_file (void)
358 while (!current_panel->dir.list[c_file].f.marked)
359 c_file++;
361 return current_panel->dir.list[c_file].fname;
364 /* --------------------------------------------------------------------------------------------- */
366 static void
367 do_chmod (struct stat *sf)
369 vfs_path_t *vpath;
370 sf->st_mode &= and_mask;
371 sf->st_mode |= or_mask;
373 vpath = vfs_path_from_str (current_panel->dir.list[c_file].fname);
374 if (mc_chmod (vpath, sf->st_mode) == -1)
375 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
376 current_panel->dir.list[c_file].fname, unix_error_string (errno));
378 vfs_path_free (vpath);
379 do_file_mark (current_panel, c_file, 0);
382 /* --------------------------------------------------------------------------------------------- */
384 static void
385 apply_mask (struct stat *sf)
387 need_update = TRUE;
388 end_chmod = TRUE;
390 do_chmod (sf);
394 char *fname;
395 vfs_path_t *vpath;
396 gboolean ok;
398 fname = next_file ();
399 vpath = vfs_path_from_str (fname);
400 ok = (mc_stat (vpath, sf) == 0);
401 vfs_path_free (vpath);
402 if (!ok)
403 return;
405 c_stat = sf->st_mode;
407 do_chmod (sf);
409 while (current_panel->marked != 0);
412 /* --------------------------------------------------------------------------------------------- */
413 /*** public functions ****************************************************************************/
414 /* --------------------------------------------------------------------------------------------- */
416 void
417 chmod_cmd (void)
419 chmod_i18n ();
422 { /* do while any files remaining */
423 vfs_path_t *vpath;
424 Dlg_head *ch_dlg;
425 struct stat sf_stat;
426 char *fname;
427 int result;
428 unsigned int i;
430 do_refresh ();
432 mode_change = FALSE;
433 need_update = FALSE;
434 end_chmod = FALSE;
435 c_file = 0;
437 if (current_panel->marked != 0)
438 fname = next_file (); /* next marked file */
439 else
440 fname = selection (current_panel)->fname; /* single file */
442 vpath = vfs_path_from_str (fname);
444 if (mc_stat (vpath, &sf_stat) != 0)
446 vfs_path_free (vpath);
447 break;
450 c_stat = sf_stat.st_mode;
452 ch_dlg = init_chmod (fname, &sf_stat);
454 /* do action */
455 result = run_dlg (ch_dlg);
457 switch (result)
459 case B_ENTER:
460 if (mode_change && mc_chmod (vpath, c_stat) == -1)
461 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
462 fname, unix_error_string (errno));
463 need_update = TRUE;
464 break;
466 case B_CANCEL:
467 end_chmod = TRUE;
468 break;
470 case B_ALL:
471 case B_MARKED:
472 and_mask = or_mask = 0;
473 and_mask = ~and_mask;
475 for (i = 0; i < check_perm_num; i++)
476 if (check_perm[i].selected || result == B_ALL)
478 if (check_perm[i].check->state & C_BOOL)
479 or_mask |= check_perm[i].mode;
480 else
481 and_mask &= ~check_perm[i].mode;
484 apply_mask (&sf_stat);
485 break;
487 case B_SETMRK:
488 and_mask = or_mask = 0;
489 and_mask = ~and_mask;
491 for (i = 0; i < check_perm_num; i++)
492 if (check_perm[i].selected)
493 or_mask |= check_perm[i].mode;
495 apply_mask (&sf_stat);
496 break;
498 case B_CLRMRK:
499 and_mask = or_mask = 0;
500 and_mask = ~and_mask;
502 for (i = 0; i < check_perm_num; i++)
503 if (check_perm[i].selected)
504 and_mask &= ~check_perm[i].mode;
506 apply_mask (&sf_stat);
507 break;
510 if (current_panel->marked != 0 && result != B_CANCEL)
512 do_file_mark (current_panel, c_file, 0);
513 need_update = TRUE;
516 vfs_path_free (vpath);
518 destroy_dlg (ch_dlg);
520 while (current_panel->marked != 0 && !end_chmod);
522 chmod_done ();
525 /* --------------------------------------------------------------------------------------------- */