(file_eta_prepare_for_show): join checks of eta_secs.
[midnight-commander.git] / src / filemanager / chmod.c
blob8093d08ceb1925f911dc8b45f07b221c6d71a239
1 /*
2 Chmod command -- for the Midnight Commander
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /** \file chmod.c
24 * \brief Source: chmod command
27 #include <config.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
34 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/skin.h"
38 #include "lib/vfs/vfs.h"
39 #include "lib/strutil.h"
40 #include "lib/util.h"
41 #include "lib/widget.h"
43 #include "cmd.h" /* chmod_cmd() */
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 #define PX 3
50 #define PY 2
52 #define B_MARKED B_USER
53 #define B_SETALL (B_USER + 1)
54 #define B_SETMRK (B_USER + 2)
55 #define B_CLRMRK (B_USER + 3)
57 #define BUTTONS 6
58 #define BUTTONS_PERM 12
59 #define LABELS 4
61 /*** file scope type declarations ****************************************************************/
63 /*** forward declarations (file scope functions) *************************************************/
65 /*** file scope variables ************************************************************************/
67 static struct
69 mode_t mode;
70 const char *text;
71 gboolean selected;
72 WCheck *check;
73 } check_perm[BUTTONS_PERM] = {
74 /* *INDENT-OFF* */
75 { S_ISUID, N_("set &user ID on execution"), FALSE, NULL },
76 { S_ISGID, N_("set &group ID on execution"), FALSE, NULL },
77 { S_ISVTX, N_("stick&y bit"), FALSE, NULL },
78 { S_IRUSR, N_("&read by owner"), FALSE, NULL },
79 { S_IWUSR, N_("&write by owner"), FALSE, NULL },
80 { S_IXUSR, N_("e&xecute/search by owner"), FALSE, NULL },
81 { S_IRGRP, N_("rea&d by group"), FALSE, NULL },
82 { S_IWGRP, N_("write by grou&p"), FALSE, NULL },
83 { S_IXGRP, N_("execu&te/search by group"), FALSE, NULL },
84 { S_IROTH, N_("read &by others"), FALSE, NULL },
85 { S_IWOTH, N_("wr&ite by others"), FALSE, NULL },
86 { S_IXOTH, N_("execute/searc&h by others"), FALSE, NULL }
87 /* *INDENT-ON* */
90 static int check_perm_len = 0;
92 static const char *file_info_labels[LABELS] = {
93 N_("Name:"),
94 N_("Permissions (octal):"),
95 N_("Owner name:"),
96 N_("Group name:")
99 static int file_info_labels_len = 0;
101 static struct
103 int ret_cmd;
104 button_flags_t flags;
105 int y; /* vertical position relatively to dialog bottom boundary */
106 int len;
107 const char *text;
108 } chmod_but[BUTTONS] = {
109 /* *INDENT-OFF* */
110 { B_SETALL, NORMAL_BUTTON, 6, 0, N_("Set &all") },
111 { B_MARKED, NORMAL_BUTTON, 6, 0, N_("&Marked all") },
112 { B_SETMRK, NORMAL_BUTTON, 5, 0, N_("S&et marked") },
113 { B_CLRMRK, NORMAL_BUTTON, 5, 0, N_("C&lear marked") },
114 { B_ENTER, DEFPUSH_BUTTON, 3, 0, N_("&Set") },
115 { B_CANCEL, NORMAL_BUTTON, 3, 0, N_("&Cancel") }
116 /* *INDENT-ON* */
119 static gboolean mode_change;
120 static int current_file;
121 static gboolean ignore_all;
123 static mode_t and_mask, or_mask, ch_mode;
125 static WLabel *statl;
126 static WGroupbox *file_gb;
128 /* --------------------------------------------------------------------------------------------- */
129 /*** file scope functions ************************************************************************/
130 /* --------------------------------------------------------------------------------------------- */
132 static void
133 chmod_init (void)
135 static gboolean i18n = FALSE;
136 int i, len;
138 for (i = 0; i < BUTTONS_PERM; i++)
139 check_perm[i].selected = FALSE;
141 if (i18n)
142 return;
144 i18n = TRUE;
146 #ifdef ENABLE_NLS
147 for (i = 0; i < BUTTONS_PERM; i++)
148 check_perm[i].text = _(check_perm[i].text);
150 for (i = 0; i < LABELS; i++)
151 file_info_labels[i] = _(file_info_labels[i]);
153 for (i = 0; i < BUTTONS; i++)
154 chmod_but[i].text = _(chmod_but[i].text);
155 #endif /* ENABLE_NLS */
157 for (i = 0; i < BUTTONS_PERM; i++)
159 len = str_term_width1 (check_perm[i].text);
160 check_perm_len = MAX (check_perm_len, len);
163 check_perm_len += 1 + 3 + 1; /* mark, [x] and space */
165 for (i = 0; i < LABELS; i++)
167 len = str_term_width1 (file_info_labels[i]) + 2; /* spaces around */
168 file_info_labels_len = MAX (file_info_labels_len, len);
171 for (i = 0; i < BUTTONS; i++)
173 chmod_but[i].len = str_term_width1 (chmod_but[i].text) + 3; /* [], spaces and w/o & */
174 if (chmod_but[i].flags == DEFPUSH_BUTTON)
175 chmod_but[i].len += 2; /* <> */
179 /* --------------------------------------------------------------------------------------------- */
181 static void
182 chmod_draw_select (const WDialog *h, int Id)
184 widget_gotoyx (h, PY + Id + 1, PX + 1);
185 tty_print_char (check_perm[Id].selected ? '*' : ' ');
186 widget_gotoyx (h, PY + Id + 1, PX + 3);
189 /* --------------------------------------------------------------------------------------------- */
191 static void
192 chmod_toggle_select (const WDialog *h, int Id)
194 check_perm[Id].selected = !check_perm[Id].selected;
195 tty_setcolor (COLOR_NORMAL);
196 chmod_draw_select (h, Id);
199 /* --------------------------------------------------------------------------------------------- */
201 static void
202 chmod_refresh (const WDialog *h)
204 int i;
205 int y, x;
207 tty_setcolor (COLOR_NORMAL);
209 for (i = 0; i < BUTTONS_PERM; i++)
210 chmod_draw_select (h, i);
212 y = WIDGET (file_gb)->rect.y + 1;
213 x = WIDGET (file_gb)->rect.x + 2;
215 tty_gotoyx (y, x);
216 tty_print_string (file_info_labels[0]);
217 tty_gotoyx (y + 2, x);
218 tty_print_string (file_info_labels[1]);
219 tty_gotoyx (y + 4, x);
220 tty_print_string (file_info_labels[2]);
221 tty_gotoyx (y + 6, x);
222 tty_print_string (file_info_labels[3]);
225 /* --------------------------------------------------------------------------------------------- */
227 static cb_ret_t
228 chmod_bg_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
230 switch (msg)
232 case MSG_DRAW:
233 frame_callback (w, NULL, MSG_DRAW, 0, NULL);
234 chmod_refresh (CONST_DIALOG (w->owner));
235 return MSG_HANDLED;
237 default:
238 return frame_callback (w, sender, msg, parm, data);
242 /* --------------------------------------------------------------------------------------------- */
244 static cb_ret_t
245 chmod_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
247 WGroup *g = GROUP (w);
248 WDialog *h = DIALOG (w);
250 switch (msg)
252 case MSG_NOTIFY:
254 /* handle checkboxes */
255 int i;
257 /* whether notification was sent by checkbox? */
258 for (i = 0; i < BUTTONS_PERM; i++)
259 if (sender == WIDGET (check_perm[i].check))
260 break;
262 if (i < BUTTONS_PERM)
264 ch_mode ^= check_perm[i].mode;
265 label_set_textv (statl, "%o", (unsigned int) ch_mode);
266 chmod_toggle_select (h, i);
267 mode_change = TRUE;
268 return MSG_HANDLED;
272 return MSG_NOT_HANDLED;
274 case MSG_KEY:
275 if (parm == 'T' || parm == 't' || parm == KEY_IC)
277 int i;
278 unsigned long id;
280 id = group_get_current_widget_id (g);
281 for (i = 0; i < BUTTONS_PERM; i++)
282 if (id == WIDGET (check_perm[i].check)->id)
283 break;
285 if (i < BUTTONS_PERM)
287 chmod_toggle_select (h, i);
288 if (parm == KEY_IC)
289 group_select_next_widget (g);
290 return MSG_HANDLED;
293 return MSG_NOT_HANDLED;
295 default:
296 return dlg_default_callback (w, sender, msg, parm, data);
300 /* --------------------------------------------------------------------------------------------- */
302 static WDialog *
303 chmod_dlg_create (WPanel *panel, const char *fname, const struct stat *sf_stat)
305 gboolean single_set;
306 WDialog *ch_dlg;
307 WGroup *g;
308 int lines, cols;
309 int i, y;
310 int perm_gb_len;
311 int file_gb_len;
312 const char *c_fname, *c_fown, *c_fgrp;
313 char buffer[BUF_TINY];
315 mode_change = FALSE;
317 single_set = (panel->marked < 2);
318 perm_gb_len = check_perm_len + 2;
319 file_gb_len = file_info_labels_len + 2;
320 cols = str_term_width1 (fname) + 2 + 1;
321 file_gb_len = MAX (file_gb_len, cols);
323 lines = single_set ? 20 : 23;
324 cols = perm_gb_len + file_gb_len + 1 + 6;
326 if (cols > COLS)
328 /* shrink the right groupbox */
329 cols = COLS;
330 file_gb_len = cols - (perm_gb_len + 1 + 6);
333 ch_dlg =
334 dlg_create (TRUE, 0, 0, lines, cols, WPOS_CENTER, FALSE, dialog_colors,
335 chmod_callback, NULL, "[Chmod]", _("Chmod command"));
336 g = GROUP (ch_dlg);
338 /* draw background */
339 ch_dlg->bg->callback = chmod_bg_callback;
341 group_add_widget (g, groupbox_new (PY, PX, BUTTONS_PERM + 2, perm_gb_len, _("Permission")));
343 for (i = 0; i < BUTTONS_PERM; i++)
345 check_perm[i].check = check_new (PY + i + 1, PX + 2, (ch_mode & check_perm[i].mode) != 0,
346 check_perm[i].text);
347 group_add_widget (g, check_perm[i].check);
350 file_gb = groupbox_new (PY, PX + perm_gb_len + 1, BUTTONS_PERM + 2, file_gb_len, _("File"));
351 group_add_widget (g, file_gb);
353 /* Set the labels */
354 y = PY + 2;
355 cols = PX + perm_gb_len + 3;
356 c_fname = str_trunc (fname, file_gb_len - 3);
357 group_add_widget (g, label_new (y, cols, c_fname));
358 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) ch_mode);
359 statl = label_new (y + 2, cols, buffer);
360 group_add_widget (g, statl);
361 c_fown = str_trunc (get_owner (sf_stat->st_uid), file_gb_len - 3);
362 group_add_widget (g, label_new (y + 4, cols, c_fown));
363 c_fgrp = str_trunc (get_group (sf_stat->st_gid), file_gb_len - 3);
364 group_add_widget (g, label_new (y + 6, cols, c_fgrp));
366 if (!single_set)
368 i = 0;
370 group_add_widget (g, hline_new (lines - chmod_but[i].y - 1, -1, -1));
372 for (; i < BUTTONS - 2; i++)
374 y = lines - chmod_but[i].y;
375 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->rect.cols / 2 - chmod_but[i].len,
376 chmod_but[i].ret_cmd, chmod_but[i].flags,
377 chmod_but[i].text, NULL));
378 i++;
379 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->rect.cols / 2 + 1,
380 chmod_but[i].ret_cmd, chmod_but[i].flags,
381 chmod_but[i].text, NULL));
385 i = BUTTONS - 2;
386 y = lines - chmod_but[i].y;
387 group_add_widget (g, hline_new (y - 1, -1, -1));
388 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->rect.cols / 2 - chmod_but[i].len,
389 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text,
390 NULL));
391 i++;
392 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->rect.cols / 2 + 1, chmod_but[i].ret_cmd,
393 chmod_but[i].flags, chmod_but[i].text, NULL));
395 /* select first checkbox */
396 widget_select (WIDGET (check_perm[0].check));
398 return ch_dlg;
401 /* --------------------------------------------------------------------------------------------- */
403 static void
404 chmod_done (gboolean need_update)
406 if (need_update)
407 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
408 repaint_screen ();
411 /* --------------------------------------------------------------------------------------------- */
413 static gboolean
414 try_chmod (const vfs_path_t *p, mode_t m)
416 const char *fname = NULL;
418 while (mc_chmod (p, m) == -1 && !ignore_all)
420 int my_errno = errno;
421 int result;
422 char *msg;
424 if (fname == NULL)
425 fname = x_basename (vfs_path_as_str (p));
426 msg = g_strdup_printf (_("Cannot chmod \"%s\"\n%s"), fname, unix_error_string (my_errno));
427 result =
428 query_dialog (MSG_ERROR, msg, D_ERROR, 4, _("&Ignore"), _("Ignore &all"), _("&Retry"),
429 _("&Cancel"));
430 g_free (msg);
432 switch (result)
434 case 0:
435 /* try next file */
436 return TRUE;
438 case 1:
439 ignore_all = TRUE;
440 /* try next file */
441 return TRUE;
443 case 2:
444 /* retry this file */
445 break;
447 case 3:
448 default:
449 /* stop remain files processing */
450 return FALSE;
454 return TRUE;
457 /* --------------------------------------------------------------------------------------------- */
459 static gboolean
460 do_chmod (WPanel *panel, const vfs_path_t *p, struct stat *sf)
462 gboolean ret;
464 sf->st_mode &= and_mask;
465 sf->st_mode |= or_mask;
467 ret = try_chmod (p, sf->st_mode);
469 do_file_mark (panel, current_file, 0);
471 return ret;
474 /* --------------------------------------------------------------------------------------------- */
476 static void
477 apply_mask (WPanel *panel, vfs_path_t *vpath, struct stat *sf)
479 gboolean ok;
481 if (!do_chmod (panel, vpath, sf))
482 return;
486 const GString *fname;
488 fname = panel_find_marked_file (panel, &current_file);
489 vpath = vfs_path_from_str (fname->str);
490 ok = (mc_stat (vpath, sf) == 0);
492 if (!ok)
494 /* if current file was deleted outside mc -- try next file */
495 /* decrease panel->marked */
496 do_file_mark (panel, current_file, 0);
498 /* try next file */
499 ok = TRUE;
501 else
503 ch_mode = sf->st_mode;
505 ok = do_chmod (panel, vpath, sf);
508 vfs_path_free (vpath, TRUE);
510 while (ok && panel->marked != 0);
513 /* --------------------------------------------------------------------------------------------- */
514 /*** public functions ****************************************************************************/
515 /* --------------------------------------------------------------------------------------------- */
517 void
518 chmod_cmd (WPanel *panel)
520 gboolean need_update;
521 gboolean end_chmod;
523 chmod_init ();
525 current_file = 0;
526 ignore_all = FALSE;
529 { /* do while any files remaining */
530 vfs_path_t *vpath;
531 WDialog *ch_dlg;
532 struct stat sf_stat;
533 const GString *fname;
534 int i, result;
536 do_refresh ();
538 need_update = FALSE;
539 end_chmod = FALSE;
541 fname = panel_get_marked_file (panel, &current_file);
542 if (fname == NULL)
543 break;
545 vpath = vfs_path_from_str (fname->str);
547 if (mc_stat (vpath, &sf_stat) != 0)
549 vfs_path_free (vpath, TRUE);
550 break;
553 ch_mode = sf_stat.st_mode;
555 ch_dlg = chmod_dlg_create (panel, fname->str, &sf_stat);
556 result = dlg_run (ch_dlg);
558 switch (result)
560 case B_CANCEL:
561 end_chmod = TRUE;
562 break;
564 case B_ENTER:
565 if (mode_change)
567 if (panel->marked <= 1)
569 /* single or last file */
570 if (mc_chmod (vpath, ch_mode) == -1 && !ignore_all)
571 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"), fname->str,
572 unix_error_string (errno));
573 end_chmod = TRUE;
575 else if (!try_chmod (vpath, ch_mode))
577 /* stop multiple files processing */
578 result = B_CANCEL;
579 end_chmod = TRUE;
583 need_update = TRUE;
584 break;
586 case B_SETALL:
587 case B_MARKED:
588 and_mask = or_mask = 0;
589 and_mask = ~and_mask;
591 for (i = 0; i < BUTTONS_PERM; i++)
592 if (check_perm[i].selected || result == B_SETALL)
594 if (check_perm[i].check->state)
595 or_mask |= check_perm[i].mode;
596 else
597 and_mask &= ~check_perm[i].mode;
600 apply_mask (panel, vpath, &sf_stat);
601 need_update = TRUE;
602 end_chmod = TRUE;
603 break;
605 case B_SETMRK:
606 and_mask = or_mask = 0;
607 and_mask = ~and_mask;
609 for (i = 0; i < BUTTONS_PERM; i++)
610 if (check_perm[i].selected)
611 or_mask |= check_perm[i].mode;
613 apply_mask (panel, vpath, &sf_stat);
614 need_update = TRUE;
615 end_chmod = TRUE;
616 break;
618 case B_CLRMRK:
619 and_mask = or_mask = 0;
620 and_mask = ~and_mask;
622 for (i = 0; i < BUTTONS_PERM; i++)
623 if (check_perm[i].selected)
624 and_mask &= ~check_perm[i].mode;
626 apply_mask (panel, vpath, &sf_stat);
627 need_update = TRUE;
628 end_chmod = TRUE;
629 break;
631 default:
632 break;
635 if (panel->marked != 0 && result != B_CANCEL)
637 do_file_mark (panel, current_file, 0);
638 need_update = TRUE;
641 vfs_path_free (vpath, TRUE);
643 widget_destroy (WIDGET (ch_dlg));
645 while (panel->marked != 0 && !end_chmod);
647 chmod_done (need_update);
650 /* --------------------------------------------------------------------------------------------- */