* Makefile.am: Remove definition of CONFDIR, it's not used.
[midnight-commander.git] / src / chmod.c
blob29dbabeab3e3e7a616239a30bf511ad4561f7f05
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;
48 static struct Dlg_head *ch_dlg;
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 umode_t and_mask, or_mask, c_stat;
74 static char *c_fname, *c_fown, *c_fgrp;
76 static WLabel *statl;
77 static int normal_color;
78 static int title_color;
79 static int selection_color;
81 static struct {
82 mode_t mode;
83 char *text;
84 int selected;
85 WCheck *check;
86 } check_perm[PERMISSIONS] =
88 { S_IXOTH, N_("execute/search by others"), 0, 0, },
89 { S_IWOTH, N_("write by others"), 0, 0, },
90 { S_IROTH, N_("read by others"), 0, 0, },
91 { S_IXGRP, N_("execute/search by group"), 0, 0, },
92 { S_IWGRP, N_("write by group"), 0, 0, },
93 { S_IRGRP, N_("read by group"), 0, 0, },
94 { S_IXUSR, N_("execute/search by owner"), 0, 0, },
95 { S_IWUSR, N_("write by owner"), 0, 0, },
96 { S_IRUSR, N_("read by owner"), 0, 0, },
97 { S_ISVTX, N_("sticky bit"), 0, 0, },
98 { S_ISGID, N_("set group ID on execution"), 0, 0, },
99 { S_ISUID, N_("set user ID on execution"), 0, 0, },
102 static struct {
103 int ret_cmd, flags, y, x;
104 char *text;
105 } chmod_but[BUTTONS] =
107 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
108 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
109 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
110 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
111 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
112 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
115 static void chmod_toggle_select (void)
117 int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
119 attrset (normal_color);
120 check_perm[Id].selected ^= 1;
122 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
123 addch ((check_perm[Id].selected) ? '*' : ' ');
124 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
127 static void chmod_refresh (void)
129 attrset (COLOR_NORMAL);
130 dlg_erase (ch_dlg);
132 draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
133 draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
134 draw_box (ch_dlg, FY, FX, 10, 25);
136 dlg_move (ch_dlg, FY + 1, FX + 2);
137 addstr (_("Name"));
138 dlg_move (ch_dlg, FY + 3, FX + 2);
139 addstr (_("Permissions (Octal)"));
140 dlg_move (ch_dlg, FY + 5, FX + 2);
141 addstr (_("Owner name"));
142 dlg_move (ch_dlg, FY + 7, FX + 2);
143 addstr (_("Group name"));
145 attrset (title_color);
146 dlg_move (ch_dlg, 1, 28);
147 addstr (_(" Chmod command "));
148 dlg_move (ch_dlg, PY, PX + 1);
149 addstr (_(" Permission "));
150 dlg_move (ch_dlg, FY, FX + 1);
151 addstr (_(" File "));
153 attrset (selection_color);
155 dlg_move (ch_dlg, TY, TX);
156 addstr (_("Use SPACE to change"));
157 dlg_move (ch_dlg, TY + 1, TX);
158 addstr (_("an option, ARROW KEYS"));
159 dlg_move (ch_dlg, TY + 2, TX);
160 addstr (_("to move between options"));
161 dlg_move (ch_dlg, TY + 3, TX);
162 addstr (_("and T or INS to mark"));
165 static int chmod_callback (Dlg_head *h, int Par, int Msg)
167 char buffer [BUF_TINY];
169 switch (Msg) {
170 case DLG_ACTION:
171 if (Par >= BUTTONS - single_set * 2){
172 c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
173 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
174 label_set_text (statl, buffer);
175 chmod_toggle_select ();
176 mode_change = 1;
178 break;
180 case DLG_KEY:
181 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
182 ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
183 chmod_toggle_select ();
184 if (Par == KEY_IC)
185 dlg_one_down (ch_dlg);
186 return 1;
188 break;
190 case DLG_DRAW:
191 chmod_refresh ();
192 break;
194 return 0;
197 static void init_chmod (void)
199 int i;
201 do_refresh ();
202 end_chmod = c_file = need_update = 0;
203 single_set = (cpanel->marked < 2) ? 2 : 0;
205 if (use_colors){
206 normal_color = COLOR_NORMAL;
207 title_color = COLOR_HOT_NORMAL;
208 selection_color = COLOR_NORMAL;
209 } else {
210 normal_color = NORMAL_COLOR;
211 title_color = SELECTED_COLOR;
212 selection_color = SELECTED_COLOR;
215 ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
216 chmod_callback, "[Chmod]", "chmod", DLG_CENTER);
218 x_set_dialog_title (ch_dlg, _("Chmod command"));
220 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
221 chmod_but[i].ret_cmd, chmod_but[i].flags, _(chmod_but[i].text), 0, 0, NULL
223 for (i = 0; i < BUTTONS; i++) {
224 if (i == 2 && single_set)
225 break;
226 else
227 add_widget (ch_dlg, button_new (XTRACT (i)));
231 #define XTRACT2(i) 0, _(check_perm [i].text), NULL
232 for (i = 0; i < PERMISSIONS; i++) {
233 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
234 XTRACT2 (i));
235 add_widget (ch_dlg, check_perm[i].check);
239 int stat_file (char *filename, struct stat *st)
241 if (mc_stat (filename, st))
242 return 0;
244 return 1;
247 static void chmod_done (void)
249 if (need_update)
250 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
251 repaint_screen ();
254 static char *next_file (void)
256 while (!cpanel->dir.list[c_file].f.marked)
257 c_file++;
259 return cpanel->dir.list[c_file].fname;
262 static void do_chmod (struct stat *sf)
264 sf->st_mode &= and_mask;
265 sf->st_mode |= or_mask;
266 if (mc_chmod (cpanel->dir.list [c_file].fname, sf->st_mode) == -1)
267 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
268 cpanel->dir.list [c_file].fname, unix_error_string (errno));
270 do_file_mark (cpanel, c_file, 0);
273 static void apply_mask (struct stat *sf)
275 char *fname;
277 need_update = end_chmod = 1;
278 do_chmod (sf);
280 do {
281 fname = next_file ();
282 if (!stat_file (fname, sf))
283 return;
284 c_stat = sf->st_mode;
286 do_chmod (sf);
287 } while (cpanel->marked);
290 void chmod_cmd (void)
292 char buffer [BUF_TINY];
293 char *fname;
294 int i;
295 struct stat sf_stat;
297 do { /* do while any files remaining */
298 init_chmod ();
299 if (cpanel->marked)
300 fname = next_file (); /* next marked file */
301 else
302 fname = selection (cpanel)->fname; /* single file */
304 if (!stat_file (fname, &sf_stat)){ /* get status of file */
305 destroy_dlg (ch_dlg);
306 break;
309 c_stat = sf_stat.st_mode;
310 mode_change = 0; /* clear changes flag */
312 /* set check buttons */
313 for (i = 0; i < PERMISSIONS; i++){
314 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
315 check_perm[i].selected = 0;
318 /* Set the labels */
319 c_fname = name_trunc (fname, 21);
320 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
321 c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
322 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
323 c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
324 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
325 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
326 statl = label_new (FY+4, FX+2, buffer, NULL);
327 add_widget (ch_dlg, statl);
329 run_dlg (ch_dlg); /* retrieve an action */
331 /* do action */
332 switch (ch_dlg->ret_value){
333 case B_ENTER:
334 if (mode_change)
335 if (mc_chmod (fname, c_stat) == -1)
336 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
337 fname, unix_error_string (errno));
338 need_update = 1;
339 break;
341 case B_CANCEL:
342 end_chmod = 1;
343 break;
345 case B_ALL:
346 case B_MARKED:
347 and_mask = or_mask = 0;
348 and_mask = ~and_mask;
350 for (i = 0; i < PERMISSIONS; i++) {
351 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL) {
352 if (check_perm[i].check->state & C_BOOL)
353 or_mask |= check_perm[i].mode;
354 else
355 and_mask &= ~check_perm[i].mode;
359 apply_mask (&sf_stat);
360 break;
362 case B_SETMRK:
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 or_mask |= check_perm[i].mode;
371 apply_mask (&sf_stat);
372 break;
373 case B_CLRMRK:
374 and_mask = or_mask = 0;
375 and_mask = ~and_mask;
377 for (i = 0; i < PERMISSIONS; i++) {
378 if (check_perm[i].selected)
379 and_mask &= ~check_perm[i].mode;
382 apply_mask (&sf_stat);
383 break;
386 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
387 do_file_mark (cpanel, c_file, 0);
388 need_update = 1;
390 destroy_dlg (ch_dlg);
391 } while (cpanel->marked && !end_chmod);
392 chmod_done ();
395 void ch1_cmd (int id)
397 if (advanced_chfns)
398 chown_advanced_cmd ();
399 else
400 chmod_cmd ();
403 void ch2_cmd (int id)
405 if (advanced_chfns)
406 chown_advanced_cmd ();
407 else
408 chown_cmd ();