Moved charsets.[ch] from src to lib directory
[midnight-commander.git] / src / chmod.c
blobd5d7d8b2505e0b48933e8807c3c3146c98f3a544
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"
43 #include "panel.h" /* do_file_mark() */
44 #include "main.h" /* update_panels() */
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 int mode_change, need_update;
79 static int c_file, end_chmod;
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 int selected;
96 WCheck *check;
97 } check_perm[PERMISSIONS] =
99 /* *INDENT-OFF* */
100 { S_IXOTH, N_("execute/search by others"), 0, 0 },
101 { S_IWOTH, N_("write by others"), 0, 0 },
102 { S_IROTH, N_("read by others"), 0, 0 },
103 { S_IXGRP, N_("execute/search by group"), 0, 0 },
104 { S_IWGRP, N_("write by group"), 0, 0 },
105 { S_IRGRP, N_("read by group"), 0, 0 },
106 { S_IXUSR, N_("execute/search by owner"), 0, 0 },
107 { S_IWUSR, N_("write by owner"), 0, 0 },
108 { S_IRUSR, N_("read by owner"), 0, 0 },
109 { S_ISVTX, N_("sticky bit"), 0, 0 },
110 { S_ISGID, N_("set group ID on execution"), 0, 0 },
111 { S_ISUID, N_("set user ID on execution"), 0, 0 }
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 ^= 1;
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 if (id >= 0)
186 c_stat ^= check_perm[id].mode;
187 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
188 label_set_text (statl, buffer);
189 chmod_toggle_select (h, id);
190 mode_change = 1;
192 return MSG_HANDLED;
194 case DLG_KEY:
195 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
197 chmod_toggle_select (h, id);
198 if (parm == KEY_IC)
199 dlg_one_down (h);
200 return MSG_HANDLED;
202 return MSG_NOT_HANDLED;
204 case DLG_DRAW:
205 chmod_refresh (h);
206 return MSG_HANDLED;
208 default:
209 return default_dlg_callback (h, sender, msg, parm, data);
213 /* --------------------------------------------------------------------------------------------- */
215 static Dlg_head *
216 init_chmod (void)
218 int i;
219 Dlg_head *ch_dlg;
221 do_refresh ();
222 end_chmod = c_file = need_update = 0;
223 single_set = (current_panel->marked < 2) ? 2 : 0;
225 ch_dlg =
226 create_dlg (TRUE, 0, 0, 22 - single_set, 70, dialog_colors,
227 chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
229 for (i = 0; i < BUTTONS; i++)
231 if (i == 2 && single_set)
232 break;
233 else
234 add_widget (ch_dlg,
235 button_new (BY + chmod_but[i].y - single_set,
236 BX + chmod_but[i].x,
237 chmod_but[i].ret_cmd,
238 chmod_but[i].flags, _(chmod_but[i].text), 0));
241 add_widget (ch_dlg, groupbox_new (FY, FX, 10, 25, _("File")));
243 for (i = 0; i < PERMISSIONS; i++)
245 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2, 0, _(check_perm[i].text));
246 add_widget (ch_dlg, check_perm[i].check);
249 add_widget (ch_dlg, groupbox_new (PY, PX, PERMISSIONS + 2, 33, _("Permission")));
251 return ch_dlg;
254 /* --------------------------------------------------------------------------------------------- */
256 static void
257 chmod_done (void)
259 if (need_update)
260 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
261 repaint_screen ();
264 /* --------------------------------------------------------------------------------------------- */
266 static char *
267 next_file (void)
269 while (!current_panel->dir.list[c_file].f.marked)
270 c_file++;
272 return current_panel->dir.list[c_file].fname;
275 /* --------------------------------------------------------------------------------------------- */
277 static void
278 do_chmod (struct stat *sf)
280 sf->st_mode &= and_mask;
281 sf->st_mode |= or_mask;
282 if (mc_chmod (current_panel->dir.list[c_file].fname, sf->st_mode) == -1)
283 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
284 current_panel->dir.list[c_file].fname, unix_error_string (errno));
286 do_file_mark (current_panel, c_file, 0);
289 /* --------------------------------------------------------------------------------------------- */
291 static void
292 apply_mask (struct stat *sf)
294 char *fname;
296 need_update = end_chmod = 1;
297 do_chmod (sf);
301 fname = next_file ();
302 if (mc_stat (fname, sf) != 0)
303 return;
304 c_stat = sf->st_mode;
306 do_chmod (sf);
308 while (current_panel->marked);
311 /* --------------------------------------------------------------------------------------------- */
312 /*** public functions ****************************************************************************/
313 /* --------------------------------------------------------------------------------------------- */
315 void
316 chmod_cmd (void)
318 char buffer[BUF_TINY];
319 char *fname;
320 int i;
321 struct stat sf_stat;
322 Dlg_head *ch_dlg;
325 { /* do while any files remaining */
326 ch_dlg = init_chmod ();
327 if (current_panel->marked)
328 fname = next_file (); /* next marked file */
329 else
330 fname = selection (current_panel)->fname; /* single file */
332 if (mc_stat (fname, &sf_stat) != 0)
333 { /* get status of file */
334 destroy_dlg (ch_dlg);
335 break;
338 c_stat = sf_stat.st_mode;
339 mode_change = 0; /* clear changes flag */
341 /* set check buttons */
342 for (i = 0; i < PERMISSIONS; i++)
344 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
345 check_perm[i].selected = 0;
348 /* Set the labels */
349 c_fname = str_trunc (fname, 21);
350 add_widget (ch_dlg, label_new (FY + 2, FX + 2, c_fname));
351 c_fown = str_trunc (get_owner (sf_stat.st_uid), 21);
352 add_widget (ch_dlg, label_new (FY + 6, FX + 2, c_fown));
353 c_fgrp = str_trunc (get_group (sf_stat.st_gid), 21);
354 add_widget (ch_dlg, label_new (FY + 8, FX + 2, c_fgrp));
355 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
356 statl = label_new (FY + 4, FX + 2, buffer);
357 add_widget (ch_dlg, statl);
359 run_dlg (ch_dlg); /* retrieve an action */
361 /* do action */
362 switch (ch_dlg->ret_value)
364 case B_ENTER:
365 if (mode_change)
366 if (mc_chmod (fname, c_stat) == -1)
367 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
368 fname, unix_error_string (errno));
369 need_update = 1;
370 break;
372 case B_CANCEL:
373 end_chmod = 1;
374 break;
376 case B_ALL:
377 case B_MARKED:
378 and_mask = or_mask = 0;
379 and_mask = ~and_mask;
381 for (i = 0; i < PERMISSIONS; i++)
383 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
385 if (check_perm[i].check->state & C_BOOL)
386 or_mask |= check_perm[i].mode;
387 else
388 and_mask &= ~check_perm[i].mode;
392 apply_mask (&sf_stat);
393 break;
395 case B_SETMRK:
396 and_mask = or_mask = 0;
397 and_mask = ~and_mask;
399 for (i = 0; i < PERMISSIONS; i++)
401 if (check_perm[i].selected)
402 or_mask |= check_perm[i].mode;
405 apply_mask (&sf_stat);
406 break;
408 case B_CLRMRK:
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 and_mask &= ~check_perm[i].mode;
418 apply_mask (&sf_stat);
419 break;
422 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
424 do_file_mark (current_panel, c_file, 0);
425 need_update = 1;
428 destroy_dlg (ch_dlg);
430 while (current_panel->marked && !end_chmod);
432 chmod_done ();
435 /* --------------------------------------------------------------------------------------------- */