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>
34 #include "lib/global.h"
36 #include "lib/tty/tty.h"
38 #include "lib/vfs/mc-vfs/vfs.h"
39 #include "lib/strutil.h"
41 #include "dialog.h" /* add_widget() */
42 #include "widget.h" /* NORMAL_BUTTON */
43 #include "wtools.h" /* message() */
44 #include "panel.h" /* do_file_mark() */
45 #include "main.h" /* update_panels() */
46 #include "layout.h" /* repaint_screen() */
49 static int single_set
;
63 #define PERMISSIONS 12
66 #define B_MARKED B_USER
67 #define B_ALL (B_USER+1)
68 #define B_SETMRK (B_USER+2)
69 #define B_CLRMRK (B_USER+3)
71 static int mode_change
, need_update
;
72 static int c_file
, end_chmod
;
74 static mode_t and_mask
, or_mask
, c_stat
;
76 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
77 * returns a pointer to a static buffer, and label_new creates its own copy
79 * --rillig, 2004-08-29 */
80 static const char *c_fname
, *c_fown
, *c_fgrp
;
89 } check_perm
[PERMISSIONS
] =
91 { S_IXOTH
, N_("execute/search by others"), 0, 0, },
92 { S_IWOTH
, N_("write by others"), 0, 0, },
93 { S_IROTH
, N_("read by others"), 0, 0, },
94 { S_IXGRP
, N_("execute/search by group"), 0, 0, },
95 { S_IWGRP
, N_("write by group"), 0, 0, },
96 { S_IRGRP
, N_("read by group"), 0, 0, },
97 { S_IXUSR
, N_("execute/search by owner"), 0, 0, },
98 { S_IWUSR
, N_("write by owner"), 0, 0, },
99 { S_IRUSR
, N_("read by owner"), 0, 0, },
100 { S_ISVTX
, N_("sticky bit"), 0, 0, },
101 { S_ISGID
, N_("set group ID on execution"), 0, 0, },
102 { S_ISUID
, N_("set user ID on execution"), 0, 0, },
106 int ret_cmd
, flags
, y
, x
;
108 } chmod_but
[BUTTONS
] =
110 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
111 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
112 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
113 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
114 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
115 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
118 static void chmod_toggle_select (Dlg_head
*h
, int Id
)
120 tty_setcolor (COLOR_NORMAL
);
121 check_perm
[Id
].selected
^= 1;
123 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
124 tty_print_char ((check_perm
[Id
].selected
) ? '*' : ' ');
125 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
128 static void chmod_refresh (Dlg_head
*h
)
130 common_dialog_repaint (h
);
132 tty_setcolor (COLOR_NORMAL
);
134 draw_box (h
, PY
, PX
, PERMISSIONS
+ 2, 33, TRUE
);
135 draw_box (h
, FY
, FX
, 10, 25, TRUE
);
137 dlg_move (h
, FY
+ 1, FX
+ 2);
138 tty_print_string (_("Name"));
139 dlg_move (h
, FY
+ 3, FX
+ 2);
140 tty_print_string (_("Permissions (Octal)"));
141 dlg_move (h
, FY
+ 5, FX
+ 2);
142 tty_print_string (_("Owner name"));
143 dlg_move (h
, FY
+ 7, FX
+ 2);
144 tty_print_string (_("Group name"));
146 dlg_move (h
, TY
, TX
);
147 tty_print_string (_("Use SPACE to change"));
148 dlg_move (h
, TY
+ 1, TX
);
149 tty_print_string (_("an option, ARROW KEYS"));
150 dlg_move (h
, TY
+ 2, TX
);
151 tty_print_string (_("to move between options"));
152 dlg_move (h
, TY
+ 3, TX
);
153 tty_print_string (_("and T or INS to mark"));
155 tty_setcolor (COLOR_HOT_NORMAL
);
157 dlg_move (h
, PY
, PX
+ 1);
158 tty_print_string (_(" Permission "));
159 dlg_move (h
, FY
, FX
+ 1);
160 tty_print_string (_(" File "));
164 chmod_callback (Dlg_head
*h
, Widget
*sender
,
165 dlg_msg_t msg
, int parm
, void *data
)
167 char buffer
[BUF_TINY
];
168 int id
= h
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
173 c_stat
^= check_perm
[id
].mode
;
174 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
175 label_set_text (statl
, buffer
);
176 chmod_toggle_select (h
, id
);
182 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0) {
183 chmod_toggle_select (h
, id
);
188 return MSG_NOT_HANDLED
;
195 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
206 end_chmod
= c_file
= need_update
= 0;
207 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
210 create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
211 chmod_callback
, "[Chmod]", _("Chmod command"),
212 DLG_CENTER
| DLG_REVERSE
);
214 for (i
= 0; i
< BUTTONS
; i
++) {
215 if (i
== 2 && single_set
)
219 button_new (BY
+ chmod_but
[i
].y
- single_set
,
221 chmod_but
[i
].ret_cmd
,
223 _(chmod_but
[i
].text
), 0));
226 for (i
= 0; i
< PERMISSIONS
; i
++) {
227 check_perm
[i
].check
=
228 check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0,
229 _(check_perm
[i
].text
));
230 add_widget (ch_dlg
, check_perm
[i
].check
);
236 static void chmod_done (void)
239 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
243 static char *next_file (void)
245 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
248 return current_panel
->dir
.list
[c_file
].fname
;
251 static void do_chmod (struct stat
*sf
)
253 sf
->st_mode
&= and_mask
;
254 sf
->st_mode
|= or_mask
;
255 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
256 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
257 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
259 do_file_mark (current_panel
, c_file
, 0);
262 static void apply_mask (struct stat
*sf
)
266 need_update
= end_chmod
= 1;
270 fname
= next_file ();
271 if (mc_stat (fname
, sf
) != 0)
273 c_stat
= sf
->st_mode
;
276 } while (current_panel
->marked
);
279 void chmod_cmd (void)
281 char buffer
[BUF_TINY
];
287 do { /* do while any files remaining */
288 ch_dlg
= init_chmod ();
289 if (current_panel
->marked
)
290 fname
= next_file (); /* next marked file */
292 fname
= selection (current_panel
)->fname
; /* single file */
294 if (mc_stat (fname
, &sf_stat
) != 0) { /* get status of file */
295 destroy_dlg (ch_dlg
);
299 c_stat
= sf_stat
.st_mode
;
300 mode_change
= 0; /* clear changes flag */
302 /* set check buttons */
303 for (i
= 0; i
< PERMISSIONS
; i
++){
304 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
305 check_perm
[i
].selected
= 0;
309 c_fname
= str_trunc (fname
, 21);
310 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
));
311 c_fown
= str_trunc (get_owner (sf_stat
.st_uid
), 21);
312 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
));
313 c_fgrp
= str_trunc (get_group (sf_stat
.st_gid
), 21);
314 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
));
315 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
316 statl
= label_new (FY
+4, FX
+2, buffer
);
317 add_widget (ch_dlg
, statl
);
319 run_dlg (ch_dlg
); /* retrieve an action */
322 switch (ch_dlg
->ret_value
){
325 if (mc_chmod (fname
, c_stat
) == -1)
326 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
327 fname
, unix_error_string (errno
));
337 and_mask
= or_mask
= 0;
338 and_mask
= ~and_mask
;
340 for (i
= 0; i
< PERMISSIONS
; i
++) {
341 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
) {
342 if (check_perm
[i
].check
->state
& C_BOOL
)
343 or_mask
|= check_perm
[i
].mode
;
345 and_mask
&= ~check_perm
[i
].mode
;
349 apply_mask (&sf_stat
);
353 and_mask
= or_mask
= 0;
354 and_mask
= ~and_mask
;
356 for (i
= 0; i
< PERMISSIONS
; i
++) {
357 if (check_perm
[i
].selected
)
358 or_mask
|= check_perm
[i
].mode
;
361 apply_mask (&sf_stat
);
364 and_mask
= or_mask
= 0;
365 and_mask
= ~and_mask
;
367 for (i
= 0; i
< PERMISSIONS
; i
++) {
368 if (check_perm
[i
].selected
)
369 and_mask
&= ~check_perm
[i
].mode
;
372 apply_mask (&sf_stat
);
376 if (current_panel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
377 do_file_mark (current_panel
, c_file
, 0);
380 destroy_dlg (ch_dlg
);
381 } while (current_panel
->marked
&& !end_chmod
);