Ticket #2557: i18n reimplementation of 'Chmod' dialog.
[midnight-commander.git] / src / filemanager / chmod.c
blob97a6a7d9796fdbb046b574fad84162dfc1eda2b0
1 /* Chmod command -- for the Midnight Commander
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3 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 <stdio.h>
28 #include <string.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/mc-vfs/vfs.h"
39 #include "lib/strutil.h"
40 #include "lib/util.h"
41 #include "lib/widget.h"
42 #include "lib/keybind.h" /* CK_DialogCancel */
44 #include "midnight.h" /* current_panel */
45 #include "layout.h" /* repaint_screen() */
46 #include "chmod.h"
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 #define PX 5
53 #define PY 2
55 #define FX 40
56 #define FY 2
58 #define BX 6
59 #define BY 17
61 #define TX 40
62 #define TY 12
64 #define PERMISSIONS 12
65 #define BUTTONS 6
67 #define B_MARKED B_USER
68 #define B_ALL (B_USER+1)
69 #define B_SETMRK (B_USER+2)
70 #define B_CLRMRK (B_USER+3)
72 /*** file scope type declarations ****************************************************************/
74 /*** file scope variables ************************************************************************/
76 static int single_set;
78 static gboolean mode_change, need_update, end_chmod;
79 static int c_file;
81 static mode_t and_mask, or_mask, c_stat;
83 /* FIXME: these variables are superfluous, aren't they? (hint: str_trunc
84 * returns a pointer to a static buffer, and label_new creates its own copy
85 * of its argument)
86 * --rillig, 2004-08-29 */
87 static const char *c_fname, *c_fown, *c_fgrp;
89 static WLabel *statl;
91 static struct
93 mode_t mode;
94 const char *text;
95 gboolean selected;
96 WCheck *check;
97 } check_perm[PERMISSIONS] =
99 /* *INDENT-OFF* */
100 { S_IXOTH, N_("execute/search by others"), FALSE, NULL },
101 { S_IWOTH, N_("write by others"), FALSE, NULL },
102 { S_IROTH, N_("read by others"), FALSE, NULL },
103 { S_IXGRP, N_("execute/search by group"), FALSE, NULL },
104 { S_IWGRP, N_("write by group"), FALSE, NULL },
105 { S_IRGRP, N_("read by group"), FALSE, NULL },
106 { S_IXUSR, N_("execute/search by owner"), FALSE, NULL },
107 { S_IWUSR, N_("write by owner"), FALSE, NULL },
108 { S_IRUSR, N_("read by owner"), FALSE, NULL },
109 { S_ISVTX, N_("sticky bit"), FALSE, NULL },
110 { S_ISGID, N_("set group ID on execution"), FALSE, NULL },
111 { S_ISUID, N_("set user ID on execution"), FALSE, NULL }
112 /* *INDENT-ON* */
115 static struct
117 int ret_cmd, flags, y, x;
118 const char *text;
119 } chmod_but[BUTTONS] =
121 /* *INDENT-OFF* */
122 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
123 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
124 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
125 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
126 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
127 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") }
128 /* *INDENT-ON* */
131 /*** file scope functions ************************************************************************/
132 /* --------------------------------------------------------------------------------------------- */
134 static void
135 chmod_toggle_select (Dlg_head * h, int Id)
137 tty_setcolor (COLOR_NORMAL);
138 check_perm[Id].selected = !check_perm[Id].selected;
140 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
141 tty_print_char ((check_perm[Id].selected) ? '*' : ' ');
142 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
145 /* --------------------------------------------------------------------------------------------- */
147 static void
148 chmod_refresh (Dlg_head * h)
150 common_dialog_repaint (h);
152 tty_setcolor (COLOR_NORMAL);
154 dlg_move (h, FY + 1, FX + 2);
155 tty_print_string (_("Name"));
156 dlg_move (h, FY + 3, FX + 2);
157 tty_print_string (_("Permissions (Octal)"));
158 dlg_move (h, FY + 5, FX + 2);
159 tty_print_string (_("Owner name"));
160 dlg_move (h, FY + 7, FX + 2);
161 tty_print_string (_("Group name"));
163 dlg_move (h, TY, TX);
164 tty_print_string (_("Use SPACE to change"));
165 dlg_move (h, TY + 1, TX);
166 tty_print_string (_("an option, ARROW KEYS"));
167 dlg_move (h, TY + 2, TX);
168 tty_print_string (_("to move between options"));
169 dlg_move (h, TY + 3, TX);
170 tty_print_string (_("and T or INS to mark"));
173 /* --------------------------------------------------------------------------------------------- */
175 static cb_ret_t
176 chmod_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
178 char buffer[BUF_TINY];
179 int id = dlg_get_current_widget_id (h) - BUTTONS + single_set * 2 - 1;
181 switch (msg)
183 case DLG_ACTION:
184 /* close dialog due to SIGINT (ctrl-g) */
185 if (sender == NULL && parm == CK_DialogCancel)
186 return MSG_NOT_HANDLED;
188 /* handle checkboxes */
189 if (id >= 0)
191 c_stat ^= check_perm[id].mode;
192 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
193 label_set_text (statl, buffer);
194 chmod_toggle_select (h, id);
195 mode_change = TRUE;
196 return MSG_HANDLED;
199 return MSG_NOT_HANDLED;
201 case DLG_KEY:
202 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
204 chmod_toggle_select (h, id);
205 if (parm == KEY_IC)
206 dlg_one_down (h);
207 return MSG_HANDLED;
209 return MSG_NOT_HANDLED;
211 case DLG_DRAW:
212 chmod_refresh (h);
213 return MSG_HANDLED;
215 default:
216 return default_dlg_callback (h, sender, msg, parm, data);
220 /* --------------------------------------------------------------------------------------------- */
222 static Dlg_head *
223 init_chmod (void)
225 unsigned int i;
226 Dlg_head *ch_dlg;
228 do_refresh ();
230 need_update = FALSE;
231 end_chmod = FALSE;
232 c_file = 0;
233 single_set = (current_panel->marked < 2) ? 2 : 0;
235 ch_dlg =
236 create_dlg (TRUE, 0, 0, 22 - single_set, 70, dialog_colors,
237 chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
239 for (i = 0; i < BUTTONS; i++)
241 if (i == 2 && single_set != 0)
242 break;
243 else
244 add_widget (ch_dlg,
245 button_new (BY + chmod_but[i].y - single_set,
246 BX + chmod_but[i].x,
247 chmod_but[i].ret_cmd,
248 chmod_but[i].flags, _(chmod_but[i].text), 0));
251 add_widget (ch_dlg, groupbox_new (FY, FX, 10, 25, _("File")));
253 for (i = 0; i < PERMISSIONS; i++)
255 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2, 0, _(check_perm[i].text));
256 add_widget (ch_dlg, check_perm[i].check);
259 add_widget (ch_dlg, groupbox_new (PY, PX, PERMISSIONS + 2, 33, _("Permission")));
261 return ch_dlg;
264 /* --------------------------------------------------------------------------------------------- */
266 static void
267 chmod_done (void)
269 if (need_update)
270 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
271 repaint_screen ();
274 /* --------------------------------------------------------------------------------------------- */
276 static char *
277 next_file (void)
279 while (!current_panel->dir.list[c_file].f.marked)
280 c_file++;
282 return current_panel->dir.list[c_file].fname;
285 /* --------------------------------------------------------------------------------------------- */
287 static void
288 do_chmod (struct stat *sf)
290 sf->st_mode &= and_mask;
291 sf->st_mode |= or_mask;
292 if (mc_chmod (current_panel->dir.list[c_file].fname, sf->st_mode) == -1)
293 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
294 current_panel->dir.list[c_file].fname, unix_error_string (errno));
296 do_file_mark (current_panel, c_file, 0);
299 /* --------------------------------------------------------------------------------------------- */
301 static void
302 apply_mask (struct stat *sf)
304 char *fname;
306 need_update = TRUE;
307 end_chmod = TRUE;
309 do_chmod (sf);
313 fname = next_file ();
314 if (mc_stat (fname, sf) != 0)
315 return;
316 c_stat = sf->st_mode;
318 do_chmod (sf);
320 while (current_panel->marked != 0);
323 /* --------------------------------------------------------------------------------------------- */
324 /*** public functions ****************************************************************************/
325 /* --------------------------------------------------------------------------------------------- */
327 void
328 chmod_cmd (void)
330 char buffer[BUF_TINY];
331 char *fname;
332 unsigned int i;
333 struct stat sf_stat;
334 Dlg_head *ch_dlg;
337 { /* do while any files remaining */
338 ch_dlg = init_chmod ();
340 if (current_panel->marked != 0)
341 fname = next_file (); /* next marked file */
342 else
343 fname = selection (current_panel)->fname; /* single file */
345 if (mc_stat (fname, &sf_stat) != 0)
346 { /* get status of file */
347 destroy_dlg (ch_dlg);
348 break;
351 c_stat = sf_stat.st_mode;
352 mode_change = FALSE; /* clear changes flag */
354 /* set check buttons */
355 for (i = 0; i < PERMISSIONS; i++)
357 check_perm[i].check->state = (c_stat & check_perm[i].mode) != 0 ? 1 : 0;
358 check_perm[i].selected = FALSE;
361 /* Set the labels */
362 c_fname = str_trunc (fname, 21);
363 add_widget (ch_dlg, label_new (FY + 2, FX + 2, c_fname));
364 c_fown = str_trunc (get_owner (sf_stat.st_uid), 21);
365 add_widget (ch_dlg, label_new (FY + 6, FX + 2, c_fown));
366 c_fgrp = str_trunc (get_group (sf_stat.st_gid), 21);
367 add_widget (ch_dlg, label_new (FY + 8, FX + 2, c_fgrp));
368 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
369 statl = label_new (FY + 4, FX + 2, buffer);
370 add_widget (ch_dlg, statl);
372 run_dlg (ch_dlg); /* retrieve an action */
374 /* do action */
375 switch (ch_dlg->ret_value)
377 case B_ENTER:
378 if (mode_change)
379 if (mc_chmod (fname, c_stat) == -1)
380 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
381 fname, unix_error_string (errno));
382 need_update = TRUE;
383 break;
385 case B_CANCEL:
386 end_chmod = TRUE;
387 break;
389 case B_ALL:
390 case B_MARKED:
391 and_mask = or_mask = 0;
392 and_mask = ~and_mask;
394 for (i = 0; i < PERMISSIONS; i++)
396 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
398 if (check_perm[i].check->state & C_BOOL)
399 or_mask |= check_perm[i].mode;
400 else
401 and_mask &= ~check_perm[i].mode;
405 apply_mask (&sf_stat);
406 break;
408 case B_SETMRK:
409 and_mask = or_mask = 0;
410 and_mask = ~and_mask;
412 for (i = 0; i < PERMISSIONS; i++)
414 if (check_perm[i].selected)
415 or_mask |= check_perm[i].mode;
418 apply_mask (&sf_stat);
419 break;
421 case B_CLRMRK:
422 and_mask = or_mask = 0;
423 and_mask = ~and_mask;
425 for (i = 0; i < PERMISSIONS; i++)
427 if (check_perm[i].selected)
428 and_mask &= ~check_perm[i].mode;
431 apply_mask (&sf_stat);
432 break;
435 if (current_panel->marked != 0 && ch_dlg->ret_value != B_CANCEL)
437 do_file_mark (current_panel, c_file, 0);
438 need_update = TRUE;
441 destroy_dlg (ch_dlg);
443 while (current_panel->marked != 0 && !end_chmod);
445 chmod_done ();
448 /* --------------------------------------------------------------------------------------------- */