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>
38 #include "dialog.h" /* For do_refresh() */
41 #include "panel.h" /* Needed for the externs */
47 #include "../vfs/vfs.h"
53 static int single_set
;
54 struct Dlg_head
*ch_dlg
;
68 #define PERMISSIONS 12
71 #define B_MARKED B_USER
72 #define B_ALL B_USER+1
73 #define B_SETMRK B_USER+2
74 #define B_CLRMRK B_USER+3
76 int mode_change
, need_update
;
77 int c_file
, end_chmod
;
79 umode_t and_mask
, or_mask
, c_stat
;
80 char *c_fname
, *c_fown
, *c_fgrp
, *c_fperm
;
84 static int normal_color
;
85 static int title_color
;
86 static int selection_color
;
93 } check_perm
[PERMISSIONS
] =
95 { S_IXOTH
, N_("execute/search by others"), 0, 0, },
96 { S_IWOTH
, N_("write by others"), 0, 0, },
97 { S_IROTH
, N_("read by others"), 0, 0, },
98 { S_IXGRP
, N_("execute/search by group"), 0, 0, },
99 { S_IWGRP
, N_("write by group"), 0, 0, },
100 { S_IRGRP
, N_("read by group"), 0, 0, },
101 { S_IXUSR
, N_("execute/search by owner"), 0, 0, },
102 { S_IWUSR
, N_("write by owner"), 0, 0, },
103 { S_IRUSR
, N_("read by owner"), 0, 0, },
104 { S_ISVTX
, N_("sticky bit"), 0, 0, },
105 { S_ISGID
, N_("set group ID on execution"), 0, 0, },
106 { S_ISUID
, N_("set user ID on execution"), 0, 0, },
110 int ret_cmd
, flags
, y
, x
;
112 } chmod_but
[BUTTONS
] =
114 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
115 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
116 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
117 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
118 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
119 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
123 static void chmod_toggle_select (void)
128 static void chmod_toggle_select (void)
130 int Id
= ch_dlg
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
132 attrset (normal_color
);
133 check_perm
[Id
].selected
^= 1;
135 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
136 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
137 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
140 static void chmod_refresh (void)
142 attrset (COLOR_NORMAL
);
145 draw_box (ch_dlg
, 1, 2, 20 - single_set
, 66);
146 draw_box (ch_dlg
, PY
, PX
, PERMISSIONS
+ 2, 33);
147 draw_box (ch_dlg
, FY
, FX
, 10, 25);
149 dlg_move (ch_dlg
, FY
+ 1, FX
+ 2);
151 dlg_move (ch_dlg
, FY
+ 3, FX
+ 2);
152 addstr (_("Permissions (Octal)"));
153 dlg_move (ch_dlg
, FY
+ 5, FX
+ 2);
154 addstr (_("Owner name"));
155 dlg_move (ch_dlg
, FY
+ 7, FX
+ 2);
156 addstr (_("Group name"));
158 attrset (title_color
);
159 dlg_move (ch_dlg
, 1, 28);
160 addstr (_(" Chmod command "));
161 dlg_move (ch_dlg
, PY
, PX
+ 1);
162 addstr (_(" Permission "));
163 dlg_move (ch_dlg
, FY
, FX
+ 1);
164 addstr (_(" File "));
166 attrset (selection_color
);
168 dlg_move (ch_dlg
, TY
, TX
);
169 addstr (_("Use SPACE to change"));
170 dlg_move (ch_dlg
, TY
+ 1, TX
);
171 addstr (_("an option, ARROW KEYS"));
172 dlg_move (ch_dlg
, TY
+ 2, TX
);
173 addstr (_("to move between options"));
174 dlg_move (ch_dlg
, TY
+ 3, TX
);
175 addstr (_("and T or INS to mark"));
179 static int chmod_callback (Dlg_head
*h
, int Par
, int Msg
)
181 char buffer
[BUF_TINY
];
185 if (Par
>= BUTTONS
- single_set
* 2){
186 c_stat
^= check_perm
[Par
- BUTTONS
+ single_set
* 2].mode
;
187 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
188 label_set_text (statl
, buffer
);
189 chmod_toggle_select ();
195 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
196 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
197 chmod_toggle_select ();
199 dlg_one_down (ch_dlg
);
212 static void init_chmod (void)
217 end_chmod
= c_file
= need_update
= 0;
218 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
221 normal_color
= COLOR_NORMAL
;
222 title_color
= COLOR_HOT_NORMAL
;
223 selection_color
= COLOR_NORMAL
;
225 normal_color
= NORMAL_COLOR
;
226 title_color
= SELECTED_COLOR
;
227 selection_color
= SELECTED_COLOR
;
230 ch_dlg
= create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
231 chmod_callback
, "[Chmod]", "chmod", DLG_CENTER
);
233 x_set_dialog_title (ch_dlg
, _("Chmod command"));
235 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
236 chmod_but[i].ret_cmd, chmod_but[i].flags, _(chmod_but[i].text), 0, 0, NULL
238 for (i
= 0; i
< BUTTONS
; i
++) {
239 if (i
== 2 && single_set
)
242 add_widgetl (ch_dlg
, button_new (XTRACT (i
)), XV_WLAY_RIGHTOF
);
246 #define XTRACT2(i) 0, _(check_perm [i].text), NULL
247 for (i
= 0; i
< PERMISSIONS
; i
++) {
248 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2,
250 add_widget (ch_dlg
, check_perm
[i
].check
);
254 int stat_file (char *filename
, struct stat
*st
)
256 if (mc_stat (filename
, st
))
258 if (!(S_ISREG(st
->st_mode
) || S_ISDIR(st
->st_mode
) ||
259 S_ISLNK(st
->st_mode
)))
265 static void chmod_done (void)
268 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
272 static char *next_file (void)
274 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
277 return cpanel
->dir
.list
[c_file
].fname
;
280 static void do_chmod (struct stat
*sf
)
282 sf
->st_mode
&= and_mask
;
283 sf
->st_mode
|= or_mask
;
284 if (mc_chmod (cpanel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
285 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
286 cpanel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
288 do_file_mark (cpanel
, c_file
, 0);
291 static void apply_mask (struct stat
*sf
)
295 need_update
= end_chmod
= 1;
299 fname
= next_file ();
300 if (!stat_file (fname
, sf
))
302 c_stat
= sf
->st_mode
;
305 } while (cpanel
->marked
);
308 void chmod_cmd (void)
310 char buffer
[BUF_TINY
];
316 /* Don't do things like this: you do not want to enumerate all
317 filesystems that can not support chmod, here. */
318 if (!vfs_current_is_local ()) {
319 if (vfs_current_is_extfs ()) {
320 message (1, _(" Oops... "),
321 _(" I can't run the Chmod command on an extfs "));
323 } else if (vfs_current_is_tarfs ()) {
324 message (1, _(" Oops... "),
325 (" I can't run the Chmod command on a tarfs "));
331 do { /* do while any files remaining */
334 fname
= next_file (); /* next marked file */
336 fname
= selection (cpanel
)->fname
; /* single file */
338 if (!stat_file (fname
, &sf_stat
)){ /* get status of file */
339 destroy_dlg (ch_dlg
);
343 c_stat
= sf_stat
.st_mode
;
344 mode_change
= 0; /* clear changes flag */
346 /* set check buttons */
347 for (i
= 0; i
< PERMISSIONS
; i
++){
348 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
349 check_perm
[i
].selected
= 0;
353 c_fname
= name_trunc (fname
, 21);
354 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
355 c_fown
= name_trunc (get_owner (sf_stat
.st_uid
), 21);
356 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
357 c_fgrp
= name_trunc (get_group (sf_stat
.st_gid
), 21);
358 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
, NULL
));
359 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
360 statl
= label_new (FY
+4, FX
+2, buffer
, NULL
);
361 add_widget (ch_dlg
, statl
);
363 run_dlg (ch_dlg
); /* retrieve an action */
366 switch (ch_dlg
->ret_value
){
369 if (mc_chmod (fname
, c_stat
) == -1)
370 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
371 fname
, unix_error_string (errno
));
381 and_mask
= or_mask
= 0;
382 and_mask
= ~and_mask
;
384 for (i
= 0; i
< PERMISSIONS
; i
++) {
385 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
386 if (check_perm
[i
].check
->state
& C_BOOL
)
387 or_mask
|= check_perm
[i
].mode
;
389 and_mask
&= ~check_perm
[i
].mode
;
392 apply_mask (&sf_stat
);
396 and_mask
= or_mask
= 0;
397 and_mask
= ~and_mask
;
399 for (i
= 0; i
< PERMISSIONS
; i
++) {
400 if (check_perm
[i
].selected
)
401 or_mask
|= check_perm
[i
].mode
;
404 apply_mask (&sf_stat
);
407 and_mask
= or_mask
= 0;
408 and_mask
= ~and_mask
;
410 for (i
= 0; i
< PERMISSIONS
; i
++) {
411 if (check_perm
[i
].selected
)
412 and_mask
&= ~check_perm
[i
].mode
;
415 apply_mask (&sf_stat
);
419 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
420 do_file_mark (cpanel
, c_file
, 0);
423 destroy_dlg (ch_dlg
);
424 } while (cpanel
->marked
&& !end_chmod
);
428 void ch1_cmd (int id
)
431 chown_advanced_cmd ();
436 void ch2_cmd (int id
)
439 chown_advanced_cmd ();