1 /* Chmod command -- for the Midnight Commander
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * \brief Source: chmod command
30 #include <sys/types.h>
36 #include "../src/tty/tty.h"
37 #include "../src/skin/skin.h"
39 #include "dialog.h" /* add_widget() */
40 #include "widget.h" /* NORMAL_BUTTON */
41 #include "wtools.h" /* message() */
42 #include "panel.h" /* do_file_mark() */
43 #include "main.h" /* update_panels() */
44 #include "layout.h" /* repaint_screen() */
48 static int single_set
;
62 #define PERMISSIONS 12
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 mode_t and_mask
, or_mask
, c_stat
;
75 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
76 * returns a pointer to a static buffer, and label_new creates its own copy
78 * --rillig, 2004-08-29 */
79 static const char *c_fname
, *c_fown
, *c_fgrp
;
88 } check_perm
[PERMISSIONS
] =
90 { S_IXOTH
, N_("execute/search by others"), 0, 0, },
91 { S_IWOTH
, N_("write by others"), 0, 0, },
92 { S_IROTH
, N_("read by others"), 0, 0, },
93 { S_IXGRP
, N_("execute/search by group"), 0, 0, },
94 { S_IWGRP
, N_("write by group"), 0, 0, },
95 { S_IRGRP
, N_("read by group"), 0, 0, },
96 { S_IXUSR
, N_("execute/search by owner"), 0, 0, },
97 { S_IWUSR
, N_("write by owner"), 0, 0, },
98 { S_IRUSR
, N_("read by owner"), 0, 0, },
99 { S_ISVTX
, N_("sticky bit"), 0, 0, },
100 { S_ISGID
, N_("set group ID on execution"), 0, 0, },
101 { S_ISUID
, N_("set user ID on execution"), 0, 0, },
105 int ret_cmd
, flags
, y
, x
;
107 } chmod_but
[BUTTONS
] =
109 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
110 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
111 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
112 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
113 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
114 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
117 static void chmod_toggle_select (Dlg_head
*h
, int Id
)
119 tty_setcolor (COLOR_NORMAL
);
120 check_perm
[Id
].selected
^= 1;
122 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
123 tty_print_char ((check_perm
[Id
].selected
) ? '*' : ' ');
124 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
127 static void chmod_refresh (Dlg_head
*h
)
129 common_dialog_repaint (h
);
131 tty_setcolor (COLOR_NORMAL
);
133 draw_box (h
, PY
, PX
, PERMISSIONS
+ 2, 33);
134 draw_box (h
, FY
, FX
, 10, 25);
136 dlg_move (h
, FY
+ 1, FX
+ 2);
137 tty_print_string (_("Name"));
138 dlg_move (h
, FY
+ 3, FX
+ 2);
139 tty_print_string (_("Permissions (Octal)"));
140 dlg_move (h
, FY
+ 5, FX
+ 2);
141 tty_print_string (_("Owner name"));
142 dlg_move (h
, FY
+ 7, FX
+ 2);
143 tty_print_string (_("Group name"));
145 dlg_move (h
, TY
, TX
);
146 tty_print_string (_("Use SPACE to change"));
147 dlg_move (h
, TY
+ 1, TX
);
148 tty_print_string (_("an option, ARROW KEYS"));
149 dlg_move (h
, TY
+ 2, TX
);
150 tty_print_string (_("to move between options"));
151 dlg_move (h
, TY
+ 3, TX
);
152 tty_print_string (_("and T or INS to mark"));
154 tty_setcolor (COLOR_HOT_NORMAL
);
156 dlg_move (h
, PY
, PX
+ 1);
157 tty_print_string (_(" Permission "));
158 dlg_move (h
, FY
, FX
+ 1);
159 tty_print_string (_(" File "));
163 chmod_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
165 char buffer
[BUF_TINY
];
166 int id
= h
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
171 c_stat
^= check_perm
[id
].mode
;
172 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
173 label_set_text (statl
, buffer
);
174 chmod_toggle_select (h
, id
);
180 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0) {
181 chmod_toggle_select (h
, id
);
186 return MSG_NOT_HANDLED
;
193 return default_dlg_callback (h
, msg
, parm
);
204 end_chmod
= c_file
= need_update
= 0;
205 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
208 create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
209 chmod_callback
, "[Chmod]", _("Chmod command"),
210 DLG_CENTER
| DLG_REVERSE
);
212 for (i
= 0; i
< BUTTONS
; i
++) {
213 if (i
== 2 && single_set
)
217 button_new (BY
+ chmod_but
[i
].y
- single_set
,
219 chmod_but
[i
].ret_cmd
,
221 _(chmod_but
[i
].text
), 0));
224 for (i
= 0; i
< PERMISSIONS
; i
++) {
225 check_perm
[i
].check
=
226 check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0,
227 _(check_perm
[i
].text
));
228 add_widget (ch_dlg
, check_perm
[i
].check
);
234 static void chmod_done (void)
237 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
241 static char *next_file (void)
243 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
246 return current_panel
->dir
.list
[c_file
].fname
;
249 static void do_chmod (struct stat
*sf
)
251 sf
->st_mode
&= and_mask
;
252 sf
->st_mode
|= or_mask
;
253 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
254 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
255 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
257 do_file_mark (current_panel
, c_file
, 0);
260 static void apply_mask (struct stat
*sf
)
264 need_update
= end_chmod
= 1;
268 fname
= next_file ();
269 if (mc_stat (fname
, sf
) != 0)
271 c_stat
= sf
->st_mode
;
274 } while (current_panel
->marked
);
277 void chmod_cmd (void)
279 char buffer
[BUF_TINY
];
285 do { /* do while any files remaining */
286 ch_dlg
= init_chmod ();
287 if (current_panel
->marked
)
288 fname
= next_file (); /* next marked file */
290 fname
= selection (current_panel
)->fname
; /* single file */
292 if (mc_stat (fname
, &sf_stat
) != 0) { /* get status of file */
293 destroy_dlg (ch_dlg
);
297 c_stat
= sf_stat
.st_mode
;
298 mode_change
= 0; /* clear changes flag */
300 /* set check buttons */
301 for (i
= 0; i
< PERMISSIONS
; i
++){
302 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
303 check_perm
[i
].selected
= 0;
307 c_fname
= str_trunc (fname
, 21);
308 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
));
309 c_fown
= str_trunc (get_owner (sf_stat
.st_uid
), 21);
310 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
));
311 c_fgrp
= str_trunc (get_group (sf_stat
.st_gid
), 21);
312 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
));
313 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
314 statl
= label_new (FY
+4, FX
+2, buffer
);
315 add_widget (ch_dlg
, statl
);
317 run_dlg (ch_dlg
); /* retrieve an action */
320 switch (ch_dlg
->ret_value
){
323 if (mc_chmod (fname
, c_stat
) == -1)
324 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
325 fname
, unix_error_string (errno
));
335 and_mask
= or_mask
= 0;
336 and_mask
= ~and_mask
;
338 for (i
= 0; i
< PERMISSIONS
; i
++) {
339 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
) {
340 if (check_perm
[i
].check
->state
& C_BOOL
)
341 or_mask
|= check_perm
[i
].mode
;
343 and_mask
&= ~check_perm
[i
].mode
;
347 apply_mask (&sf_stat
);
351 and_mask
= or_mask
= 0;
352 and_mask
= ~and_mask
;
354 for (i
= 0; i
< PERMISSIONS
; i
++) {
355 if (check_perm
[i
].selected
)
356 or_mask
|= check_perm
[i
].mode
;
359 apply_mask (&sf_stat
);
362 and_mask
= or_mask
= 0;
363 and_mask
= ~and_mask
;
365 for (i
= 0; i
< PERMISSIONS
; i
++) {
366 if (check_perm
[i
].selected
)
367 and_mask
&= ~check_perm
[i
].mode
;
370 apply_mask (&sf_stat
);
374 if (current_panel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
375 do_file_mark (current_panel
, c_file
, 0);
378 destroy_dlg (ch_dlg
);
379 } while (current_panel
->marked
&& !end_chmod
);