Added chmod_i18n() function.
[midnight-commander.git] / src / filemanager / chmod.c
blob19080e3c0d1687ab43d1ff1f0f3dbb5bad130c07
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/mc-vfs/vfs.h"
36 #include "lib/strutil.h"
37 #include "lib/util.h"
38 #include "lib/widget.h"
39 #include "lib/keybind.h" /* CK_DialogCancel */
41 #include "midnight.h" /* current_panel */
42 #include "layout.h" /* repaint_screen() */
43 #include "chmod.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 #define PX 5
50 #define PY 2
52 #define FX 40
53 #define FY 2
55 #define BX 6
56 #define BY 17
58 #define TX 40
59 #define TY 12
61 #define PERMISSIONS 12
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 struct
108 int ret_cmd;
109 int flags;
110 int y;
111 int x;
112 const char *text;
113 } chmod_but[BUTTONS] =
115 /* *INDENT-OFF* */
116 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
117 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
118 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
119 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
120 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
121 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") }
122 /* *INDENT-ON* */
125 /*** file scope functions ************************************************************************/
126 /* --------------------------------------------------------------------------------------------- */
128 static void
129 chmod_i18n (void)
131 #if ENABLE_NLS
132 static gboolean i18n = FALSE;
133 unsigned int i;
135 if (i18n)
136 return;
138 i18n = TRUE;
140 for (i = 0; i < PERMISSIONS; i++)
141 check_perm[i].text = _(check_perm[i].text);
143 for (i = 0; i < BUTTONS; i++)
144 chmod_but[i].text = _(chmod_but[i].text);
145 #endif /* ENABLE_NLS */
148 /* --------------------------------------------------------------------------------------------- */
150 static void
151 chmod_toggle_select (Dlg_head * h, int Id)
153 tty_setcolor (COLOR_NORMAL);
154 check_perm[Id].selected = !check_perm[Id].selected;
156 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
157 tty_print_char (check_perm[Id].selected ? '*' : ' ');
158 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
161 /* --------------------------------------------------------------------------------------------- */
163 static void
164 chmod_refresh (Dlg_head * h)
166 common_dialog_repaint (h);
168 tty_setcolor (COLOR_NORMAL);
170 dlg_move (h, FY + 1, FX + 2);
171 tty_print_string (_("Name"));
172 dlg_move (h, FY + 3, FX + 2);
173 tty_print_string (_("Permissions (Octal)"));
174 dlg_move (h, FY + 5, FX + 2);
175 tty_print_string (_("Owner name"));
176 dlg_move (h, FY + 7, FX + 2);
177 tty_print_string (_("Group name"));
180 /* --------------------------------------------------------------------------------------------- */
182 static cb_ret_t
183 chmod_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
185 char buffer[BUF_TINY];
186 int id;
188 id = dlg_get_current_widget_id (h) - BUTTONS + single_set * 2 - 1;
190 switch (msg)
192 case DLG_ACTION:
193 /* close dialog due to SIGINT (ctrl-g) */
194 if (sender == NULL && parm == CK_DialogCancel)
195 return MSG_NOT_HANDLED;
197 /* handle checkboxes */
198 if (id >= 0)
200 c_stat ^= check_perm[id].mode;
201 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
202 label_set_text (statl, buffer);
203 chmod_toggle_select (h, id);
204 mode_change = TRUE;
205 return MSG_HANDLED;
208 return MSG_NOT_HANDLED;
210 case DLG_KEY:
211 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
213 chmod_toggle_select (h, id);
214 if (parm == KEY_IC)
215 dlg_one_down (h);
216 return MSG_HANDLED;
218 return MSG_NOT_HANDLED;
220 case DLG_DRAW:
221 chmod_refresh (h);
222 return MSG_HANDLED;
224 default:
225 return default_dlg_callback (h, sender, msg, parm, data);
229 /* --------------------------------------------------------------------------------------------- */
231 static Dlg_head *
232 init_chmod (void)
234 unsigned int i;
235 Dlg_head *ch_dlg;
237 do_refresh ();
239 need_update = FALSE;
240 end_chmod = FALSE;
241 c_file = 0;
242 single_set = (current_panel->marked < 2) ? 2 : 0;
244 ch_dlg =
245 create_dlg (TRUE, 0, 0, 22 - single_set, 70, dialog_colors,
246 chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
248 for (i = 0; i < BUTTONS; i++)
250 if (i == 2 && single_set != 0)
251 break;
253 add_widget (ch_dlg,
254 button_new (BY + chmod_but[i].y - single_set,
255 BX + chmod_but[i].x,
256 chmod_but[i].ret_cmd,
257 chmod_but[i].flags, chmod_but[i].text, 0));
260 add_widget (ch_dlg, groupbox_new (FY, FX, 10, 25, _("File")));
262 for (i = 0; i < PERMISSIONS; i++)
264 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2, 0, check_perm[i].text);
265 add_widget (ch_dlg, check_perm[i].check);
268 add_widget (ch_dlg, groupbox_new (PY, PX, PERMISSIONS + 2, 33, _("Permission")));
270 return ch_dlg;
273 /* --------------------------------------------------------------------------------------------- */
275 static void
276 chmod_done (void)
278 if (need_update)
279 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
280 repaint_screen ();
283 /* --------------------------------------------------------------------------------------------- */
285 static char *
286 next_file (void)
288 while (!current_panel->dir.list[c_file].f.marked)
289 c_file++;
291 return current_panel->dir.list[c_file].fname;
294 /* --------------------------------------------------------------------------------------------- */
296 static void
297 do_chmod (struct stat *sf)
299 sf->st_mode &= and_mask;
300 sf->st_mode |= or_mask;
302 if (mc_chmod (current_panel->dir.list[c_file].fname, sf->st_mode) == -1)
303 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
304 current_panel->dir.list[c_file].fname, unix_error_string (errno));
306 do_file_mark (current_panel, c_file, 0);
309 /* --------------------------------------------------------------------------------------------- */
311 static void
312 apply_mask (struct stat *sf)
314 need_update = TRUE;
315 end_chmod = TRUE;
317 do_chmod (sf);
321 char *fname;
323 fname = next_file ();
324 if (mc_stat (fname, sf) != 0)
325 return;
327 c_stat = sf->st_mode;
329 do_chmod (sf);
331 while (current_panel->marked != 0);
334 /* --------------------------------------------------------------------------------------------- */
335 /*** public functions ****************************************************************************/
336 /* --------------------------------------------------------------------------------------------- */
338 void
339 chmod_cmd (void)
341 char *fname;
342 struct stat sf_stat;
343 char buffer[BUF_TINY];
344 unsigned int i;
346 chmod_i18n ();
349 { /* do while any files remaining */
350 Dlg_head *ch_dlg;
351 int result;
352 const char *c_fname, *c_fown, *c_fgrp;
354 ch_dlg = init_chmod ();
356 if (current_panel->marked != 0)
357 fname = next_file (); /* next marked file */
358 else
359 fname = selection (current_panel)->fname; /* single file */
361 if (mc_stat (fname, &sf_stat) != 0)
362 { /* get status of file */
363 destroy_dlg (ch_dlg);
364 break;
367 c_stat = sf_stat.st_mode;
368 mode_change = FALSE; /* clear changes flag */
370 /* set check buttons */
371 for (i = 0; i < PERMISSIONS; i++)
372 check_perm[i].check->state = (c_stat & check_perm[i].mode) != 0 ? 1 : 0;
374 /* Set the labels */
375 c_fname = str_trunc (fname, 21);
376 add_widget (ch_dlg, label_new (FY + 2, FX + 2, c_fname));
377 c_fown = str_trunc (get_owner (sf_stat.st_uid), 21);
378 add_widget (ch_dlg, label_new (FY + 6, FX + 2, c_fown));
379 c_fgrp = str_trunc (get_group (sf_stat.st_gid), 21);
380 add_widget (ch_dlg, label_new (FY + 8, FX + 2, c_fgrp));
381 g_snprintf (buffer, sizeof (buffer), "%o", (unsigned int) c_stat);
382 statl = label_new (FY + 4, FX + 2, buffer);
383 add_widget (ch_dlg, statl);
385 /* do action */
386 result = run_dlg (ch_dlg);
388 switch (result)
390 case B_ENTER:
391 if (mode_change && mc_chmod (fname, c_stat) == -1)
392 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
393 fname, unix_error_string (errno));
394 need_update = TRUE;
395 break;
397 case B_CANCEL:
398 end_chmod = TRUE;
399 break;
401 case B_ALL:
402 case B_MARKED:
403 and_mask = or_mask = 0;
404 and_mask = ~and_mask;
406 for (i = 0; i < PERMISSIONS; i++)
407 if (check_perm[i].selected || result == B_ALL)
409 if (check_perm[i].check->state & C_BOOL)
410 or_mask |= check_perm[i].mode;
411 else
412 and_mask &= ~check_perm[i].mode;
415 apply_mask (&sf_stat);
416 break;
418 case B_SETMRK:
419 and_mask = or_mask = 0;
420 and_mask = ~and_mask;
422 for (i = 0; i < PERMISSIONS; i++)
423 if (check_perm[i].selected)
424 or_mask |= check_perm[i].mode;
426 apply_mask (&sf_stat);
427 break;
429 case B_CLRMRK:
430 and_mask = or_mask = 0;
431 and_mask = ~and_mask;
433 for (i = 0; i < PERMISSIONS; i++)
434 if (check_perm[i].selected)
435 and_mask &= ~check_perm[i].mode;
437 apply_mask (&sf_stat);
438 break;
441 if (current_panel->marked != 0 && result != B_CANCEL)
443 do_file_mark (current_panel, c_file, 0);
444 need_update = TRUE;
447 destroy_dlg (ch_dlg);
449 while (current_panel->marked != 0 && !end_chmod);
451 chmod_done ();
454 /* --------------------------------------------------------------------------------------------- */