Move widget add/del API from WDialog to WGroup.
[midnight-commander.git] / src / filemanager / chmod.c
blob37d142f718ce6ec052081353a1a3867ad740dd66
1 /*
2 Chmod command -- for the Midnight Commander
4 Copyright (C) 1994-2020
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 "midnight.h" /* current_panel */
45 #include "chmod.h"
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 #define PX 3
52 #define PY 2
54 #define B_MARKED B_USER
55 #define B_SETALL (B_USER + 1)
56 #define B_SETMRK (B_USER + 2)
57 #define B_CLRMRK (B_USER + 3)
59 #define BUTTONS 6
60 #define BUTTONS_PERM 12
61 #define LABELS 4
63 /*** file scope type declarations ****************************************************************/
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] =
75 /* *INDENT-OFF* */
76 { S_ISUID, N_("set &user ID on execution"), FALSE, NULL },
77 { S_ISGID, N_("set &group ID on execution"), FALSE, NULL },
78 { S_ISVTX, N_("stick&y bit"), FALSE, NULL },
79 { S_IRUSR, N_("&read by owner"), FALSE, NULL },
80 { S_IWUSR, N_("&write by owner"), FALSE, NULL },
81 { S_IXUSR, N_("e&xecute/search by owner"), FALSE, NULL },
82 { S_IRGRP, N_("rea&d by group"), FALSE, NULL },
83 { S_IWGRP, N_("write by grou&p"), FALSE, NULL },
84 { S_IXGRP, N_("execu&te/search by group"), FALSE, NULL },
85 { S_IROTH, N_("read &by others"), FALSE, NULL },
86 { S_IWOTH, N_("wr&ite by others"), FALSE, NULL },
87 { S_IXOTH, N_("execute/searc&h by others"), FALSE, NULL }
88 /* *INDENT-ON* */
91 static int check_perm_len = 0;
93 static const char *file_info_labels[LABELS] = {
94 N_("Name:"),
95 N_("Permissions (octal):"),
96 N_("Owner name:"),
97 N_("Group name:")
100 static int file_info_labels_len = 0;
102 static struct
104 int ret_cmd;
105 button_flags_t flags;
106 int y; /* vertical position relatively to dialog bottom boundary */
107 int len;
108 const char *text;
109 } chmod_but[BUTTONS] =
111 /* *INDENT-OFF* */
112 { B_SETALL, NORMAL_BUTTON, 6, 0, N_("Set &all") },
113 { B_MARKED, NORMAL_BUTTON, 6, 0, N_("&Marked all") },
114 { B_SETMRK, NORMAL_BUTTON, 5, 0, N_("S&et marked") },
115 { B_CLRMRK, NORMAL_BUTTON, 5, 0, N_("C&lear marked") },
116 { B_ENTER, DEFPUSH_BUTTON, 3, 0, N_("&Set") },
117 { B_CANCEL, NORMAL_BUTTON, 3, 0, N_("&Cancel") }
118 /* *INDENT-ON* */
121 static gboolean mode_change;
122 static int current_file;
123 static gboolean ignore_all;
125 static mode_t and_mask, or_mask, ch_mode;
127 static WLabel *statl;
128 static WGroupbox *file_gb;
130 /* --------------------------------------------------------------------------------------------- */
131 /*** file scope functions ************************************************************************/
132 /* --------------------------------------------------------------------------------------------- */
134 static void
135 chmod_i18n (void)
137 static gboolean i18n = FALSE;
138 int i, len;
140 if (i18n)
141 return;
143 i18n = TRUE;
145 #ifdef ENABLE_NLS
146 for (i = 0; i < BUTTONS_PERM; i++)
147 check_perm[i].text = _(check_perm[i].text);
149 for (i = 0; i < LABELS; i++)
150 file_info_labels[i] = _(file_info_labels[i]);
152 for (i = 0; i < BUTTONS; i++)
153 chmod_but[i].text = _(chmod_but[i].text);
154 #endif /* ENABLE_NLS */
156 for (i = 0; i < BUTTONS_PERM; i++)
158 len = str_term_width1 (check_perm[i].text);
159 check_perm_len = MAX (check_perm_len, len);
162 check_perm_len += 1 + 3 + 1; /* mark, [x] and space */
164 for (i = 0; i < LABELS; i++)
166 len = str_term_width1 (file_info_labels[i]) + 2; /* spaces around */
167 file_info_labels_len = MAX (file_info_labels_len, len);
170 for (i = 0; i < BUTTONS; i++)
172 chmod_but[i].len = str_term_width1 (chmod_but[i].text) + 3; /* [], spaces and w/o & */
173 if (chmod_but[i].flags == DEFPUSH_BUTTON)
174 chmod_but[i].len += 2; /* <> */
178 /* --------------------------------------------------------------------------------------------- */
180 static void
181 chmod_toggle_select (WDialog * h, int Id)
183 tty_setcolor (COLOR_NORMAL);
184 check_perm[Id].selected = !check_perm[Id].selected;
186 widget_gotoyx (h, PY + Id + 1, PX + 1);
187 tty_print_char (check_perm[Id].selected ? '*' : ' ');
188 widget_gotoyx (h, PY + Id + 1, PX + 3);
191 /* --------------------------------------------------------------------------------------------- */
193 static void
194 chmod_refresh (WDialog * h)
196 int y = WIDGET (file_gb)->y + 1;
197 int x = WIDGET (file_gb)->x + 2;
199 dlg_default_repaint (h);
201 tty_setcolor (COLOR_NORMAL);
203 tty_gotoyx (y, x);
204 tty_print_string (file_info_labels[0]);
205 tty_gotoyx (y + 2, x);
206 tty_print_string (file_info_labels[1]);
207 tty_gotoyx (y + 4, x);
208 tty_print_string (file_info_labels[2]);
209 tty_gotoyx (y + 6, x);
210 tty_print_string (file_info_labels[3]);
213 /* --------------------------------------------------------------------------------------------- */
215 static cb_ret_t
216 chmod_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
218 WDialog *h = DIALOG (w);
220 switch (msg)
222 case MSG_DRAW:
223 chmod_refresh (h);
224 return MSG_HANDLED;
226 case MSG_NOTIFY:
228 /* handle checkboxes */
229 int i;
231 /* whether notification was sent by checkbox? */
232 for (i = 0; i < BUTTONS_PERM; i++)
233 if (sender == WIDGET (check_perm[i].check))
234 break;
236 if (i < BUTTONS_PERM)
238 char buffer[BUF_TINY];
240 ch_mode ^= check_perm[i].mode;
241 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) ch_mode);
242 label_set_text (statl, buffer);
243 chmod_toggle_select (h, i);
244 mode_change = TRUE;
245 return MSG_HANDLED;
249 return MSG_NOT_HANDLED;
251 case MSG_KEY:
252 if (parm == 'T' || parm == 't' || parm == KEY_IC)
254 int i;
255 unsigned long id;
257 id = dlg_get_current_widget_id (h);
258 for (i = 0; i < BUTTONS_PERM; i++)
259 if (id == WIDGET (check_perm[i].check)->id)
260 break;
262 if (i < BUTTONS_PERM)
264 chmod_toggle_select (h, i);
265 if (parm == KEY_IC)
266 group_select_next_widget (GROUP (h));
267 return MSG_HANDLED;
270 return MSG_NOT_HANDLED;
272 default:
273 return dlg_default_callback (w, sender, msg, parm, data);
277 /* --------------------------------------------------------------------------------------------- */
279 static WDialog *
280 chmod_init (const char *fname, const struct stat *sf_stat)
282 gboolean single_set;
283 WDialog *ch_dlg;
284 WGroup *g;
285 int lines, cols;
286 int i, y;
287 int perm_gb_len;
288 int file_gb_len;
289 const char *c_fname, *c_fown, *c_fgrp;
290 char buffer[BUF_TINY];
292 mode_change = FALSE;
294 single_set = (current_panel->marked < 2);
295 perm_gb_len = check_perm_len + 2;
296 file_gb_len = file_info_labels_len + 2;
297 cols = str_term_width1 (fname) + 2 + 1;
298 file_gb_len = MAX (file_gb_len, cols);
300 lines = single_set ? 20 : 23;
301 cols = perm_gb_len + file_gb_len + 1 + 6;
303 if (cols > COLS)
305 /* shrink the right groupbox */
306 cols = COLS;
307 file_gb_len = cols - (perm_gb_len + 1 + 6);
310 ch_dlg =
311 dlg_create (TRUE, 0, 0, lines, cols, WPOS_CENTER, FALSE, dialog_colors,
312 chmod_callback, NULL, "[Chmod]", _("Chmod command"));
313 g = GROUP (ch_dlg);
315 group_add_widget (g, groupbox_new (PY, PX, BUTTONS_PERM + 2, perm_gb_len, _("Permission")));
317 for (i = 0; i < BUTTONS_PERM; i++)
319 check_perm[i].check = check_new (PY + i + 1, PX + 2, (ch_mode & check_perm[i].mode) != 0,
320 check_perm[i].text);
321 group_add_widget (g, check_perm[i].check);
324 file_gb = groupbox_new (PY, PX + perm_gb_len + 1, BUTTONS_PERM + 2, file_gb_len, _("File"));
325 group_add_widget (g, file_gb);
327 /* Set the labels */
328 y = PY + 2;
329 cols = PX + perm_gb_len + 3;
330 c_fname = str_trunc (fname, file_gb_len - 3);
331 group_add_widget (g, label_new (y, cols, c_fname));
332 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) ch_mode);
333 statl = label_new (y + 2, cols, buffer);
334 group_add_widget (g, statl);
335 c_fown = str_trunc (get_owner (sf_stat->st_uid), file_gb_len - 3);
336 group_add_widget (g, label_new (y + 4, cols, c_fown));
337 c_fgrp = str_trunc (get_group (sf_stat->st_gid), file_gb_len - 3);
338 group_add_widget (g, label_new (y + 6, cols, c_fgrp));
340 if (!single_set)
342 i = 0;
344 group_add_widget (g, hline_new (lines - chmod_but[i].y - 1, -1, -1));
346 for (; i < BUTTONS - 2; i++)
348 y = lines - chmod_but[i].y;
349 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->cols / 2 - chmod_but[i].len,
350 chmod_but[i].ret_cmd, chmod_but[i].flags,
351 chmod_but[i].text, NULL));
352 i++;
353 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->cols / 2 + 1,
354 chmod_but[i].ret_cmd, chmod_but[i].flags,
355 chmod_but[i].text, NULL));
359 i = BUTTONS - 2;
360 y = lines - chmod_but[i].y;
361 group_add_widget (g, hline_new (y - 1, -1, -1));
362 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->cols / 2 - chmod_but[i].len,
363 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text,
364 NULL));
365 i++;
366 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->cols / 2 + 1, chmod_but[i].ret_cmd,
367 chmod_but[i].flags, chmod_but[i].text, NULL));
369 /* select first checkbox */
370 widget_select (WIDGET (check_perm[0].check));
372 return ch_dlg;
375 /* --------------------------------------------------------------------------------------------- */
377 static void
378 chmod_done (gboolean need_update)
380 if (need_update)
381 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
382 repaint_screen ();
385 /* --------------------------------------------------------------------------------------------- */
387 static const char *
388 next_file (void)
390 while (!current_panel->dir.list[current_file].f.marked)
391 current_file++;
393 return current_panel->dir.list[current_file].fname;
396 /* --------------------------------------------------------------------------------------------- */
398 static gboolean
399 try_chmod (const vfs_path_t * p, mode_t m)
401 while (mc_chmod (p, m) == -1 && !ignore_all)
403 int my_errno = errno;
404 int result;
405 char *msg;
407 msg =
408 g_strdup_printf (_("Cannot chmod \"%s\"\n%s"), x_basename (vfs_path_as_str (p)),
409 unix_error_string (my_errno));
410 result =
411 query_dialog (MSG_ERROR, msg, D_ERROR, 4, _("&Ignore"), _("Ignore &all"), _("&Retry"),
412 _("&Cancel"));
413 g_free (msg);
415 switch (result)
417 case 0:
418 /* try next file */
419 return TRUE;
421 case 1:
422 ignore_all = TRUE;
423 /* try next file */
424 return TRUE;
426 case 2:
427 /* retry this file */
428 break;
430 case 3:
431 default:
432 /* stop remain files processing */
433 return FALSE;
437 return TRUE;
440 /* --------------------------------------------------------------------------------------------- */
442 static gboolean
443 do_chmod (const vfs_path_t * p, struct stat *sf)
445 gboolean ret;
447 sf->st_mode &= and_mask;
448 sf->st_mode |= or_mask;
450 ret = try_chmod (p, sf->st_mode);
452 do_file_mark (current_panel, current_file, 0);
454 return ret;
457 /* --------------------------------------------------------------------------------------------- */
459 static void
460 apply_mask (vfs_path_t * vpath, struct stat *sf)
462 gboolean ok;
464 if (!do_chmod (vpath, sf))
465 return;
469 const char *fname;
471 fname = next_file ();
472 vpath = vfs_path_from_str (fname);
473 ok = (mc_stat (vpath, sf) == 0);
475 if (!ok)
477 /* if current file was deleted outside mc -- try next file */
478 /* decrease current_panel->marked */
479 do_file_mark (current_panel, current_file, 0);
481 /* try next file */
482 ok = TRUE;
484 else
486 ch_mode = sf->st_mode;
488 ok = do_chmod (vpath, sf);
491 vfs_path_free (vpath);
493 while (ok && current_panel->marked != 0);
496 /* --------------------------------------------------------------------------------------------- */
497 /*** public functions ****************************************************************************/
498 /* --------------------------------------------------------------------------------------------- */
500 void
501 chmod_cmd (void)
503 gboolean need_update;
504 gboolean end_chmod;
506 chmod_i18n ();
508 current_file = 0;
509 ignore_all = FALSE;
512 { /* do while any files remaining */
513 vfs_path_t *vpath;
514 WDialog *ch_dlg;
515 struct stat sf_stat;
516 const char *fname;
517 int i, result;
519 do_refresh ();
521 need_update = FALSE;
522 end_chmod = FALSE;
524 if (current_panel->marked != 0)
525 fname = next_file (); /* next marked file */
526 else
527 fname = selection (current_panel)->fname; /* single file */
529 vpath = vfs_path_from_str (fname);
531 if (mc_stat (vpath, &sf_stat) != 0)
533 vfs_path_free (vpath);
534 break;
537 ch_mode = sf_stat.st_mode;
539 ch_dlg = chmod_init (fname, &sf_stat);
541 result = dlg_run (ch_dlg);
543 switch (result)
545 case B_CANCEL:
546 end_chmod = TRUE;
547 break;
549 case B_ENTER:
550 if (mode_change)
552 if (current_panel->marked <= 1)
554 /* single or last file */
555 if (mc_chmod (vpath, ch_mode) == -1 && !ignore_all)
556 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"), fname,
557 unix_error_string (errno));
558 end_chmod = TRUE;
560 else if (!try_chmod (vpath, ch_mode))
562 /* stop multiple files processing */
563 result = B_CANCEL;
564 end_chmod = TRUE;
568 need_update = TRUE;
569 break;
571 case B_SETALL:
572 case B_MARKED:
573 and_mask = or_mask = 0;
574 and_mask = ~and_mask;
576 for (i = 0; i < BUTTONS_PERM; i++)
577 if (check_perm[i].selected || result == B_SETALL)
579 if (check_perm[i].check->state)
580 or_mask |= check_perm[i].mode;
581 else
582 and_mask &= ~check_perm[i].mode;
585 apply_mask (vpath, &sf_stat);
586 need_update = TRUE;
587 end_chmod = TRUE;
588 break;
590 case B_SETMRK:
591 and_mask = or_mask = 0;
592 and_mask = ~and_mask;
594 for (i = 0; i < BUTTONS_PERM; i++)
595 if (check_perm[i].selected)
596 or_mask |= check_perm[i].mode;
598 apply_mask (vpath, &sf_stat);
599 need_update = TRUE;
600 end_chmod = TRUE;
601 break;
603 case B_CLRMRK:
604 and_mask = or_mask = 0;
605 and_mask = ~and_mask;
607 for (i = 0; i < BUTTONS_PERM; i++)
608 if (check_perm[i].selected)
609 and_mask &= ~check_perm[i].mode;
611 apply_mask (vpath, &sf_stat);
612 need_update = TRUE;
613 end_chmod = TRUE;
614 break;
616 default:
617 break;
620 if (current_panel->marked != 0 && result != B_CANCEL)
622 do_file_mark (current_panel, current_file, 0);
623 need_update = TRUE;
626 vfs_path_free (vpath);
628 dlg_destroy (ch_dlg);
630 while (current_panel->marked != 0 && !end_chmod);
632 chmod_done (need_update);
635 /* --------------------------------------------------------------------------------------------- */