2 Chmod command -- for the Midnight Commander
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * \brief Source: chmod command
31 #include <sys/types.h>
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
39 #include "lib/vfs/vfs.h"
40 #include "lib/strutil.h"
42 #include "lib/widget.h"
43 #include "lib/keybind.h" /* CK_Cancel */
45 #include "midnight.h" /* current_panel */
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
55 #define B_MARKED B_USER
56 #define B_ALL (B_USER + 1)
57 #define B_SETMRK (B_USER + 2)
58 #define B_CLRMRK (B_USER + 3)
60 /*** file scope type declarations ****************************************************************/
62 /*** file scope variables ************************************************************************/
64 static gboolean single_set
;
66 static gboolean mode_change
, need_update
, end_chmod
;
69 static mode_t and_mask
, or_mask
, c_stat
;
72 static WGroupbox
*file_gb
;
83 { S_ISUID
, N_("set &user ID on execution"), FALSE
, NULL
},
84 { S_ISGID
, N_("set &group ID on execution"), FALSE
, NULL
},
85 { S_ISVTX
, N_("stick&y bit"), FALSE
, NULL
},
86 { S_IRUSR
, N_("&read by owner"), FALSE
, NULL
},
87 { S_IWUSR
, N_("&write by owner"), FALSE
, NULL
},
88 { S_IXUSR
, N_("e&xecute/search by owner"), FALSE
, NULL
},
89 { S_IRGRP
, N_("rea&d by group"), FALSE
, NULL
},
90 { S_IWGRP
, N_("write by grou&p"), FALSE
, NULL
},
91 { S_IXGRP
, N_("execu&te/search by group"), FALSE
, NULL
},
92 { S_IROTH
, N_("read &by others"), FALSE
, NULL
},
93 { S_IWOTH
, N_("wr&ite by others"), FALSE
, NULL
},
94 { S_IXOTH
, N_("execute/searc&h by others"), FALSE
, NULL
}
98 static const unsigned int check_perm_num
= G_N_ELEMENTS (check_perm
);
99 static int check_perm_len
= 0;
101 static const char *file_info_labels
[] = {
103 N_("Permissions (octal):"),
108 static const unsigned int file_info_labels_num
= G_N_ELEMENTS (file_info_labels
);
109 static int file_info_labels_len
= 0;
115 int y
; /* vertical position relatively to dialog bottom boundary */
121 { B_ALL
, NORMAL_BUTTON
, 6, 0, N_("Set &all") },
122 { B_MARKED
, NORMAL_BUTTON
, 6, 0, N_("&Marked all") },
123 { B_SETMRK
, NORMAL_BUTTON
, 5, 0, N_("S&et marked") },
124 { B_CLRMRK
, NORMAL_BUTTON
, 5, 0, N_("C&lear marked") },
125 { B_ENTER
, DEFPUSH_BUTTON
, 3, 0, N_("&Set") },
126 { B_CANCEL
, NORMAL_BUTTON
, 3, 0, N_("&Cancel") }
130 static const unsigned int chmod_but_num
= G_N_ELEMENTS (chmod_but
);
132 /* --------------------------------------------------------------------------------------------- */
133 /*** file scope functions ************************************************************************/
134 /* --------------------------------------------------------------------------------------------- */
139 static gboolean i18n
= FALSE
;
149 for (i
= 0; i
< check_perm_num
; i
++)
150 check_perm
[i
].text
= _(check_perm
[i
].text
);
152 for (i
= 0; i
< file_info_labels_num
; i
++)
153 file_info_labels
[i
] = _(file_info_labels
[i
]);
155 for (i
= 0; i
< chmod_but_num
; i
++)
156 chmod_but
[i
].text
= _(chmod_but
[i
].text
);
157 #endif /* ENABLE_NLS */
159 for (i
= 0; i
< check_perm_num
; i
++)
161 len
= str_term_width1 (check_perm
[i
].text
);
162 check_perm_len
= max (check_perm_len
, len
);
165 check_perm_len
+= 1 + 3 + 1; /* mark, [x] and space */
167 for (i
= 0; i
< file_info_labels_num
; i
++)
169 len
= str_term_width1 (file_info_labels
[i
]) + 2; /* spaces around */
170 file_info_labels_len
= max (file_info_labels_len
, len
);
173 for (i
= 0; i
< chmod_but_num
; i
++)
175 chmod_but
[i
].len
= str_term_width1 (chmod_but
[i
].text
) + 3; /* [], spaces and w/o & */
176 if (chmod_but
[i
].flags
== DEFPUSH_BUTTON
)
177 chmod_but
[i
].len
+= 2; /* <> */
181 /* --------------------------------------------------------------------------------------------- */
184 chmod_toggle_select (WDialog
* h
, int Id
)
186 tty_setcolor (COLOR_NORMAL
);
187 check_perm
[Id
].selected
= !check_perm
[Id
].selected
;
189 widget_move (h
, PY
+ Id
+ 1, PX
+ 1);
190 tty_print_char (check_perm
[Id
].selected
? '*' : ' ');
191 widget_move (h
, PY
+ Id
+ 1, PX
+ 3);
194 /* --------------------------------------------------------------------------------------------- */
197 chmod_refresh (WDialog
* h
)
199 int y
= WIDGET (file_gb
)->y
+ 1;
200 int x
= WIDGET (file_gb
)->x
+ 2;
202 dlg_default_repaint (h
);
204 tty_setcolor (COLOR_NORMAL
);
207 tty_print_string (file_info_labels
[0]);
208 tty_gotoyx (y
+ 2, x
);
209 tty_print_string (file_info_labels
[1]);
210 tty_gotoyx (y
+ 4, x
);
211 tty_print_string (file_info_labels
[2]);
212 tty_gotoyx (y
+ 6, x
);
213 tty_print_string (file_info_labels
[3]);
216 /* --------------------------------------------------------------------------------------------- */
219 chmod_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
221 WDialog
*h
= DIALOG (w
);
227 /* handle checkboxes */
230 /* close dialog due to SIGINT (ctrl-g) */
231 if (sender
== NULL
&& parm
== CK_Cancel
)
232 return MSG_NOT_HANDLED
;
234 /* whether action was sent by checkbox? */
235 for (i
= 0; i
< check_perm_num
; i
++)
236 if (sender
== WIDGET (check_perm
[i
].check
))
239 if (i
< check_perm_num
)
241 char buffer
[BUF_TINY
];
243 c_stat
^= check_perm
[i
].mode
;
244 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
245 label_set_text (statl
, buffer
);
246 chmod_toggle_select (h
, i
);
252 return MSG_NOT_HANDLED
;
255 if (parm
== 'T' || parm
== 't' || parm
== KEY_IC
)
260 id
= dlg_get_current_widget_id (h
);
261 for (i
= 0; i
< check_perm_num
; i
++)
262 if (id
== WIDGET (check_perm
[i
].check
)->id
)
265 if (i
< check_perm_num
)
267 chmod_toggle_select (h
, i
);
273 return MSG_NOT_HANDLED
;
280 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
284 /* --------------------------------------------------------------------------------------------- */
287 init_chmod (const char *fname
, const struct stat
*sf_stat
)
295 const char *c_fname
, *c_fown
, *c_fgrp
;
296 char buffer
[BUF_TINY
];
298 single_set
= (current_panel
->marked
< 2);
299 perm_gb_len
= check_perm_len
+ 2;
300 file_gb_len
= file_info_labels_len
+ 2;
301 cols
= str_term_width1 (fname
) + 2 + 1;
302 file_gb_len
= max (file_gb_len
, cols
);
304 lines
= single_set
? 20 : 23;
305 cols
= perm_gb_len
+ file_gb_len
+ 1 + 6;
309 /* shrink the right groupbox */
311 file_gb_len
= cols
- (perm_gb_len
+ 1 + 6);
315 create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
,
316 chmod_callback
, NULL
, "[Chmod]", _("Chmod command"), DLG_CENTER
);
318 add_widget (ch_dlg
, groupbox_new (PY
, PX
, check_perm_num
+ 2, perm_gb_len
, _("Permission")));
320 for (i
= 0; i
< check_perm_num
; i
++)
322 check_perm
[i
].check
= check_new (PY
+ i
+ 1, PX
+ 2,
323 (c_stat
& check_perm
[i
].mode
) != 0 ? 1 : 0,
325 add_widget (ch_dlg
, check_perm
[i
].check
);
328 file_gb
= groupbox_new (PY
, PX
+ perm_gb_len
+ 1, check_perm_num
+ 2, file_gb_len
, _("File"));
329 add_widget (ch_dlg
, file_gb
);
333 cols
= PX
+ perm_gb_len
+ 3;
334 c_fname
= str_trunc (fname
, file_gb_len
- 3);
335 add_widget (ch_dlg
, label_new (y
, cols
, c_fname
));
336 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
337 statl
= label_new (y
+ 2, cols
, buffer
);
338 add_widget (ch_dlg
, statl
);
339 c_fown
= str_trunc (get_owner (sf_stat
->st_uid
), file_gb_len
- 3);
340 add_widget (ch_dlg
, label_new (y
+ 4, cols
, c_fown
));
341 c_fgrp
= str_trunc (get_group (sf_stat
->st_gid
), file_gb_len
- 3);
342 add_widget (ch_dlg
, label_new (y
+ 6, cols
, c_fgrp
));
347 add_widget (ch_dlg
, hline_new (lines
- chmod_but
[i
].y
- 1, -1, -1));
348 for (; i
< chmod_but_num
- 2; i
++)
350 y
= lines
- chmod_but
[i
].y
;
352 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 - chmod_but
[i
].len
,
353 chmod_but
[i
].ret_cmd
, chmod_but
[i
].flags
, chmod_but
[i
].text
,
357 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 + 1,
358 chmod_but
[i
].ret_cmd
, chmod_but
[i
].flags
, chmod_but
[i
].text
,
363 i
= chmod_but_num
- 2;
364 y
= lines
- chmod_but
[i
].y
;
365 add_widget (ch_dlg
, hline_new (y
- 1, -1, -1));
367 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 - chmod_but
[i
].len
, chmod_but
[i
].ret_cmd
,
368 chmod_but
[i
].flags
, chmod_but
[i
].text
, NULL
));
371 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 + 1, chmod_but
[i
].ret_cmd
,
372 chmod_but
[i
].flags
, chmod_but
[i
].text
, NULL
));
374 /* select first checkbox */
375 dlg_select_widget (check_perm
[0].check
);
380 /* --------------------------------------------------------------------------------------------- */
386 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
390 /* --------------------------------------------------------------------------------------------- */
395 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
398 return current_panel
->dir
.list
[c_file
].fname
;
401 /* --------------------------------------------------------------------------------------------- */
404 do_chmod (struct stat
*sf
)
407 sf
->st_mode
&= and_mask
;
408 sf
->st_mode
|= or_mask
;
410 vpath
= vfs_path_from_str (current_panel
->dir
.list
[c_file
].fname
);
411 if (mc_chmod (vpath
, sf
->st_mode
) == -1)
412 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
413 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
415 vfs_path_free (vpath
);
416 do_file_mark (current_panel
, c_file
, 0);
419 /* --------------------------------------------------------------------------------------------- */
422 apply_mask (struct stat
*sf
)
435 fname
= next_file ();
436 vpath
= vfs_path_from_str (fname
);
437 ok
= (mc_stat (vpath
, sf
) == 0);
438 vfs_path_free (vpath
);
442 c_stat
= sf
->st_mode
;
446 while (current_panel
->marked
!= 0);
449 /* --------------------------------------------------------------------------------------------- */
450 /*** public functions ****************************************************************************/
451 /* --------------------------------------------------------------------------------------------- */
459 { /* do while any files remaining */
474 if (current_panel
->marked
!= 0)
475 fname
= next_file (); /* next marked file */
477 fname
= selection (current_panel
)->fname
; /* single file */
479 vpath
= vfs_path_from_str (fname
);
481 if (mc_stat (vpath
, &sf_stat
) != 0)
483 vfs_path_free (vpath
);
487 c_stat
= sf_stat
.st_mode
;
489 ch_dlg
= init_chmod (fname
, &sf_stat
);
492 result
= run_dlg (ch_dlg
);
497 if (mode_change
&& mc_chmod (vpath
, c_stat
) == -1)
498 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
499 fname
, unix_error_string (errno
));
509 and_mask
= or_mask
= 0;
510 and_mask
= ~and_mask
;
512 for (i
= 0; i
< check_perm_num
; i
++)
513 if (check_perm
[i
].selected
|| result
== B_ALL
)
515 if (check_perm
[i
].check
->state
& C_BOOL
)
516 or_mask
|= check_perm
[i
].mode
;
518 and_mask
&= ~check_perm
[i
].mode
;
521 apply_mask (&sf_stat
);
525 and_mask
= or_mask
= 0;
526 and_mask
= ~and_mask
;
528 for (i
= 0; i
< check_perm_num
; i
++)
529 if (check_perm
[i
].selected
)
530 or_mask
|= check_perm
[i
].mode
;
532 apply_mask (&sf_stat
);
536 and_mask
= or_mask
= 0;
537 and_mask
= ~and_mask
;
539 for (i
= 0; i
< check_perm_num
; i
++)
540 if (check_perm
[i
].selected
)
541 and_mask
&= ~check_perm
[i
].mode
;
543 apply_mask (&sf_stat
);
547 if (current_panel
->marked
!= 0 && result
!= B_CANCEL
)
549 do_file_mark (current_panel
, c_file
, 0);
553 vfs_path_free (vpath
);
555 destroy_dlg (ch_dlg
);
557 while (current_panel
->marked
!= 0 && !end_chmod
);
562 /* --------------------------------------------------------------------------------------------- */