Merge commit 'origin/mc-4.6'
[midnight-commander.git] / src / chmod.c
blob2671c6567ea5a3bc1926d8f6a81a60645f8ece64
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 #include <config.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
30 #include "global.h"
31 #include "tty.h" /* A_REVERSE */
32 #include "color.h" /* dialog_colors */
33 #include "dialog.h" /* add_widget() */
34 #include "widget.h" /* NORMAL_BUTTON */
35 #include "wtools.h" /* message() */
36 #include "panel.h" /* do_file_mark() */
37 #include "main.h" /* update_panels() */
38 #include "chmod.h"
40 static int single_set;
42 #define PX 5
43 #define PY 2
45 #define FX 40
46 #define FY 2
48 #define BX 6
49 #define BY 17
51 #define TX 40
52 #define TY 12
54 #define PERMISSIONS 12
55 #define BUTTONS 6
57 #define B_MARKED B_USER
58 #define B_ALL (B_USER+1)
59 #define B_SETMRK (B_USER+2)
60 #define B_CLRMRK (B_USER+3)
62 static int mode_change, need_update;
63 static int c_file, end_chmod;
65 static mode_t and_mask, or_mask, c_stat;
67 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
68 * returns a pointer to a static buffer, and label_new creates its own copy
69 * of its argument)
70 * --rillig, 2004-08-29 */
71 static const char *c_fname, *c_fown, *c_fgrp;
73 static WLabel *statl;
75 static struct {
76 mode_t mode;
77 const char *text;
78 int selected;
79 WCheck *check;
80 } check_perm[PERMISSIONS] =
82 { S_IXOTH, N_("execute/search by others"), 0, 0, },
83 { S_IWOTH, N_("write by others"), 0, 0, },
84 { S_IROTH, N_("read by others"), 0, 0, },
85 { S_IXGRP, N_("execute/search by group"), 0, 0, },
86 { S_IWGRP, N_("write by group"), 0, 0, },
87 { S_IRGRP, N_("read by group"), 0, 0, },
88 { S_IXUSR, N_("execute/search by owner"), 0, 0, },
89 { S_IWUSR, N_("write by owner"), 0, 0, },
90 { S_IRUSR, N_("read by owner"), 0, 0, },
91 { S_ISVTX, N_("sticky bit"), 0, 0, },
92 { S_ISGID, N_("set group ID on execution"), 0, 0, },
93 { S_ISUID, N_("set user ID on execution"), 0, 0, },
96 static struct {
97 int ret_cmd, flags, y, x;
98 const char *text;
99 } chmod_but[BUTTONS] =
101 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
102 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
103 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
104 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
105 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
106 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
109 static void chmod_toggle_select (Dlg_head *h, int Id)
111 attrset (COLOR_NORMAL);
112 check_perm[Id].selected ^= 1;
114 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
115 addch ((check_perm[Id].selected) ? '*' : ' ');
116 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
119 static void chmod_refresh (Dlg_head *h)
121 common_dialog_repaint (h);
123 attrset (COLOR_NORMAL);
125 draw_box (h, PY, PX, PERMISSIONS + 2, 33);
126 draw_box (h, FY, FX, 10, 25);
128 dlg_move (h, FY + 1, FX + 2);
129 addstr (_("Name"));
130 dlg_move (h, FY + 3, FX + 2);
131 addstr (_("Permissions (Octal)"));
132 dlg_move (h, FY + 5, FX + 2);
133 addstr (_("Owner name"));
134 dlg_move (h, FY + 7, FX + 2);
135 addstr (_("Group name"));
137 dlg_move (h, TY, TX);
138 addstr (_("Use SPACE to change"));
139 dlg_move (h, TY + 1, TX);
140 addstr (_("an option, ARROW KEYS"));
141 dlg_move (h, TY + 2, TX);
142 addstr (_("to move between options"));
143 dlg_move (h, TY + 3, TX);
144 addstr (_("and T or INS to mark"));
146 attrset (COLOR_HOT_NORMAL);
148 dlg_move (h, PY, PX + 1);
149 addstr (_(" Permission "));
150 dlg_move (h, FY, FX + 1);
151 addstr (_(" File "));
154 static cb_ret_t
155 chmod_callback (Dlg_head *h, dlg_msg_t msg, int parm)
157 char buffer[BUF_TINY];
158 int id = h->current->dlg_id - BUTTONS + single_set * 2;
160 switch (msg) {
161 case DLG_ACTION:
162 if (id >= 0) {
163 c_stat ^= check_perm[id].mode;
164 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
165 label_set_text (statl, buffer);
166 chmod_toggle_select (h, id);
167 mode_change = 1;
169 return MSG_HANDLED;
171 case DLG_KEY:
172 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0) {
173 chmod_toggle_select (h, id);
174 if (parm == KEY_IC)
175 dlg_one_down (h);
176 return MSG_HANDLED;
178 return MSG_NOT_HANDLED;
180 case DLG_DRAW:
181 chmod_refresh (h);
182 return MSG_HANDLED;
184 default:
185 return default_dlg_callback (h, msg, parm);
189 static Dlg_head *
190 init_chmod (void)
192 int i;
193 Dlg_head *ch_dlg;
195 do_refresh ();
196 end_chmod = c_file = need_update = 0;
197 single_set = (current_panel->marked < 2) ? 2 : 0;
199 ch_dlg =
200 create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
201 chmod_callback, "[Chmod]", _("Chmod command"),
202 DLG_CENTER | DLG_REVERSE);
204 for (i = 0; i < BUTTONS; i++) {
205 if (i == 2 && single_set)
206 break;
207 else
208 add_widget (ch_dlg,
209 button_new (BY + chmod_but[i].y - single_set,
210 BX + chmod_but[i].x,
211 chmod_but[i].ret_cmd,
212 chmod_but[i].flags,
213 _(chmod_but[i].text), 0));
216 for (i = 0; i < PERMISSIONS; i++) {
217 check_perm[i].check =
218 check_new (PY + (PERMISSIONS - i), PX + 2, 0,
219 _(check_perm[i].text));
220 add_widget (ch_dlg, check_perm[i].check);
223 return ch_dlg;
226 static void chmod_done (void)
228 if (need_update)
229 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
230 repaint_screen ();
233 static char *next_file (void)
235 while (!current_panel->dir.list[c_file].f.marked)
236 c_file++;
238 return current_panel->dir.list[c_file].fname;
241 static void do_chmod (struct stat *sf)
243 sf->st_mode &= and_mask;
244 sf->st_mode |= or_mask;
245 if (mc_chmod (current_panel->dir.list [c_file].fname, sf->st_mode) == -1)
246 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
247 current_panel->dir.list [c_file].fname, unix_error_string (errno));
249 do_file_mark (current_panel, c_file, 0);
252 static void apply_mask (struct stat *sf)
254 char *fname;
256 need_update = end_chmod = 1;
257 do_chmod (sf);
259 do {
260 fname = next_file ();
261 if (mc_stat (fname, sf) != 0)
262 return;
263 c_stat = sf->st_mode;
265 do_chmod (sf);
266 } while (current_panel->marked);
269 void chmod_cmd (void)
271 char buffer [BUF_TINY];
272 char *fname;
273 int i;
274 struct stat sf_stat;
275 Dlg_head *ch_dlg;
277 do { /* do while any files remaining */
278 ch_dlg = init_chmod ();
279 if (current_panel->marked)
280 fname = next_file (); /* next marked file */
281 else
282 fname = selection (current_panel)->fname; /* single file */
284 if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
285 destroy_dlg (ch_dlg);
286 break;
289 c_stat = sf_stat.st_mode;
290 mode_change = 0; /* clear changes flag */
292 /* set check buttons */
293 for (i = 0; i < PERMISSIONS; i++){
294 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
295 check_perm[i].selected = 0;
298 /* Set the labels */
299 c_fname = name_trunc (fname, 21);
300 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname));
301 c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
302 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown));
303 c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
304 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp));
305 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
306 statl = label_new (FY+4, FX+2, buffer);
307 add_widget (ch_dlg, statl);
309 run_dlg (ch_dlg); /* retrieve an action */
311 /* do action */
312 switch (ch_dlg->ret_value){
313 case B_ENTER:
314 if (mode_change)
315 if (mc_chmod (fname, c_stat) == -1)
316 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
317 fname, unix_error_string (errno));
318 need_update = 1;
319 break;
321 case B_CANCEL:
322 end_chmod = 1;
323 break;
325 case B_ALL:
326 case B_MARKED:
327 and_mask = or_mask = 0;
328 and_mask = ~and_mask;
330 for (i = 0; i < PERMISSIONS; i++) {
331 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL) {
332 if (check_perm[i].check->state & C_BOOL)
333 or_mask |= check_perm[i].mode;
334 else
335 and_mask &= ~check_perm[i].mode;
339 apply_mask (&sf_stat);
340 break;
342 case B_SETMRK:
343 and_mask = or_mask = 0;
344 and_mask = ~and_mask;
346 for (i = 0; i < PERMISSIONS; i++) {
347 if (check_perm[i].selected)
348 or_mask |= check_perm[i].mode;
351 apply_mask (&sf_stat);
352 break;
353 case B_CLRMRK:
354 and_mask = or_mask = 0;
355 and_mask = ~and_mask;
357 for (i = 0; i < PERMISSIONS; i++) {
358 if (check_perm[i].selected)
359 and_mask &= ~check_perm[i].mode;
362 apply_mask (&sf_stat);
363 break;
366 if (current_panel->marked && ch_dlg->ret_value!=B_CANCEL) {
367 do_file_mark (current_panel, c_file, 0);
368 need_update = 1;
370 destroy_dlg (ch_dlg);
371 } while (current_panel->marked && !end_chmod);
372 chmod_done ();