Merge branch '2123_crash_while_copy'
[midnight-commander.git] / src / chmod.c
blob2ba47ac3c88126196b2abe1c03f23fbdf243091c
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"
41 #include "dialog.h" /* add_widget() */
42 #include "widget.h" /* NORMAL_BUTTON */
43 #include "wtools.h" /* message() */
44 #include "panel.h" /* do_file_mark() */
45 #include "main.h" /* update_panels() */
46 #include "layout.h" /* repaint_screen() */
47 #include "chmod.h"
49 static int single_set;
51 #define PX 5
52 #define PY 2
54 #define FX 40
55 #define FY 2
57 #define BX 6
58 #define BY 17
60 #define TX 40
61 #define TY 12
63 #define PERMISSIONS 12
64 #define BUTTONS 6
66 #define B_MARKED B_USER
67 #define B_ALL (B_USER+1)
68 #define B_SETMRK (B_USER+2)
69 #define B_CLRMRK (B_USER+3)
71 static int mode_change, need_update;
72 static int c_file, end_chmod;
74 static mode_t and_mask, or_mask, c_stat;
76 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
77 * returns a pointer to a static buffer, and label_new creates its own copy
78 * of its argument)
79 * --rillig, 2004-08-29 */
80 static const char *c_fname, *c_fown, *c_fgrp;
82 static WLabel *statl;
84 static struct
86 mode_t mode;
87 const char *text;
88 int selected;
89 WCheck *check;
90 } check_perm[PERMISSIONS] =
92 /* *INDENT-OFF* */
93 { S_IXOTH, N_("execute/search by others"), 0, 0 },
94 { S_IWOTH, N_("write by others"), 0, 0 },
95 { S_IROTH, N_("read by others"), 0, 0 },
96 { S_IXGRP, N_("execute/search by group"), 0, 0 },
97 { S_IWGRP, N_("write by group"), 0, 0 },
98 { S_IRGRP, N_("read by group"), 0, 0 },
99 { S_IXUSR, N_("execute/search by owner"), 0, 0 },
100 { S_IWUSR, N_("write by owner"), 0, 0 },
101 { S_IRUSR, N_("read by owner"), 0, 0 },
102 { S_ISVTX, N_("sticky bit"), 0, 0 },
103 { S_ISGID, N_("set group ID on execution"), 0, 0 },
104 { S_ISUID, N_("set user ID on execution"), 0, 0 }
105 /* *INDENT-ON* */
108 static struct
110 int ret_cmd, flags, y, x;
111 const char *text;
112 } chmod_but[BUTTONS] =
114 /* *INDENT-OFF* */
115 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
116 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
117 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
118 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
119 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
120 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") }
121 /* *INDENT-ON* */
124 static void
125 chmod_toggle_select (Dlg_head * h, int Id)
127 tty_setcolor (COLOR_NORMAL);
128 check_perm[Id].selected ^= 1;
130 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
131 tty_print_char ((check_perm[Id].selected) ? '*' : ' ');
132 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
135 static void
136 chmod_refresh (Dlg_head * h)
138 common_dialog_repaint (h);
140 tty_setcolor (COLOR_NORMAL);
142 dlg_move (h, FY + 1, FX + 2);
143 tty_print_string (_("Name"));
144 dlg_move (h, FY + 3, FX + 2);
145 tty_print_string (_("Permissions (Octal)"));
146 dlg_move (h, FY + 5, FX + 2);
147 tty_print_string (_("Owner name"));
148 dlg_move (h, FY + 7, FX + 2);
149 tty_print_string (_("Group name"));
151 dlg_move (h, TY, TX);
152 tty_print_string (_("Use SPACE to change"));
153 dlg_move (h, TY + 1, TX);
154 tty_print_string (_("an option, ARROW KEYS"));
155 dlg_move (h, TY + 2, TX);
156 tty_print_string (_("to move between options"));
157 dlg_move (h, TY + 3, TX);
158 tty_print_string (_("and T or INS to mark"));
161 static cb_ret_t
162 chmod_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
164 char buffer[BUF_TINY];
165 int id = dlg_get_current_widget_id (h) - BUTTONS + single_set * 2 - 1;
167 switch (msg)
169 case DLG_ACTION:
170 if (id >= 0)
172 c_stat ^= check_perm[id].mode;
173 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
174 label_set_text (statl, buffer);
175 chmod_toggle_select (h, id);
176 mode_change = 1;
178 return MSG_HANDLED;
180 case DLG_KEY:
181 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
183 chmod_toggle_select (h, id);
184 if (parm == KEY_IC)
185 dlg_one_down (h);
186 return MSG_HANDLED;
188 return MSG_NOT_HANDLED;
190 case DLG_DRAW:
191 chmod_refresh (h);
192 return MSG_HANDLED;
194 default:
195 return default_dlg_callback (h, sender, msg, parm, data);
199 static Dlg_head *
200 init_chmod (void)
202 int i;
203 Dlg_head *ch_dlg;
205 do_refresh ();
206 end_chmod = c_file = need_update = 0;
207 single_set = (current_panel->marked < 2) ? 2 : 0;
209 ch_dlg =
210 create_dlg (TRUE, 0, 0, 22 - single_set, 70, dialog_colors,
211 chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
213 for (i = 0; i < BUTTONS; i++)
215 if (i == 2 && single_set)
216 break;
217 else
218 add_widget (ch_dlg,
219 button_new (BY + chmod_but[i].y - single_set,
220 BX + chmod_but[i].x,
221 chmod_but[i].ret_cmd,
222 chmod_but[i].flags, _(chmod_but[i].text), 0));
225 add_widget (ch_dlg, groupbox_new (FY, FX, 10, 25, _("File")));
227 for (i = 0; i < PERMISSIONS; i++)
229 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2, 0, _(check_perm[i].text));
230 add_widget (ch_dlg, check_perm[i].check);
233 add_widget (ch_dlg, groupbox_new ( PY, PX, PERMISSIONS + 2, 33, _("Permission")));
235 return ch_dlg;
238 static void
239 chmod_done (void)
241 if (need_update)
242 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
243 repaint_screen ();
246 static char *
247 next_file (void)
249 while (!current_panel->dir.list[c_file].f.marked)
250 c_file++;
252 return current_panel->dir.list[c_file].fname;
255 static void
256 do_chmod (struct stat *sf)
258 sf->st_mode &= and_mask;
259 sf->st_mode |= or_mask;
260 if (mc_chmod (current_panel->dir.list[c_file].fname, sf->st_mode) == -1)
261 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
262 current_panel->dir.list[c_file].fname, unix_error_string (errno));
264 do_file_mark (current_panel, c_file, 0);
267 static void
268 apply_mask (struct stat *sf)
270 char *fname;
272 need_update = end_chmod = 1;
273 do_chmod (sf);
277 fname = next_file ();
278 if (mc_stat (fname, sf) != 0)
279 return;
280 c_stat = sf->st_mode;
282 do_chmod (sf);
284 while (current_panel->marked);
287 void
288 chmod_cmd (void)
290 char buffer[BUF_TINY];
291 char *fname;
292 int i;
293 struct stat sf_stat;
294 Dlg_head *ch_dlg;
297 { /* do while any files remaining */
298 ch_dlg = init_chmod ();
299 if (current_panel->marked)
300 fname = next_file (); /* next marked file */
301 else
302 fname = selection (current_panel)->fname; /* single file */
304 if (mc_stat (fname, &sf_stat) != 0)
305 { /* get status of file */
306 destroy_dlg (ch_dlg);
307 break;
310 c_stat = sf_stat.st_mode;
311 mode_change = 0; /* clear changes flag */
313 /* set check buttons */
314 for (i = 0; i < PERMISSIONS; i++)
316 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
317 check_perm[i].selected = 0;
320 /* Set the labels */
321 c_fname = str_trunc (fname, 21);
322 add_widget (ch_dlg, label_new (FY + 2, FX + 2, c_fname));
323 c_fown = str_trunc (get_owner (sf_stat.st_uid), 21);
324 add_widget (ch_dlg, label_new (FY + 6, FX + 2, c_fown));
325 c_fgrp = str_trunc (get_group (sf_stat.st_gid), 21);
326 add_widget (ch_dlg, label_new (FY + 8, FX + 2, c_fgrp));
327 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
328 statl = label_new (FY + 4, FX + 2, buffer);
329 add_widget (ch_dlg, statl);
331 run_dlg (ch_dlg); /* retrieve an action */
333 /* do action */
334 switch (ch_dlg->ret_value)
336 case B_ENTER:
337 if (mode_change)
338 if (mc_chmod (fname, c_stat) == -1)
339 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
340 fname, unix_error_string (errno));
341 need_update = 1;
342 break;
344 case B_CANCEL:
345 end_chmod = 1;
346 break;
348 case B_ALL:
349 case B_MARKED:
350 and_mask = or_mask = 0;
351 and_mask = ~and_mask;
353 for (i = 0; i < PERMISSIONS; i++)
355 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
357 if (check_perm[i].check->state & C_BOOL)
358 or_mask |= check_perm[i].mode;
359 else
360 and_mask &= ~check_perm[i].mode;
364 apply_mask (&sf_stat);
365 break;
367 case B_SETMRK:
368 and_mask = or_mask = 0;
369 and_mask = ~and_mask;
371 for (i = 0; i < PERMISSIONS; i++)
373 if (check_perm[i].selected)
374 or_mask |= check_perm[i].mode;
377 apply_mask (&sf_stat);
378 break;
380 case B_CLRMRK:
381 and_mask = or_mask = 0;
382 and_mask = ~and_mask;
384 for (i = 0; i < PERMISSIONS; i++)
386 if (check_perm[i].selected)
387 and_mask &= ~check_perm[i].mode;
390 apply_mask (&sf_stat);
391 break;
394 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
396 do_file_mark (current_panel, c_file, 0);
397 need_update = 1;
400 destroy_dlg (ch_dlg);
402 while (current_panel->marked && !end_chmod);
404 chmod_done ();