Place buttons in 2 rows if more than 2 files are processed.
[midnight-commander.git] / src / filemanager / chmod.c
blob069dcf1ee33d5936d4ac0b88a6954a424165b30d
1 /* Chmod command -- for the Midnight Commander
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
3 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /** \file chmod.c
21 * \brief Source: chmod command
24 #include <config.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
31 #include "lib/global.h"
33 #include "lib/tty/tty.h"
34 #include "lib/skin.h"
35 #include "lib/vfs/vfs.h"
36 #include "lib/strutil.h"
37 #include "lib/util.h"
38 #include "lib/widget.h"
39 #include "lib/keybind.h" /* CK_Cancel */
41 #include "midnight.h" /* current_panel */
42 #include "chmod.h"
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
48 #define PX 5
49 #define PY 2
51 #define FX 40
52 #define FY 2
54 #define BX 6
55 #define BY 17
57 #define TX 40
58 #define TY 12
60 #define PERMISSIONS 12
61 #define LABELS 4
62 #define BUTTONS 6
64 #define B_MARKED B_USER
65 #define B_ALL (B_USER + 1)
66 #define B_SETMRK (B_USER + 2)
67 #define B_CLRMRK (B_USER + 3)
69 /*** file scope type declarations ****************************************************************/
71 /*** file scope variables ************************************************************************/
73 static gboolean single_set;
75 static gboolean mode_change, need_update, end_chmod;
76 static int c_file;
78 static mode_t and_mask, or_mask, c_stat;
80 static WLabel *statl;
82 static struct
84 mode_t mode;
85 const char *text;
86 gboolean selected;
87 WCheck *check;
88 } check_perm[PERMISSIONS] =
90 /* *INDENT-OFF* */
91 { S_IXOTH, N_("execute/search by others"), FALSE, NULL },
92 { S_IWOTH, N_("write by others"), FALSE, NULL },
93 { S_IROTH, N_("read by others"), FALSE, NULL },
94 { S_IXGRP, N_("execute/search by group"), FALSE, NULL },
95 { S_IWGRP, N_("write by group"), FALSE, NULL },
96 { S_IRGRP, N_("read by group"), FALSE, NULL },
97 { S_IXUSR, N_("execute/search by owner"), FALSE, NULL },
98 { S_IWUSR, N_("write by owner"), FALSE, NULL },
99 { S_IRUSR, N_("read by owner"), FALSE, NULL },
100 { S_ISVTX, N_("sticky bit"), FALSE, NULL },
101 { S_ISGID, N_("set group ID on execution"), FALSE, NULL },
102 { S_ISUID, N_("set user ID on execution"), FALSE, NULL }
103 /* *INDENT-ON* */
106 static const char *file_info_labels[LABELS] =
108 N_("Name:"),
109 N_("Permissions (octal):"),
110 N_("Owner name:"),
111 N_("Group name:")
114 static struct
116 int ret_cmd;
117 int flags;
118 int y; /* vertical position relatively to dialog bottom boundary */
119 int len;
120 const char *text;
121 } chmod_but[BUTTONS] =
123 /* *INDENT-OFF* */
124 { B_CANCEL, NORMAL_BUTTON, 3, 0, N_("&Cancel") },
125 { B_ENTER, DEFPUSH_BUTTON, 3, 0, N_("&Set") },
126 { B_CLRMRK, NORMAL_BUTTON, 5, 0, N_("C&lear marked") },
127 { B_SETMRK, NORMAL_BUTTON, 5, 0, N_("S&et marked") },
128 { B_MARKED, NORMAL_BUTTON, 6, 0, N_("&Marked all") },
129 { B_ALL, NORMAL_BUTTON, 6, 0, N_("Set &all") }
130 /* *INDENT-ON* */
133 /*** file scope functions ************************************************************************/
134 /* --------------------------------------------------------------------------------------------- */
136 static void
137 chmod_i18n (void)
139 static gboolean i18n = FALSE;
140 unsigned int i;
142 if (i18n)
143 return;
145 i18n = TRUE;
147 #if ENABLE_NLS
148 for (i = 0; i < PERMISSIONS; i++)
149 check_perm[i].text = _(check_perm[i].text);
151 for (i = 0; i < LABELS; i++)
152 file_info_labels[i] = _(file_info_labels[i]);
154 for (i = 0; i < BUTTONS; i++)
155 chmod_but[i].text = _(chmod_but[i].text);
156 #endif /* ENABLE_NLS */
158 for (i = 0; i < BUTTONS; i++)
160 chmod_but[i].len = str_term_width1 (chmod_but[i].text) + 3; /* [], spaces and w/o & */
161 if (chmod_but[i].flags == DEFPUSH_BUTTON)
162 chmod_but[i].len += 2; /* <> */
166 /* --------------------------------------------------------------------------------------------- */
168 static void
169 chmod_toggle_select (Dlg_head * h, int Id)
171 tty_setcolor (COLOR_NORMAL);
172 check_perm[Id].selected = !check_perm[Id].selected;
174 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
175 tty_print_char (check_perm[Id].selected ? '*' : ' ');
176 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
179 /* --------------------------------------------------------------------------------------------- */
181 static void
182 chmod_refresh (Dlg_head * h)
184 common_dialog_repaint (h);
186 tty_setcolor (COLOR_NORMAL);
188 dlg_move (h, FY + 1, FX + 2);
189 tty_print_string (file_info_labels[0]);
190 dlg_move (h, FY + 3, FX + 2);
191 tty_print_string (file_info_labels[1]);
192 dlg_move (h, FY + 5, FX + 2);
193 tty_print_string (file_info_labels[2]);
194 dlg_move (h, FY + 7, FX + 2);
195 tty_print_string (file_info_labels[3]);
198 /* --------------------------------------------------------------------------------------------- */
200 static cb_ret_t
201 chmod_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
203 char buffer[BUF_TINY];
204 int id;
206 id = dlg_get_current_widget_id (h) - (BUTTONS - (single_set ? 4 : 0)) - 1;
208 switch (msg)
210 case DLG_ACTION:
211 /* close dialog due to SIGINT (ctrl-g) */
212 if (sender == NULL && parm == CK_Cancel)
213 return MSG_NOT_HANDLED;
215 /* handle checkboxes */
216 if (id >= 0)
218 c_stat ^= check_perm[id].mode;
219 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
220 label_set_text (statl, buffer);
221 chmod_toggle_select (h, id);
222 mode_change = TRUE;
223 return MSG_HANDLED;
226 return MSG_NOT_HANDLED;
228 case DLG_KEY:
229 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
231 chmod_toggle_select (h, id);
232 if (parm == KEY_IC)
233 dlg_one_down (h);
234 return MSG_HANDLED;
236 return MSG_NOT_HANDLED;
238 case DLG_DRAW:
239 chmod_refresh (h);
240 return MSG_HANDLED;
242 default:
243 return default_dlg_callback (h, sender, msg, parm, data);
247 /* --------------------------------------------------------------------------------------------- */
249 static Dlg_head *
250 init_chmod (const char *fname, const struct stat *sf_stat)
252 Dlg_head *ch_dlg;
253 int lines;
254 unsigned int i;
255 const char *c_fname, *c_fown, *c_fgrp;
256 char buffer[BUF_TINY];
258 single_set = (current_panel->marked < 2);
259 lines = single_set ? 20 : 23;
261 ch_dlg =
262 create_dlg (TRUE, 0, 0, lines, 70, dialog_colors,
263 chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
265 for (i = 0; i < BUTTONS; i++)
267 add_widget (ch_dlg,
268 button_new (lines - chmod_but[i].y, ch_dlg->cols / 2 + 1,
269 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0));
271 i++;
273 add_widget (ch_dlg,
274 button_new (lines - chmod_but[i].y, ch_dlg->cols / 2 - chmod_but[i].len,
275 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0));
277 if (single_set)
278 break;
281 add_widget (ch_dlg, groupbox_new (FY, FX, 10, 25, _("File")));
283 for (i = 0; i < PERMISSIONS; i++)
285 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
286 (c_stat & check_perm[i].mode) != 0 ? 1 : 0,
287 check_perm[i].text);
288 add_widget (ch_dlg, check_perm[i].check);
291 add_widget (ch_dlg, groupbox_new (PY, PX, PERMISSIONS + 2, 33, _("Permission")));
293 /* Set the labels */
294 /* Do this at end to have a widget id in a simple way */
295 c_fname = str_trunc (fname, 21);
296 add_widget (ch_dlg, label_new (FY + 2, FX + 2, c_fname));
297 c_fown = str_trunc (get_owner (sf_stat->st_uid), 21);
298 add_widget (ch_dlg, label_new (FY + 6, FX + 2, c_fown));
299 c_fgrp = str_trunc (get_group (sf_stat->st_gid), 21);
300 add_widget (ch_dlg, label_new (FY + 8, FX + 2, c_fgrp));
301 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
302 statl = label_new (FY + 4, FX + 2, buffer);
303 add_widget (ch_dlg, statl);
305 return ch_dlg;
308 /* --------------------------------------------------------------------------------------------- */
310 static void
311 chmod_done (void)
313 if (need_update)
314 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
315 repaint_screen ();
318 /* --------------------------------------------------------------------------------------------- */
320 static char *
321 next_file (void)
323 while (!current_panel->dir.list[c_file].f.marked)
324 c_file++;
326 return current_panel->dir.list[c_file].fname;
329 /* --------------------------------------------------------------------------------------------- */
331 static void
332 do_chmod (struct stat *sf)
334 sf->st_mode &= and_mask;
335 sf->st_mode |= or_mask;
337 if (mc_chmod (current_panel->dir.list[c_file].fname, sf->st_mode) == -1)
338 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
339 current_panel->dir.list[c_file].fname, unix_error_string (errno));
341 do_file_mark (current_panel, c_file, 0);
344 /* --------------------------------------------------------------------------------------------- */
346 static void
347 apply_mask (struct stat *sf)
349 need_update = TRUE;
350 end_chmod = TRUE;
352 do_chmod (sf);
356 char *fname;
358 fname = next_file ();
359 if (mc_stat (fname, sf) != 0)
360 return;
362 c_stat = sf->st_mode;
364 do_chmod (sf);
366 while (current_panel->marked != 0);
369 /* --------------------------------------------------------------------------------------------- */
370 /*** public functions ****************************************************************************/
371 /* --------------------------------------------------------------------------------------------- */
373 void
374 chmod_cmd (void)
376 chmod_i18n ();
379 { /* do while any files remaining */
380 Dlg_head *ch_dlg;
381 struct stat sf_stat;
382 char *fname;
383 int result;
384 unsigned int i;
386 do_refresh ();
388 mode_change = FALSE;
389 need_update = FALSE;
390 end_chmod = FALSE;
391 c_file = 0;
393 if (current_panel->marked != 0)
394 fname = next_file (); /* next marked file */
395 else
396 fname = selection (current_panel)->fname; /* single file */
398 if (mc_stat (fname, &sf_stat) != 0)
399 break;
401 c_stat = sf_stat.st_mode;
403 ch_dlg = init_chmod (fname, &sf_stat);
405 /* do action */
406 result = run_dlg (ch_dlg);
408 switch (result)
410 case B_ENTER:
411 if (mode_change && mc_chmod (fname, c_stat) == -1)
412 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
413 fname, unix_error_string (errno));
414 need_update = TRUE;
415 break;
417 case B_CANCEL:
418 end_chmod = TRUE;
419 break;
421 case B_ALL:
422 case B_MARKED:
423 and_mask = or_mask = 0;
424 and_mask = ~and_mask;
426 for (i = 0; i < PERMISSIONS; i++)
427 if (check_perm[i].selected || result == B_ALL)
429 if (check_perm[i].check->state & C_BOOL)
430 or_mask |= check_perm[i].mode;
431 else
432 and_mask &= ~check_perm[i].mode;
435 apply_mask (&sf_stat);
436 break;
438 case B_SETMRK:
439 and_mask = or_mask = 0;
440 and_mask = ~and_mask;
442 for (i = 0; i < PERMISSIONS; i++)
443 if (check_perm[i].selected)
444 or_mask |= check_perm[i].mode;
446 apply_mask (&sf_stat);
447 break;
449 case B_CLRMRK:
450 and_mask = or_mask = 0;
451 and_mask = ~and_mask;
453 for (i = 0; i < PERMISSIONS; i++)
454 if (check_perm[i].selected)
455 and_mask &= ~check_perm[i].mode;
457 apply_mask (&sf_stat);
458 break;
461 if (current_panel->marked != 0 && result != B_CANCEL)
463 do_file_mark (current_panel, c_file, 0);
464 need_update = TRUE;
467 destroy_dlg (ch_dlg);
469 while (current_panel->marked != 0 && !end_chmod);
471 chmod_done ();
474 /* --------------------------------------------------------------------------------------------- */