corrected some file operations dialog msgs
[midnight-commander.git] / src / chmod.c
blob7c29ca65f411bf76527d587d8c421d52118a2302
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include <config.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <errno.h> /* For errno on SunOS systems */
23 /* Needed for the extern declarations of integer parameters */
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
30 #include "global.h"
31 #include "tty.h"
32 #include "win.h"
33 #include "color.h"
34 #include "dlg.h"
35 #include "widget.h"
36 #include "dialog.h" /* For do_refresh() */
38 #include "dir.h"
39 #include "panel.h" /* Needed for the externs */
40 #include "file.h"
41 #include "main.h"
42 #include "chmod.h"
43 #include "achown.h"
44 #include "chown.h"
45 #include "../vfs/vfs.h"
47 static int single_set;
49 #define PX 5
50 #define PY 2
52 #define FX 40
53 #define FY 2
55 #define BX 6
56 #define BY 17
58 #define TX 40
59 #define TY 12
61 #define PERMISSIONS 12
62 #define BUTTONS 6
64 #define B_MARKED B_USER
65 #define B_ALL B_USER+1
66 #define B_SETMRK B_USER+2
67 #define B_CLRMRK B_USER+3
69 static int mode_change, need_update;
70 static int c_file, end_chmod;
72 static umode_t and_mask, or_mask, c_stat;
73 static char *c_fname, *c_fown, *c_fgrp;
75 static WLabel *statl;
77 static struct {
78 mode_t mode;
79 char *text;
80 int selected;
81 WCheck *check;
82 } check_perm[PERMISSIONS] =
84 { S_IXOTH, N_("execute/search by others"), 0, 0, },
85 { S_IWOTH, N_("write by others"), 0, 0, },
86 { S_IROTH, N_("read by others"), 0, 0, },
87 { S_IXGRP, N_("execute/search by group"), 0, 0, },
88 { S_IWGRP, N_("write by group"), 0, 0, },
89 { S_IRGRP, N_("read by group"), 0, 0, },
90 { S_IXUSR, N_("execute/search by owner"), 0, 0, },
91 { S_IWUSR, N_("write by owner"), 0, 0, },
92 { S_IRUSR, N_("read by owner"), 0, 0, },
93 { S_ISVTX, N_("sticky bit"), 0, 0, },
94 { S_ISGID, N_("set group ID on execution"), 0, 0, },
95 { S_ISUID, N_("set user ID on execution"), 0, 0, },
98 static struct {
99 int ret_cmd, flags, y, x;
100 char *text;
101 } chmod_but[BUTTONS] =
103 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
104 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
105 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
106 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
107 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
108 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
111 static void chmod_toggle_select (Dlg_head *h)
113 int Id = h->current->dlg_id - BUTTONS + single_set * 2;
115 attrset (COLOR_NORMAL);
116 check_perm[Id].selected ^= 1;
118 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
119 addch ((check_perm[Id].selected) ? '*' : ' ');
120 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
123 static void chmod_refresh (Dlg_head *h)
125 common_dialog_repaint (h);
127 attrset (COLOR_NORMAL);
129 draw_box (h, PY, PX, PERMISSIONS + 2, 33);
130 draw_box (h, FY, FX, 10, 25);
132 dlg_move (h, FY + 1, FX + 2);
133 addstr (_("Name"));
134 dlg_move (h, FY + 3, FX + 2);
135 addstr (_("Permissions (Octal)"));
136 dlg_move (h, FY + 5, FX + 2);
137 addstr (_("Owner name"));
138 dlg_move (h, FY + 7, FX + 2);
139 addstr (_("Group name"));
141 dlg_move (h, TY, TX);
142 addstr (_("Use SPACE to change"));
143 dlg_move (h, TY + 1, TX);
144 addstr (_("an option, ARROW KEYS"));
145 dlg_move (h, TY + 2, TX);
146 addstr (_("to move between options"));
147 dlg_move (h, TY + 3, TX);
148 addstr (_("and T or INS to mark"));
150 attrset (COLOR_HOT_NORMAL);
152 dlg_move (h, PY, PX + 1);
153 addstr (_(" Permission "));
154 dlg_move (h, FY, FX + 1);
155 addstr (_(" File "));
158 static int chmod_callback (Dlg_head *h, int Par, int Msg)
160 char buffer [BUF_TINY];
162 switch (Msg) {
163 case DLG_ACTION:
164 if (Par >= BUTTONS - single_set * 2){
165 c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
166 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
167 label_set_text (statl, buffer);
168 chmod_toggle_select (h);
169 mode_change = 1;
171 break;
173 case DLG_KEY:
174 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
175 h->current->dlg_id >= BUTTONS - single_set * 2) {
176 chmod_toggle_select (h);
177 if (Par == KEY_IC)
178 dlg_one_down (h);
179 return 1;
181 break;
183 case DLG_DRAW:
184 chmod_refresh (h);
185 break;
187 return 0;
190 Dlg_head *
191 init_chmod (void)
193 int i;
194 Dlg_head *ch_dlg;
196 do_refresh ();
197 end_chmod = c_file = need_update = 0;
198 single_set = (cpanel->marked < 2) ? 2 : 0;
200 ch_dlg =
201 create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
202 chmod_callback, "[Chmod]", _("Chmod command"),
203 DLG_CENTER);
205 for (i = 0; i < BUTTONS; i++) {
206 if (i == 2 && single_set)
207 break;
208 else
209 add_widget (ch_dlg,
210 button_new (BY + chmod_but[i].y - single_set,
211 BX + chmod_but[i].x,
212 chmod_but[i].ret_cmd,
213 chmod_but[i].flags,
214 _(chmod_but[i].text), 0, 0, NULL));
217 for (i = 0; i < PERMISSIONS; i++) {
218 check_perm[i].check =
219 check_new (PY + (PERMISSIONS - i), PX + 2, 0,
220 _(check_perm[i].text), NULL);
221 add_widget (ch_dlg, check_perm[i].check);
224 return ch_dlg;
227 int stat_file (char *filename, struct stat *st)
229 if (mc_stat (filename, st))
230 return 0;
232 return 1;
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 (!cpanel->dir.list[c_file].f.marked)
245 c_file++;
247 return cpanel->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 (cpanel->dir.list [c_file].fname, sf->st_mode) == -1)
255 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
256 cpanel->dir.list [c_file].fname, unix_error_string (errno));
258 do_file_mark (cpanel, 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 (!stat_file (fname, sf))
271 return;
272 c_stat = sf->st_mode;
274 do_chmod (sf);
275 } while (cpanel->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 (cpanel->marked)
289 fname = next_file (); /* next marked file */
290 else
291 fname = selection (cpanel)->fname; /* single file */
293 if (!stat_file (fname, &sf_stat)){ /* 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 = name_trunc (fname, 21);
309 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
310 c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
311 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
312 c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
313 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
314 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
315 statl = label_new (FY+4, FX+2, buffer, NULL);
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 (1, MSG_ERROR, _(" Couldn't 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 (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
376 do_file_mark (cpanel, c_file, 0);
377 need_update = 1;
379 destroy_dlg (ch_dlg);
380 } while (cpanel->marked && !end_chmod);
381 chmod_done ();