* sldisply.c (SLtt_initialize): Fix typo. Change Is_Bg_BGR
[midnight-commander.git] / src / chmod.c
blob14b5e9a9dd0341efe200e540e8b305b0c77e7a3a
1 /* Chmod command -- for the Midnight Commander
2 Copyright (C) 1994 Radek Doulik
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
29 #include "global.h"
30 #include "tty.h" /* A_REVERSE */
31 #include "color.h" /* dialog_colors */
32 #include "dialog.h" /* add_widget() */
33 #include "widget.h" /* NORMAL_BUTTON */
34 #include "wtools.h" /* message() */
35 #include "panel.h" /* do_file_mark() */
36 #include "main.h" /* update_panels() */
37 #include "chmod.h"
39 static int single_set;
41 #define PX 5
42 #define PY 2
44 #define FX 40
45 #define FY 2
47 #define BX 6
48 #define BY 17
50 #define TX 40
51 #define TY 12
53 #define PERMISSIONS 12
54 #define BUTTONS 6
56 #define B_MARKED B_USER
57 #define B_ALL (B_USER+1)
58 #define B_SETMRK (B_USER+2)
59 #define B_CLRMRK (B_USER+3)
61 static int mode_change, need_update;
62 static int c_file, end_chmod;
64 static mode_t and_mask, or_mask, c_stat;
66 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
67 * returns a pointer to a static buffer, and label_new creates its own copy
68 * of its argument)
69 * --rillig, 2004-08-29 */
70 static const char *c_fname, *c_fown, *c_fgrp;
72 static WLabel *statl;
74 static struct {
75 mode_t mode;
76 const char *text;
77 int selected;
78 WCheck *check;
79 } check_perm[PERMISSIONS] =
81 { S_IXOTH, N_("execute/search by others"), 0, 0, },
82 { S_IWOTH, N_("write by others"), 0, 0, },
83 { S_IROTH, N_("read by others"), 0, 0, },
84 { S_IXGRP, N_("execute/search by group"), 0, 0, },
85 { S_IWGRP, N_("write by group"), 0, 0, },
86 { S_IRGRP, N_("read by group"), 0, 0, },
87 { S_IXUSR, N_("execute/search by owner"), 0, 0, },
88 { S_IWUSR, N_("write by owner"), 0, 0, },
89 { S_IRUSR, N_("read by owner"), 0, 0, },
90 { S_ISVTX, N_("sticky bit"), 0, 0, },
91 { S_ISGID, N_("set group ID on execution"), 0, 0, },
92 { S_ISUID, N_("set user ID on execution"), 0, 0, },
95 static struct {
96 int ret_cmd, flags, y, x;
97 const char *text;
98 } chmod_but[BUTTONS] =
100 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
101 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
102 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
103 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
104 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
105 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
108 static void chmod_toggle_select (Dlg_head *h, int Id)
110 attrset (COLOR_NORMAL);
111 check_perm[Id].selected ^= 1;
113 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
114 addch ((check_perm[Id].selected) ? '*' : ' ');
115 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
118 static void chmod_refresh (Dlg_head *h)
120 common_dialog_repaint (h);
122 attrset (COLOR_NORMAL);
124 draw_box (h, PY, PX, PERMISSIONS + 2, 33);
125 draw_box (h, FY, FX, 10, 25);
127 dlg_move (h, FY + 1, FX + 2);
128 addstr (_("Name"));
129 dlg_move (h, FY + 3, FX + 2);
130 addstr (_("Permissions (Octal)"));
131 dlg_move (h, FY + 5, FX + 2);
132 addstr (_("Owner name"));
133 dlg_move (h, FY + 7, FX + 2);
134 addstr (_("Group name"));
136 dlg_move (h, TY, TX);
137 addstr (_("Use SPACE to change"));
138 dlg_move (h, TY + 1, TX);
139 addstr (_("an option, ARROW KEYS"));
140 dlg_move (h, TY + 2, TX);
141 addstr (_("to move between options"));
142 dlg_move (h, TY + 3, TX);
143 addstr (_("and T or INS to mark"));
145 attrset (COLOR_HOT_NORMAL);
147 dlg_move (h, PY, PX + 1);
148 addstr (_(" Permission "));
149 dlg_move (h, FY, FX + 1);
150 addstr (_(" File "));
153 static cb_ret_t
154 chmod_callback (Dlg_head *h, dlg_msg_t msg, int parm)
156 char buffer[BUF_TINY];
157 int id = h->current->dlg_id - BUTTONS + single_set * 2;
159 switch (msg) {
160 case DLG_ACTION:
161 if (id >= 0) {
162 c_stat ^= check_perm[id].mode;
163 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
164 label_set_text (statl, buffer);
165 chmod_toggle_select (h, id);
166 mode_change = 1;
168 return MSG_HANDLED;
170 case DLG_KEY:
171 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0) {
172 chmod_toggle_select (h, id);
173 if (parm == KEY_IC)
174 dlg_one_down (h);
175 return MSG_HANDLED;
177 return MSG_NOT_HANDLED;
179 case DLG_DRAW:
180 chmod_refresh (h);
181 return MSG_HANDLED;
183 default:
184 return default_dlg_callback (h, msg, parm);
188 static Dlg_head *
189 init_chmod (void)
191 int i;
192 Dlg_head *ch_dlg;
194 do_refresh ();
195 end_chmod = c_file = need_update = 0;
196 single_set = (current_panel->marked < 2) ? 2 : 0;
198 ch_dlg =
199 create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
200 chmod_callback, "[Chmod]", _("Chmod command"),
201 DLG_CENTER | DLG_REVERSE);
203 for (i = 0; i < BUTTONS; i++) {
204 if (i == 2 && single_set)
205 break;
206 else
207 add_widget (ch_dlg,
208 button_new (BY + chmod_but[i].y - single_set,
209 BX + chmod_but[i].x,
210 chmod_but[i].ret_cmd,
211 chmod_but[i].flags,
212 _(chmod_but[i].text), 0));
215 for (i = 0; i < PERMISSIONS; i++) {
216 check_perm[i].check =
217 check_new (PY + (PERMISSIONS - i), PX + 2, 0,
218 _(check_perm[i].text));
219 add_widget (ch_dlg, check_perm[i].check);
222 return ch_dlg;
225 static void chmod_done (void)
227 if (need_update)
228 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
229 repaint_screen ();
232 static char *next_file (void)
234 while (!current_panel->dir.list[c_file].f.marked)
235 c_file++;
237 return current_panel->dir.list[c_file].fname;
240 static void do_chmod (struct stat *sf)
242 sf->st_mode &= and_mask;
243 sf->st_mode |= or_mask;
244 if (mc_chmod (current_panel->dir.list [c_file].fname, sf->st_mode) == -1)
245 message (1, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
246 current_panel->dir.list [c_file].fname, unix_error_string (errno));
248 do_file_mark (current_panel, c_file, 0);
251 static void apply_mask (struct stat *sf)
253 char *fname;
255 need_update = end_chmod = 1;
256 do_chmod (sf);
258 do {
259 fname = next_file ();
260 if (mc_stat (fname, sf) != 0)
261 return;
262 c_stat = sf->st_mode;
264 do_chmod (sf);
265 } while (current_panel->marked);
268 void chmod_cmd (void)
270 char buffer [BUF_TINY];
271 char *fname;
272 int i;
273 struct stat sf_stat;
274 Dlg_head *ch_dlg;
276 do { /* do while any files remaining */
277 ch_dlg = init_chmod ();
278 if (current_panel->marked)
279 fname = next_file (); /* next marked file */
280 else
281 fname = selection (current_panel)->fname; /* single file */
283 if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
284 destroy_dlg (ch_dlg);
285 break;
288 c_stat = sf_stat.st_mode;
289 mode_change = 0; /* clear changes flag */
291 /* set check buttons */
292 for (i = 0; i < PERMISSIONS; i++){
293 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
294 check_perm[i].selected = 0;
297 /* Set the labels */
298 c_fname = name_trunc (fname, 21);
299 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname));
300 c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
301 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown));
302 c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
303 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp));
304 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
305 statl = label_new (FY+4, FX+2, buffer);
306 add_widget (ch_dlg, statl);
308 run_dlg (ch_dlg); /* retrieve an action */
310 /* do action */
311 switch (ch_dlg->ret_value){
312 case B_ENTER:
313 if (mode_change)
314 if (mc_chmod (fname, c_stat) == -1)
315 message (1, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
316 fname, unix_error_string (errno));
317 need_update = 1;
318 break;
320 case B_CANCEL:
321 end_chmod = 1;
322 break;
324 case B_ALL:
325 case B_MARKED:
326 and_mask = or_mask = 0;
327 and_mask = ~and_mask;
329 for (i = 0; i < PERMISSIONS; i++) {
330 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL) {
331 if (check_perm[i].check->state & C_BOOL)
332 or_mask |= check_perm[i].mode;
333 else
334 and_mask &= ~check_perm[i].mode;
338 apply_mask (&sf_stat);
339 break;
341 case B_SETMRK:
342 and_mask = or_mask = 0;
343 and_mask = ~and_mask;
345 for (i = 0; i < PERMISSIONS; i++) {
346 if (check_perm[i].selected)
347 or_mask |= check_perm[i].mode;
350 apply_mask (&sf_stat);
351 break;
352 case B_CLRMRK:
353 and_mask = or_mask = 0;
354 and_mask = ~and_mask;
356 for (i = 0; i < PERMISSIONS; i++) {
357 if (check_perm[i].selected)
358 and_mask &= ~check_perm[i].mode;
361 apply_mask (&sf_stat);
362 break;
365 if (current_panel->marked && ch_dlg->ret_value!=B_CANCEL) {
366 do_file_mark (current_panel, c_file, 0);
367 need_update = 1;
369 destroy_dlg (ch_dlg);
370 } while (current_panel->marked && !end_chmod);
371 chmod_done ();