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., 675 Mass Ave, Cambridge, MA 02139, USA.
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/param.h>
39 #include "dialog.h" /* For do_refresh() */
42 #include "panel.h" /* Needed for the externs */
48 #include "../vfs/vfs.h"
54 static int single_set
;
55 struct Dlg_head
*ch_dlg
;
69 #define PERMISSIONS 12
72 #define B_MARKED B_USER
73 #define B_ALL B_USER+1
74 #define B_SETMRK B_USER+2
75 #define B_CLRMRK B_USER+3
77 int mode_change
, need_update
;
78 int c_file
, end_chmod
;
80 umode_t and_mask
, or_mask
, c_stat
;
81 char *c_fname
, *c_fown
, *c_fgrp
, *c_fperm
;
85 static int normal_color
;
86 static int title_color
;
87 static int selection_color
;
94 } check_perm
[PERMISSIONS
] =
96 { S_IXOTH
, N_("execute/search by others"), 0, 0, },
97 { S_IWOTH
, N_("write by others"), 0, 0, },
98 { S_IROTH
, N_("read by others"), 0, 0, },
99 { S_IXGRP
, N_("execute/search by group"), 0, 0, },
100 { S_IWGRP
, N_("write by group"), 0, 0, },
101 { S_IRGRP
, N_("read by group"), 0, 0, },
102 { S_IXUSR
, N_("execute/search by owner"), 0, 0, },
103 { S_IWUSR
, N_("write by owner"), 0, 0, },
104 { S_IRUSR
, N_("read by owner"), 0, 0, },
105 { S_ISVTX
, N_("sticky bit"), 0, 0, },
106 { S_ISGID
, N_("set group ID on execution"), 0, 0, },
107 { S_ISUID
, N_("set user ID on execution"), 0, 0, },
111 int ret_cmd
, flags
, y
, x
;
113 } chmod_but
[BUTTONS
] =
115 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
116 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
117 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
118 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
119 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
120 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
124 static void chmod_toggle_select (void)
127 char *wn
= (char *) ch_dlg
->current
->widget
->wdata
;
128 int id
= ch_dlg
->current
->dlg_id
-BUTTONS
+ single_set
* 2;
130 check_perm
[id
].selected
^= 1;
132 tk_evalf ("%s configure -color $setup(%s)",
133 wn
+1, check_perm
[id
].selected
? "marked":"black");
138 static void chmod_toggle_select (void)
140 int Id
= ch_dlg
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
142 attrset (normal_color
);
143 check_perm
[Id
].selected
^= 1;
145 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
146 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
147 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
150 static void chmod_refresh (void)
152 attrset (COLOR_NORMAL
);
155 draw_box (ch_dlg
, 1, 2, 20 - single_set
, 66);
156 draw_box (ch_dlg
, PY
, PX
, PERMISSIONS
+ 2, 33);
157 draw_box (ch_dlg
, FY
, FX
, 10, 25);
159 dlg_move (ch_dlg
, FY
+ 1, FX
+ 2);
161 dlg_move (ch_dlg
, FY
+ 3, FX
+ 2);
162 addstr (_("Permissions (Octal)"));
163 dlg_move (ch_dlg
, FY
+ 5, FX
+ 2);
164 addstr (_("Owner name"));
165 dlg_move (ch_dlg
, FY
+ 7, FX
+ 2);
166 addstr (_("Group name"));
168 attrset (title_color
);
169 dlg_move (ch_dlg
, 1, 28);
170 addstr (_(" Chmod command "));
171 dlg_move (ch_dlg
, PY
, PX
+ 1);
172 addstr (_(" Permission "));
173 dlg_move (ch_dlg
, FY
, FX
+ 1);
174 addstr (_(" File "));
176 attrset (selection_color
);
178 dlg_move (ch_dlg
, TY
, TX
);
179 addstr (_("Use SPACE to change"));
180 dlg_move (ch_dlg
, TY
+ 1, TX
);
181 addstr (_("an option, ARROW KEYS"));
182 dlg_move (ch_dlg
, TY
+ 2, TX
);
183 addstr (_("to move between options"));
184 dlg_move (ch_dlg
, TY
+ 3, TX
);
185 addstr (_("and T or INS to mark"));
189 static int chmod_callback (Dlg_head
*h
, int Par
, int Msg
)
195 if (Par
>= BUTTONS
- single_set
* 2){
196 c_stat
^= check_perm
[Par
- BUTTONS
+ single_set
* 2].mode
;
197 sprintf (buffer
, "%o", c_stat
);
198 label_set_text (statl
, buffer
);
199 chmod_toggle_select ();
205 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
206 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
207 chmod_toggle_select ();
209 dlg_one_down (ch_dlg
);
222 static void init_chmod (void)
227 end_chmod
= c_file
= need_update
= 0;
228 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
231 normal_color
= COLOR_NORMAL
;
232 title_color
= COLOR_HOT_NORMAL
;
233 selection_color
= COLOR_NORMAL
;
235 normal_color
= NORMAL_COLOR
;
236 title_color
= SELECTED_COLOR
;
237 selection_color
= SELECTED_COLOR
;
240 ch_dlg
= create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
241 chmod_callback
, "[Chmod]", "chmod", DLG_CENTER
);
243 x_set_dialog_title (ch_dlg
, _("Chmod command"));
245 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
246 chmod_but[i].ret_cmd, chmod_but[i].flags, _(chmod_but[i].text), 0, 0, NULL
248 tk_new_frame (ch_dlg
, "b.");
249 for (i
= 0; i
< BUTTONS
; i
++) {
250 if (i
== 2 && single_set
)
253 add_widgetl (ch_dlg
, button_new (XTRACT (i
)), XV_WLAY_RIGHTOF
);
257 #define XTRACT2(i) 0, _(check_perm [i].text), NULL
258 tk_new_frame (ch_dlg
, "c.");
259 for (i
= 0; i
< PERMISSIONS
; i
++) {
260 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2,
262 add_widget (ch_dlg
, check_perm
[i
].check
);
266 int stat_file (char *filename
, struct stat
*st
)
268 if (mc_stat (filename
, st
))
270 if (!(S_ISREG(st
->st_mode
) || S_ISDIR(st
->st_mode
) ||
271 S_ISLNK(st
->st_mode
)))
277 static void chmod_done (void)
280 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
284 static char *next_file (void)
286 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
289 return cpanel
->dir
.list
[c_file
].fname
;
292 static void do_chmod (struct stat
*sf
)
294 sf
->st_mode
&= and_mask
;
295 sf
->st_mode
|= or_mask
;
296 if (mc_chmod (cpanel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
297 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
298 cpanel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
300 do_file_mark (cpanel
, c_file
, 0);
303 static void apply_mask (struct stat
*sf
)
307 need_update
= end_chmod
= 1;
311 fname
= next_file ();
312 if (!stat_file (fname
, sf
))
314 c_stat
= sf
->st_mode
;
317 } while (cpanel
->marked
);
320 void chmod_cmd (void)
327 if (!vfs_current_is_local ()) {
328 if (vfs_current_is_extfs ()) {
329 message (1, _(" Oops... "),
330 _(" I can't run the Chmod command on an extfs "));
332 } else if (vfs_current_is_tarfs ()) {
333 message (1, _(" Oops... "),
334 (" I can't run the Chmod command on a tarfs "));
339 do { /* do while any files remaining */
342 fname
= next_file (); /* next marked file */
344 fname
= selection (cpanel
)->fname
; /* single file */
346 if (!stat_file (fname
, &sf_stat
)){ /* get status of file */
347 destroy_dlg (ch_dlg
);
351 c_stat
= sf_stat
.st_mode
;
352 mode_change
= 0; /* clear changes flag */
354 /* set check buttons */
355 for (i
= 0; i
< PERMISSIONS
; i
++){
356 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
357 check_perm
[i
].selected
= 0;
360 tk_new_frame (ch_dlg
, "l.");
362 c_fname
= name_trunc (fname
, 21);
363 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
364 c_fown
= name_trunc (get_owner (sf_stat
.st_uid
), 21);
365 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
366 c_fgrp
= name_trunc (get_group (sf_stat
.st_gid
), 21);
367 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
, NULL
));
368 sprintf (buffer
, "%o", c_stat
);
369 statl
= label_new (FY
+4, FX
+2, buffer
, NULL
);
370 add_widget (ch_dlg
, statl
);
373 run_dlg (ch_dlg
); /* retrieve an action */
376 switch (ch_dlg
->ret_value
){
379 if (mc_chmod (fname
, c_stat
) == -1)
380 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
381 fname
, unix_error_string (errno
));
391 and_mask
= or_mask
= 0;
392 and_mask
= ~and_mask
;
394 for (i
= 0; i
< PERMISSIONS
; i
++) {
395 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
396 if (check_perm
[i
].check
->state
& C_BOOL
)
397 or_mask
|= check_perm
[i
].mode
;
399 and_mask
&= ~check_perm
[i
].mode
;
402 apply_mask (&sf_stat
);
406 and_mask
= or_mask
= 0;
407 and_mask
= ~and_mask
;
409 for (i
= 0; i
< PERMISSIONS
; i
++) {
410 if (check_perm
[i
].selected
)
411 or_mask
|= check_perm
[i
].mode
;
414 apply_mask (&sf_stat
);
417 and_mask
= or_mask
= 0;
418 and_mask
= ~and_mask
;
420 for (i
= 0; i
< PERMISSIONS
; i
++) {
421 if (check_perm
[i
].selected
)
422 and_mask
&= ~check_perm
[i
].mode
;
425 apply_mask (&sf_stat
);
429 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
430 do_file_mark (cpanel
, c_file
, 0);
433 destroy_dlg (ch_dlg
);
434 } while (cpanel
->marked
&& !end_chmod
);
438 void ch1_cmd (int id
)
441 chown_advanced_cmd ();
446 void ch2_cmd (int id
)
449 chown_advanced_cmd ();