2 Chmod command -- for the Midnight Commander
4 Copyright (C) 1994-2015
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * \brief Source: chmod command
30 #include <sys/types.h>
34 #include "lib/global.h"
36 #include "lib/tty/tty.h"
38 #include "lib/vfs/vfs.h"
39 #include "lib/strutil.h"
41 #include "lib/widget.h"
42 #include "lib/keybind.h" /* CK_Cancel */
44 #include "midnight.h" /* current_panel */
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
54 #define B_MARKED B_USER
55 #define B_ALL (B_USER + 1)
56 #define B_SETMRK (B_USER + 2)
57 #define B_CLRMRK (B_USER + 3)
59 /*** file scope type declarations ****************************************************************/
61 /*** file scope variables ************************************************************************/
63 static gboolean single_set
;
65 static gboolean mode_change
, need_update
, end_chmod
;
68 static mode_t and_mask
, or_mask
, c_stat
;
71 static WGroupbox
*file_gb
;
82 { S_ISUID
, N_("set &user ID on execution"), FALSE
, NULL
},
83 { S_ISGID
, N_("set &group ID on execution"), FALSE
, NULL
},
84 { S_ISVTX
, N_("stick&y bit"), FALSE
, NULL
},
85 { S_IRUSR
, N_("&read by owner"), FALSE
, NULL
},
86 { S_IWUSR
, N_("&write by owner"), FALSE
, NULL
},
87 { S_IXUSR
, N_("e&xecute/search by owner"), FALSE
, NULL
},
88 { S_IRGRP
, N_("rea&d by group"), FALSE
, NULL
},
89 { S_IWGRP
, N_("write by grou&p"), FALSE
, NULL
},
90 { S_IXGRP
, N_("execu&te/search by group"), FALSE
, NULL
},
91 { S_IROTH
, N_("read &by others"), FALSE
, NULL
},
92 { S_IWOTH
, N_("wr&ite by others"), FALSE
, NULL
},
93 { S_IXOTH
, N_("execute/searc&h by others"), FALSE
, NULL
}
97 static const unsigned int check_perm_num
= G_N_ELEMENTS (check_perm
);
98 static int check_perm_len
= 0;
100 static const char *file_info_labels
[] = {
102 N_("Permissions (octal):"),
107 static const unsigned int file_info_labels_num
= G_N_ELEMENTS (file_info_labels
);
108 static int file_info_labels_len
= 0;
114 int y
; /* vertical position relatively to dialog bottom boundary */
120 { B_ALL
, NORMAL_BUTTON
, 6, 0, N_("Set &all") },
121 { B_MARKED
, NORMAL_BUTTON
, 6, 0, N_("&Marked all") },
122 { B_SETMRK
, NORMAL_BUTTON
, 5, 0, N_("S&et marked") },
123 { B_CLRMRK
, NORMAL_BUTTON
, 5, 0, N_("C&lear marked") },
124 { B_ENTER
, DEFPUSH_BUTTON
, 3, 0, N_("&Set") },
125 { B_CANCEL
, NORMAL_BUTTON
, 3, 0, N_("&Cancel") }
129 static const unsigned int chmod_but_num
= G_N_ELEMENTS (chmod_but
);
131 /* --------------------------------------------------------------------------------------------- */
132 /*** file scope functions ************************************************************************/
133 /* --------------------------------------------------------------------------------------------- */
138 static gboolean i18n
= FALSE
;
148 for (i
= 0; i
< check_perm_num
; i
++)
149 check_perm
[i
].text
= _(check_perm
[i
].text
);
151 for (i
= 0; i
< file_info_labels_num
; i
++)
152 file_info_labels
[i
] = _(file_info_labels
[i
]);
154 for (i
= 0; i
< chmod_but_num
; i
++)
155 chmod_but
[i
].text
= _(chmod_but
[i
].text
);
156 #endif /* ENABLE_NLS */
158 for (i
= 0; i
< check_perm_num
; i
++)
160 len
= str_term_width1 (check_perm
[i
].text
);
161 check_perm_len
= max (check_perm_len
, len
);
164 check_perm_len
+= 1 + 3 + 1; /* mark, [x] and space */
166 for (i
= 0; i
< file_info_labels_num
; i
++)
168 len
= str_term_width1 (file_info_labels
[i
]) + 2; /* spaces around */
169 file_info_labels_len
= max (file_info_labels_len
, len
);
172 for (i
= 0; i
< chmod_but_num
; i
++)
174 chmod_but
[i
].len
= str_term_width1 (chmod_but
[i
].text
) + 3; /* [], spaces and w/o & */
175 if (chmod_but
[i
].flags
== DEFPUSH_BUTTON
)
176 chmod_but
[i
].len
+= 2; /* <> */
180 /* --------------------------------------------------------------------------------------------- */
183 chmod_toggle_select (WDialog
* h
, int Id
)
185 tty_setcolor (COLOR_NORMAL
);
186 check_perm
[Id
].selected
= !check_perm
[Id
].selected
;
188 widget_move (h
, PY
+ Id
+ 1, PX
+ 1);
189 tty_print_char (check_perm
[Id
].selected
? '*' : ' ');
190 widget_move (h
, PY
+ Id
+ 1, PX
+ 3);
193 /* --------------------------------------------------------------------------------------------- */
196 chmod_refresh (WDialog
* h
)
198 int y
= WIDGET (file_gb
)->y
+ 1;
199 int x
= WIDGET (file_gb
)->x
+ 2;
201 dlg_default_repaint (h
);
203 tty_setcolor (COLOR_NORMAL
);
206 tty_print_string (file_info_labels
[0]);
207 tty_gotoyx (y
+ 2, x
);
208 tty_print_string (file_info_labels
[1]);
209 tty_gotoyx (y
+ 4, x
);
210 tty_print_string (file_info_labels
[2]);
211 tty_gotoyx (y
+ 6, x
);
212 tty_print_string (file_info_labels
[3]);
215 /* --------------------------------------------------------------------------------------------- */
218 chmod_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
220 WDialog
*h
= DIALOG (w
);
226 /* handle checkboxes */
229 /* close dialog due to SIGINT (ctrl-g) */
230 if (sender
== NULL
&& parm
== CK_Cancel
)
231 return MSG_NOT_HANDLED
;
233 /* whether action was sent by checkbox? */
234 for (i
= 0; i
< check_perm_num
; i
++)
235 if (sender
== WIDGET (check_perm
[i
].check
))
238 if (i
< check_perm_num
)
240 char buffer
[BUF_TINY
];
242 c_stat
^= check_perm
[i
].mode
;
243 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
244 label_set_text (statl
, buffer
);
245 chmod_toggle_select (h
, i
);
251 return MSG_NOT_HANDLED
;
254 if (parm
== 'T' || parm
== 't' || parm
== KEY_IC
)
259 id
= dlg_get_current_widget_id (h
);
260 for (i
= 0; i
< check_perm_num
; i
++)
261 if (id
== WIDGET (check_perm
[i
].check
)->id
)
264 if (i
< check_perm_num
)
266 chmod_toggle_select (h
, i
);
272 return MSG_NOT_HANDLED
;
279 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
283 /* --------------------------------------------------------------------------------------------- */
286 init_chmod (const char *fname
, const struct stat
*sf_stat
)
294 const char *c_fname
, *c_fown
, *c_fgrp
;
295 char buffer
[BUF_TINY
];
297 single_set
= (current_panel
->marked
< 2);
298 perm_gb_len
= check_perm_len
+ 2;
299 file_gb_len
= file_info_labels_len
+ 2;
300 cols
= str_term_width1 (fname
) + 2 + 1;
301 file_gb_len
= max (file_gb_len
, cols
);
303 lines
= single_set
? 20 : 23;
304 cols
= perm_gb_len
+ file_gb_len
+ 1 + 6;
308 /* shrink the right groupbox */
310 file_gb_len
= cols
- (perm_gb_len
+ 1 + 6);
314 dlg_create (TRUE
, 0, 0, lines
, cols
, dialog_colors
,
315 chmod_callback
, NULL
, "[Chmod]", _("Chmod command"), DLG_CENTER
);
317 add_widget (ch_dlg
, groupbox_new (PY
, PX
, check_perm_num
+ 2, perm_gb_len
, _("Permission")));
319 for (i
= 0; i
< check_perm_num
; i
++)
321 check_perm
[i
].check
= check_new (PY
+ i
+ 1, PX
+ 2,
322 (c_stat
& check_perm
[i
].mode
) != 0 ? 1 : 0,
324 add_widget (ch_dlg
, check_perm
[i
].check
);
327 file_gb
= groupbox_new (PY
, PX
+ perm_gb_len
+ 1, check_perm_num
+ 2, file_gb_len
, _("File"));
328 add_widget (ch_dlg
, file_gb
);
332 cols
= PX
+ perm_gb_len
+ 3;
333 c_fname
= str_trunc (fname
, file_gb_len
- 3);
334 add_widget (ch_dlg
, label_new (y
, cols
, c_fname
));
335 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
336 statl
= label_new (y
+ 2, cols
, buffer
);
337 add_widget (ch_dlg
, statl
);
338 c_fown
= str_trunc (get_owner (sf_stat
->st_uid
), file_gb_len
- 3);
339 add_widget (ch_dlg
, label_new (y
+ 4, cols
, c_fown
));
340 c_fgrp
= str_trunc (get_group (sf_stat
->st_gid
), file_gb_len
- 3);
341 add_widget (ch_dlg
, label_new (y
+ 6, cols
, c_fgrp
));
346 add_widget (ch_dlg
, hline_new (lines
- chmod_but
[i
].y
- 1, -1, -1));
347 for (; i
< chmod_but_num
- 2; i
++)
349 y
= lines
- chmod_but
[i
].y
;
351 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 - chmod_but
[i
].len
,
352 chmod_but
[i
].ret_cmd
, chmod_but
[i
].flags
, chmod_but
[i
].text
,
356 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 + 1,
357 chmod_but
[i
].ret_cmd
, chmod_but
[i
].flags
, chmod_but
[i
].text
,
362 i
= chmod_but_num
- 2;
363 y
= lines
- chmod_but
[i
].y
;
364 add_widget (ch_dlg
, hline_new (y
- 1, -1, -1));
366 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 - chmod_but
[i
].len
, chmod_but
[i
].ret_cmd
,
367 chmod_but
[i
].flags
, chmod_but
[i
].text
, NULL
));
370 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 + 1, chmod_but
[i
].ret_cmd
,
371 chmod_but
[i
].flags
, chmod_but
[i
].text
, NULL
));
373 /* select first checkbox */
374 dlg_select_widget (check_perm
[0].check
);
379 /* --------------------------------------------------------------------------------------------- */
385 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
389 /* --------------------------------------------------------------------------------------------- */
394 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
397 return current_panel
->dir
.list
[c_file
].fname
;
400 /* --------------------------------------------------------------------------------------------- */
403 do_chmod (struct stat
*sf
)
406 sf
->st_mode
&= and_mask
;
407 sf
->st_mode
|= or_mask
;
409 vpath
= vfs_path_from_str (current_panel
->dir
.list
[c_file
].fname
);
410 if (mc_chmod (vpath
, sf
->st_mode
) == -1)
411 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
412 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
414 vfs_path_free (vpath
);
415 do_file_mark (current_panel
, c_file
, 0);
418 /* --------------------------------------------------------------------------------------------- */
421 apply_mask (struct stat
*sf
)
434 fname
= next_file ();
435 vpath
= vfs_path_from_str (fname
);
436 ok
= (mc_stat (vpath
, sf
) == 0);
437 vfs_path_free (vpath
);
441 c_stat
= sf
->st_mode
;
445 while (current_panel
->marked
!= 0);
448 /* --------------------------------------------------------------------------------------------- */
449 /*** public functions ****************************************************************************/
450 /* --------------------------------------------------------------------------------------------- */
458 { /* do while any files remaining */
473 if (current_panel
->marked
!= 0)
474 fname
= next_file (); /* next marked file */
476 fname
= selection (current_panel
)->fname
; /* single file */
478 vpath
= vfs_path_from_str (fname
);
480 if (mc_stat (vpath
, &sf_stat
) != 0)
482 vfs_path_free (vpath
);
486 c_stat
= sf_stat
.st_mode
;
488 ch_dlg
= init_chmod (fname
, &sf_stat
);
491 result
= dlg_run (ch_dlg
);
496 if (mode_change
&& mc_chmod (vpath
, c_stat
) == -1)
497 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
498 fname
, unix_error_string (errno
));
508 and_mask
= or_mask
= 0;
509 and_mask
= ~and_mask
;
511 for (i
= 0; i
< check_perm_num
; i
++)
512 if (check_perm
[i
].selected
|| result
== B_ALL
)
514 if (check_perm
[i
].check
->state
& C_BOOL
)
515 or_mask
|= check_perm
[i
].mode
;
517 and_mask
&= ~check_perm
[i
].mode
;
520 apply_mask (&sf_stat
);
524 and_mask
= or_mask
= 0;
525 and_mask
= ~and_mask
;
527 for (i
= 0; i
< check_perm_num
; i
++)
528 if (check_perm
[i
].selected
)
529 or_mask
|= check_perm
[i
].mode
;
531 apply_mask (&sf_stat
);
535 and_mask
= or_mask
= 0;
536 and_mask
= ~and_mask
;
538 for (i
= 0; i
< check_perm_num
; i
++)
539 if (check_perm
[i
].selected
)
540 and_mask
&= ~check_perm
[i
].mode
;
542 apply_mask (&sf_stat
);
549 if (current_panel
->marked
!= 0 && result
!= B_CANCEL
)
551 do_file_mark (current_panel
, c_file
, 0);
555 vfs_path_free (vpath
);
557 dlg_destroy (ch_dlg
);
559 while (current_panel
->marked
!= 0 && !end_chmod
);
564 /* --------------------------------------------------------------------------------------------- */