Merge branch '1790_mc_crash'
[midnight-commander.git] / src / chmod.c
blob4ddb0422a6b847c23882ec188efb143b86e9b658
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 "global.h"
36 #include "../src/tty/tty.h"
37 #include "../src/skin/skin.h"
39 #include "dialog.h" /* add_widget() */
40 #include "widget.h" /* NORMAL_BUTTON */
41 #include "wtools.h" /* message() */
42 #include "panel.h" /* do_file_mark() */
43 #include "main.h" /* update_panels() */
44 #include "layout.h" /* repaint_screen() */
45 #include "chmod.h"
46 #include "strutil.h"
48 static int single_set;
50 #define PX 5
51 #define PY 2
53 #define FX 40
54 #define FY 2
56 #define BX 6
57 #define BY 17
59 #define TX 40
60 #define TY 12
62 #define PERMISSIONS 12
63 #define BUTTONS 6
65 #define B_MARKED B_USER
66 #define B_ALL (B_USER+1)
67 #define B_SETMRK (B_USER+2)
68 #define B_CLRMRK (B_USER+3)
70 static int mode_change, need_update;
71 static int c_file, end_chmod;
73 static mode_t and_mask, or_mask, c_stat;
75 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
76 * returns a pointer to a static buffer, and label_new creates its own copy
77 * of its argument)
78 * --rillig, 2004-08-29 */
79 static const char *c_fname, *c_fown, *c_fgrp;
81 static WLabel *statl;
83 static struct {
84 mode_t mode;
85 const char *text;
86 int selected;
87 WCheck *check;
88 } check_perm[PERMISSIONS] =
90 { S_IXOTH, N_("execute/search by others"), 0, 0, },
91 { S_IWOTH, N_("write by others"), 0, 0, },
92 { S_IROTH, N_("read by others"), 0, 0, },
93 { S_IXGRP, N_("execute/search by group"), 0, 0, },
94 { S_IWGRP, N_("write by group"), 0, 0, },
95 { S_IRGRP, N_("read by group"), 0, 0, },
96 { S_IXUSR, N_("execute/search by owner"), 0, 0, },
97 { S_IWUSR, N_("write by owner"), 0, 0, },
98 { S_IRUSR, N_("read by owner"), 0, 0, },
99 { S_ISVTX, N_("sticky bit"), 0, 0, },
100 { S_ISGID, N_("set group ID on execution"), 0, 0, },
101 { S_ISUID, N_("set user ID on execution"), 0, 0, },
104 static struct {
105 int ret_cmd, flags, y, x;
106 const char *text;
107 } chmod_but[BUTTONS] =
109 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
110 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
111 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
112 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
113 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
114 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
117 static void chmod_toggle_select (Dlg_head *h, int Id)
119 tty_setcolor (COLOR_NORMAL);
120 check_perm[Id].selected ^= 1;
122 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
123 tty_print_char ((check_perm[Id].selected) ? '*' : ' ');
124 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
127 static void chmod_refresh (Dlg_head *h)
129 common_dialog_repaint (h);
131 tty_setcolor (COLOR_NORMAL);
133 draw_box (h, PY, PX, PERMISSIONS + 2, 33);
134 draw_box (h, FY, FX, 10, 25);
136 dlg_move (h, FY + 1, FX + 2);
137 tty_print_string (_("Name"));
138 dlg_move (h, FY + 3, FX + 2);
139 tty_print_string (_("Permissions (Octal)"));
140 dlg_move (h, FY + 5, FX + 2);
141 tty_print_string (_("Owner name"));
142 dlg_move (h, FY + 7, FX + 2);
143 tty_print_string (_("Group name"));
145 dlg_move (h, TY, TX);
146 tty_print_string (_("Use SPACE to change"));
147 dlg_move (h, TY + 1, TX);
148 tty_print_string (_("an option, ARROW KEYS"));
149 dlg_move (h, TY + 2, TX);
150 tty_print_string (_("to move between options"));
151 dlg_move (h, TY + 3, TX);
152 tty_print_string (_("and T or INS to mark"));
154 tty_setcolor (COLOR_HOT_NORMAL);
156 dlg_move (h, PY, PX + 1);
157 tty_print_string (_(" Permission "));
158 dlg_move (h, FY, FX + 1);
159 tty_print_string (_(" File "));
162 static cb_ret_t
163 chmod_callback (Dlg_head *h, dlg_msg_t msg, int parm)
165 char buffer[BUF_TINY];
166 int id = h->current->dlg_id - BUTTONS + single_set * 2;
168 switch (msg) {
169 case DLG_ACTION:
170 if (id >= 0) {
171 c_stat ^= check_perm[id].mode;
172 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
173 label_set_text (statl, buffer);
174 chmod_toggle_select (h, id);
175 mode_change = 1;
177 return MSG_HANDLED;
179 case DLG_KEY:
180 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0) {
181 chmod_toggle_select (h, id);
182 if (parm == KEY_IC)
183 dlg_one_down (h);
184 return MSG_HANDLED;
186 return MSG_NOT_HANDLED;
188 case DLG_DRAW:
189 chmod_refresh (h);
190 return MSG_HANDLED;
192 default:
193 return default_dlg_callback (h, msg, parm);
197 static Dlg_head *
198 init_chmod (void)
200 int i;
201 Dlg_head *ch_dlg;
203 do_refresh ();
204 end_chmod = c_file = need_update = 0;
205 single_set = (current_panel->marked < 2) ? 2 : 0;
207 ch_dlg =
208 create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
209 chmod_callback, "[Chmod]", _("Chmod command"),
210 DLG_CENTER | DLG_REVERSE);
212 for (i = 0; i < BUTTONS; i++) {
213 if (i == 2 && single_set)
214 break;
215 else
216 add_widget (ch_dlg,
217 button_new (BY + chmod_but[i].y - single_set,
218 BX + chmod_but[i].x,
219 chmod_but[i].ret_cmd,
220 chmod_but[i].flags,
221 _(chmod_but[i].text), 0));
224 for (i = 0; i < PERMISSIONS; i++) {
225 check_perm[i].check =
226 check_new (PY + (PERMISSIONS - i), PX + 2, 0,
227 _(check_perm[i].text));
228 add_widget (ch_dlg, check_perm[i].check);
231 return ch_dlg;
234 static void chmod_done (void)
236 if (need_update)
237 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
238 repaint_screen ();
241 static char *next_file (void)
243 while (!current_panel->dir.list[c_file].f.marked)
244 c_file++;
246 return current_panel->dir.list[c_file].fname;
249 static void do_chmod (struct stat *sf)
251 sf->st_mode &= and_mask;
252 sf->st_mode |= or_mask;
253 if (mc_chmod (current_panel->dir.list [c_file].fname, sf->st_mode) == -1)
254 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
255 current_panel->dir.list [c_file].fname, unix_error_string (errno));
257 do_file_mark (current_panel, c_file, 0);
260 static void apply_mask (struct stat *sf)
262 char *fname;
264 need_update = end_chmod = 1;
265 do_chmod (sf);
267 do {
268 fname = next_file ();
269 if (mc_stat (fname, sf) != 0)
270 return;
271 c_stat = sf->st_mode;
273 do_chmod (sf);
274 } while (current_panel->marked);
277 void chmod_cmd (void)
279 char buffer [BUF_TINY];
280 char *fname;
281 int i;
282 struct stat sf_stat;
283 Dlg_head *ch_dlg;
285 do { /* do while any files remaining */
286 ch_dlg = init_chmod ();
287 if (current_panel->marked)
288 fname = next_file (); /* next marked file */
289 else
290 fname = selection (current_panel)->fname; /* single file */
292 if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
293 destroy_dlg (ch_dlg);
294 break;
297 c_stat = sf_stat.st_mode;
298 mode_change = 0; /* clear changes flag */
300 /* set check buttons */
301 for (i = 0; i < PERMISSIONS; i++){
302 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
303 check_perm[i].selected = 0;
306 /* Set the labels */
307 c_fname = str_trunc (fname, 21);
308 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname));
309 c_fown = str_trunc (get_owner (sf_stat.st_uid), 21);
310 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown));
311 c_fgrp = str_trunc (get_group (sf_stat.st_gid), 21);
312 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp));
313 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
314 statl = label_new (FY+4, FX+2, buffer);
315 add_widget (ch_dlg, statl);
317 run_dlg (ch_dlg); /* retrieve an action */
319 /* do action */
320 switch (ch_dlg->ret_value){
321 case B_ENTER:
322 if (mode_change)
323 if (mc_chmod (fname, c_stat) == -1)
324 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
325 fname, unix_error_string (errno));
326 need_update = 1;
327 break;
329 case B_CANCEL:
330 end_chmod = 1;
331 break;
333 case B_ALL:
334 case B_MARKED:
335 and_mask = or_mask = 0;
336 and_mask = ~and_mask;
338 for (i = 0; i < PERMISSIONS; i++) {
339 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL) {
340 if (check_perm[i].check->state & C_BOOL)
341 or_mask |= check_perm[i].mode;
342 else
343 and_mask &= ~check_perm[i].mode;
347 apply_mask (&sf_stat);
348 break;
350 case B_SETMRK:
351 and_mask = or_mask = 0;
352 and_mask = ~and_mask;
354 for (i = 0; i < PERMISSIONS; i++) {
355 if (check_perm[i].selected)
356 or_mask |= check_perm[i].mode;
359 apply_mask (&sf_stat);
360 break;
361 case B_CLRMRK:
362 and_mask = or_mask = 0;
363 and_mask = ~and_mask;
365 for (i = 0; i < PERMISSIONS; i++) {
366 if (check_perm[i].selected)
367 and_mask &= ~check_perm[i].mode;
370 apply_mask (&sf_stat);
371 break;
374 if (current_panel->marked && ch_dlg->ret_value!=B_CANCEL) {
375 do_file_mark (current_panel, c_file, 0);
376 need_update = 1;
378 destroy_dlg (ch_dlg);
379 } while (current_panel->marked && !end_chmod);
380 chmod_done ();