Merge branch '1396_with_search_engine'
[midnight-commander.git] / src / chmod.c
blobd9dfff19dd27fc4b5d1fd5d789cc84bdc2525df3
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, Widget *sender,
164 dlg_msg_t msg, int parm, void *data)
166 char buffer[BUF_TINY];
167 int id = h->current->dlg_id - BUTTONS + single_set * 2;
169 switch (msg) {
170 case DLG_ACTION:
171 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) {
182 chmod_toggle_select (h, id);
183 if (parm == KEY_IC)
184 dlg_one_down (h);
185 return MSG_HANDLED;
187 return MSG_NOT_HANDLED;
189 case DLG_DRAW:
190 chmod_refresh (h);
191 return MSG_HANDLED;
193 default:
194 return default_dlg_callback (h, sender, msg, parm, data);
198 static Dlg_head *
199 init_chmod (void)
201 int i;
202 Dlg_head *ch_dlg;
204 do_refresh ();
205 end_chmod = c_file = need_update = 0;
206 single_set = (current_panel->marked < 2) ? 2 : 0;
208 ch_dlg =
209 create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
210 chmod_callback, "[Chmod]", _("Chmod command"),
211 DLG_CENTER | DLG_REVERSE);
213 for (i = 0; i < BUTTONS; i++) {
214 if (i == 2 && single_set)
215 break;
216 else
217 add_widget (ch_dlg,
218 button_new (BY + chmod_but[i].y - single_set,
219 BX + chmod_but[i].x,
220 chmod_but[i].ret_cmd,
221 chmod_but[i].flags,
222 _(chmod_but[i].text), 0));
225 for (i = 0; i < PERMISSIONS; i++) {
226 check_perm[i].check =
227 check_new (PY + (PERMISSIONS - i), PX + 2, 0,
228 _(check_perm[i].text));
229 add_widget (ch_dlg, check_perm[i].check);
232 return ch_dlg;
235 static void chmod_done (void)
237 if (need_update)
238 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
239 repaint_screen ();
242 static char *next_file (void)
244 while (!current_panel->dir.list[c_file].f.marked)
245 c_file++;
247 return current_panel->dir.list[c_file].fname;
250 static void do_chmod (struct stat *sf)
252 sf->st_mode &= and_mask;
253 sf->st_mode |= or_mask;
254 if (mc_chmod (current_panel->dir.list [c_file].fname, sf->st_mode) == -1)
255 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
256 current_panel->dir.list [c_file].fname, unix_error_string (errno));
258 do_file_mark (current_panel, c_file, 0);
261 static void apply_mask (struct stat *sf)
263 char *fname;
265 need_update = end_chmod = 1;
266 do_chmod (sf);
268 do {
269 fname = next_file ();
270 if (mc_stat (fname, sf) != 0)
271 return;
272 c_stat = sf->st_mode;
274 do_chmod (sf);
275 } while (current_panel->marked);
278 void chmod_cmd (void)
280 char buffer [BUF_TINY];
281 char *fname;
282 int i;
283 struct stat sf_stat;
284 Dlg_head *ch_dlg;
286 do { /* do while any files remaining */
287 ch_dlg = init_chmod ();
288 if (current_panel->marked)
289 fname = next_file (); /* next marked file */
290 else
291 fname = selection (current_panel)->fname; /* single file */
293 if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
294 destroy_dlg (ch_dlg);
295 break;
298 c_stat = sf_stat.st_mode;
299 mode_change = 0; /* clear changes flag */
301 /* set check buttons */
302 for (i = 0; i < PERMISSIONS; i++){
303 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
304 check_perm[i].selected = 0;
307 /* Set the labels */
308 c_fname = str_trunc (fname, 21);
309 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname));
310 c_fown = str_trunc (get_owner (sf_stat.st_uid), 21);
311 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown));
312 c_fgrp = str_trunc (get_group (sf_stat.st_gid), 21);
313 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp));
314 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
315 statl = label_new (FY+4, FX+2, buffer);
316 add_widget (ch_dlg, statl);
318 run_dlg (ch_dlg); /* retrieve an action */
320 /* do action */
321 switch (ch_dlg->ret_value){
322 case B_ENTER:
323 if (mode_change)
324 if (mc_chmod (fname, c_stat) == -1)
325 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
326 fname, unix_error_string (errno));
327 need_update = 1;
328 break;
330 case B_CANCEL:
331 end_chmod = 1;
332 break;
334 case B_ALL:
335 case B_MARKED:
336 and_mask = or_mask = 0;
337 and_mask = ~and_mask;
339 for (i = 0; i < PERMISSIONS; i++) {
340 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL) {
341 if (check_perm[i].check->state & C_BOOL)
342 or_mask |= check_perm[i].mode;
343 else
344 and_mask &= ~check_perm[i].mode;
348 apply_mask (&sf_stat);
349 break;
351 case B_SETMRK:
352 and_mask = or_mask = 0;
353 and_mask = ~and_mask;
355 for (i = 0; i < PERMISSIONS; i++) {
356 if (check_perm[i].selected)
357 or_mask |= check_perm[i].mode;
360 apply_mask (&sf_stat);
361 break;
362 case B_CLRMRK:
363 and_mask = or_mask = 0;
364 and_mask = ~and_mask;
366 for (i = 0; i < PERMISSIONS; i++) {
367 if (check_perm[i].selected)
368 and_mask &= ~check_perm[i].mode;
371 apply_mask (&sf_stat);
372 break;
375 if (current_panel->marked && ch_dlg->ret_value!=B_CANCEL) {
376 do_file_mark (current_panel, c_file, 0);
377 need_update = 1;
379 destroy_dlg (ch_dlg);
380 } while (current_panel->marked && !end_chmod);
381 chmod_done ();