Optimization of Chmod dialog initialization.
[midnight-commander.git] / src / filemanager / chmod.c
blob3c22516516bfeccfc65b9abc1908c0f535943f14
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 int 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;
119 int x;
120 const char *text;
121 } chmod_but[BUTTONS] =
123 /* *INDENT-OFF* */
124 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
125 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
126 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
127 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
128 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
129 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") }
130 /* *INDENT-ON* */
133 /*** file scope functions ************************************************************************/
134 /* --------------------------------------------------------------------------------------------- */
136 static void
137 chmod_i18n (void)
139 #if ENABLE_NLS
140 static gboolean i18n = FALSE;
141 unsigned int i;
143 if (i18n)
144 return;
146 i18n = TRUE;
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 */
159 /* --------------------------------------------------------------------------------------------- */
161 static void
162 chmod_toggle_select (Dlg_head * h, int Id)
164 tty_setcolor (COLOR_NORMAL);
165 check_perm[Id].selected = !check_perm[Id].selected;
167 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
168 tty_print_char (check_perm[Id].selected ? '*' : ' ');
169 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
172 /* --------------------------------------------------------------------------------------------- */
174 static void
175 chmod_refresh (Dlg_head * h)
177 common_dialog_repaint (h);
179 tty_setcolor (COLOR_NORMAL);
181 dlg_move (h, FY + 1, FX + 2);
182 tty_print_string (file_info_labels[0]);
183 dlg_move (h, FY + 3, FX + 2);
184 tty_print_string (file_info_labels[1]);
185 dlg_move (h, FY + 5, FX + 2);
186 tty_print_string (file_info_labels[2]);
187 dlg_move (h, FY + 7, FX + 2);
188 tty_print_string (file_info_labels[3]);
191 /* --------------------------------------------------------------------------------------------- */
193 static cb_ret_t
194 chmod_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
196 char buffer[BUF_TINY];
197 int id;
199 id = dlg_get_current_widget_id (h) - BUTTONS + single_set * 2 - 1;
201 switch (msg)
203 case DLG_ACTION:
204 /* close dialog due to SIGINT (ctrl-g) */
205 if (sender == NULL && parm == CK_Cancel)
206 return MSG_NOT_HANDLED;
208 /* handle checkboxes */
209 if (id >= 0)
211 c_stat ^= check_perm[id].mode;
212 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
213 label_set_text (statl, buffer);
214 chmod_toggle_select (h, id);
215 mode_change = TRUE;
216 return MSG_HANDLED;
219 return MSG_NOT_HANDLED;
221 case DLG_KEY:
222 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
224 chmod_toggle_select (h, id);
225 if (parm == KEY_IC)
226 dlg_one_down (h);
227 return MSG_HANDLED;
229 return MSG_NOT_HANDLED;
231 case DLG_DRAW:
232 chmod_refresh (h);
233 return MSG_HANDLED;
235 default:
236 return default_dlg_callback (h, sender, msg, parm, data);
240 /* --------------------------------------------------------------------------------------------- */
242 static Dlg_head *
243 init_chmod (const char *fname, const struct stat *sf_stat)
245 Dlg_head *ch_dlg;
246 unsigned int i;
247 const char *c_fname, *c_fown, *c_fgrp;
248 char buffer[BUF_TINY];
250 single_set = (current_panel->marked < 2) ? 2 : 0;
252 ch_dlg =
253 create_dlg (TRUE, 0, 0, 22 - single_set, 70, dialog_colors,
254 chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
256 for (i = 0; i < BUTTONS; i++)
258 if (i == 2 && single_set != 0)
259 break;
261 add_widget (ch_dlg,
262 button_new (BY + chmod_but[i].y - single_set,
263 BX + chmod_but[i].x,
264 chmod_but[i].ret_cmd,
265 chmod_but[i].flags, chmod_but[i].text, 0));
268 add_widget (ch_dlg, groupbox_new (FY, FX, 10, 25, _("File")));
270 for (i = 0; i < PERMISSIONS; i++)
272 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
273 (c_stat & check_perm[i].mode) != 0 ? 1 : 0,
274 check_perm[i].text);
275 add_widget (ch_dlg, check_perm[i].check);
278 add_widget (ch_dlg, groupbox_new (PY, PX, PERMISSIONS + 2, 33, _("Permission")));
280 /* Set the labels */
281 /* Do this at end to have a widget id in a simple way */
282 c_fname = str_trunc (fname, 21);
283 add_widget (ch_dlg, label_new (FY + 2, FX + 2, c_fname));
284 c_fown = str_trunc (get_owner (sf_stat->st_uid), 21);
285 add_widget (ch_dlg, label_new (FY + 6, FX + 2, c_fown));
286 c_fgrp = str_trunc (get_group (sf_stat->st_gid), 21);
287 add_widget (ch_dlg, label_new (FY + 8, FX + 2, c_fgrp));
288 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
289 statl = label_new (FY + 4, FX + 2, buffer);
290 add_widget (ch_dlg, statl);
292 return ch_dlg;
295 /* --------------------------------------------------------------------------------------------- */
297 static void
298 chmod_done (void)
300 if (need_update)
301 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
302 repaint_screen ();
305 /* --------------------------------------------------------------------------------------------- */
307 static char *
308 next_file (void)
310 while (!current_panel->dir.list[c_file].f.marked)
311 c_file++;
313 return current_panel->dir.list[c_file].fname;
316 /* --------------------------------------------------------------------------------------------- */
318 static void
319 do_chmod (struct stat *sf)
321 sf->st_mode &= and_mask;
322 sf->st_mode |= or_mask;
324 if (mc_chmod (current_panel->dir.list[c_file].fname, sf->st_mode) == -1)
325 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
326 current_panel->dir.list[c_file].fname, unix_error_string (errno));
328 do_file_mark (current_panel, c_file, 0);
331 /* --------------------------------------------------------------------------------------------- */
333 static void
334 apply_mask (struct stat *sf)
336 need_update = TRUE;
337 end_chmod = TRUE;
339 do_chmod (sf);
343 char *fname;
345 fname = next_file ();
346 if (mc_stat (fname, sf) != 0)
347 return;
349 c_stat = sf->st_mode;
351 do_chmod (sf);
353 while (current_panel->marked != 0);
356 /* --------------------------------------------------------------------------------------------- */
357 /*** public functions ****************************************************************************/
358 /* --------------------------------------------------------------------------------------------- */
360 void
361 chmod_cmd (void)
363 chmod_i18n ();
366 { /* do while any files remaining */
367 Dlg_head *ch_dlg;
368 struct stat sf_stat;
369 char *fname;
370 int result;
371 unsigned int i;
373 do_refresh ();
375 mode_change = FALSE;
376 need_update = FALSE;
377 end_chmod = FALSE;
378 c_file = 0;
380 if (current_panel->marked != 0)
381 fname = next_file (); /* next marked file */
382 else
383 fname = selection (current_panel)->fname; /* single file */
385 if (mc_stat (fname, &sf_stat) != 0)
386 break;
388 c_stat = sf_stat.st_mode;
390 ch_dlg = init_chmod (fname, &sf_stat);
392 /* do action */
393 result = run_dlg (ch_dlg);
395 switch (result)
397 case B_ENTER:
398 if (mode_change && mc_chmod (fname, c_stat) == -1)
399 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
400 fname, unix_error_string (errno));
401 need_update = TRUE;
402 break;
404 case B_CANCEL:
405 end_chmod = TRUE;
406 break;
408 case B_ALL:
409 case B_MARKED:
410 and_mask = or_mask = 0;
411 and_mask = ~and_mask;
413 for (i = 0; i < PERMISSIONS; i++)
414 if (check_perm[i].selected || result == B_ALL)
416 if (check_perm[i].check->state & C_BOOL)
417 or_mask |= check_perm[i].mode;
418 else
419 and_mask &= ~check_perm[i].mode;
422 apply_mask (&sf_stat);
423 break;
425 case B_SETMRK:
426 and_mask = or_mask = 0;
427 and_mask = ~and_mask;
429 for (i = 0; i < PERMISSIONS; i++)
430 if (check_perm[i].selected)
431 or_mask |= check_perm[i].mode;
433 apply_mask (&sf_stat);
434 break;
436 case B_CLRMRK:
437 and_mask = or_mask = 0;
438 and_mask = ~and_mask;
440 for (i = 0; i < PERMISSIONS; i++)
441 if (check_perm[i].selected)
442 and_mask &= ~check_perm[i].mode;
444 apply_mask (&sf_stat);
445 break;
448 if (current_panel->marked != 0 && result != B_CANCEL)
450 do_file_mark (current_panel, c_file, 0);
451 need_update = TRUE;
454 destroy_dlg (ch_dlg);
456 while (current_panel->marked != 0 && !end_chmod);
458 chmod_done ();
461 /* --------------------------------------------------------------------------------------------- */