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_IXOTH
, N_("execute/search by others"), FALSE
, NULL
},
84 { S_IWOTH
, N_("write by others"), FALSE
, NULL
},
85 { S_IROTH
, N_("read by others"), FALSE
, NULL
},
86 { S_IXGRP
, N_("execute/search by group"), FALSE
, NULL
},
87 { S_IWGRP
, N_("write by group"), FALSE
, NULL
},
88 { S_IRGRP
, N_("read by group"), FALSE
, NULL
},
89 { S_IXUSR
, N_("execute/search by owner"), FALSE
, NULL
},
90 { S_IWUSR
, N_("write by owner"), FALSE
, NULL
},
91 { S_IRUSR
, N_("read by owner"), FALSE
, NULL
},
92 { S_ISVTX
, N_("sticky bit"), FALSE
, NULL
},
93 { S_ISGID
, N_("set group ID on execution"), FALSE
, NULL
},
94 { S_ISUID
, N_("set user ID on execution"), 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_CANCEL
, NORMAL_BUTTON
, 3, 0, N_("&Cancel") },
122 { B_ENTER
, DEFPUSH_BUTTON
, 3, 0, N_("&Set") },
123 { B_CLRMRK
, NORMAL_BUTTON
, 5, 0, N_("C&lear marked") },
124 { B_SETMRK
, NORMAL_BUTTON
, 5, 0, N_("S&et marked") },
125 { B_MARKED
, NORMAL_BUTTON
, 6, 0, N_("&Marked all") },
126 { B_ALL
, NORMAL_BUTTON
, 6, 0, N_("Set &all") }
130 static const unsigned int chmod_but_num
= G_N_ELEMENTS (chmod_but
);
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 (Dlg_head
* h
, int Id
)
185 tty_setcolor (COLOR_NORMAL
);
186 check_perm
[Id
].selected
= !check_perm
[Id
].selected
;
188 dlg_move (h
, PY
+ check_perm_num
- Id
, PX
+ 1);
189 tty_print_char (check_perm
[Id
].selected
? '*' : ' ');
190 dlg_move (h
, PY
+ check_perm_num
- Id
, PX
+ 3);
193 /* --------------------------------------------------------------------------------------------- */
196 chmod_refresh (Dlg_head
* h
)
198 int y
= file_gb
->widget
.y
+ 1;
199 int x
= file_gb
->widget
.x
+ 2;
201 common_dialog_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 (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
220 char buffer
[BUF_TINY
];
223 id
= dlg_get_current_widget_id (h
) - (chmod_but_num
- (single_set
? 4 : 0)) - 1;
228 /* close dialog due to SIGINT (ctrl-g) */
229 if (sender
== NULL
&& parm
== CK_Cancel
)
230 return MSG_NOT_HANDLED
;
232 /* handle checkboxes */
235 c_stat
^= check_perm
[id
].mode
;
236 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
237 label_set_text (statl
, buffer
);
238 chmod_toggle_select (h
, id
);
243 return MSG_NOT_HANDLED
;
246 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0)
248 chmod_toggle_select (h
, id
);
253 return MSG_NOT_HANDLED
;
260 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
264 /* --------------------------------------------------------------------------------------------- */
267 init_chmod (const char *fname
, const struct stat
*sf_stat
)
274 const char *c_fname
, *c_fown
, *c_fgrp
;
275 char buffer
[BUF_TINY
];
277 single_set
= (current_panel
->marked
< 2);
278 perm_gb_len
= check_perm_len
+ 2;
279 file_gb_len
= file_info_labels_len
+ 2;
280 cols
= str_term_width1 (fname
) + 2 + 1;
281 file_gb_len
= max (file_gb_len
, cols
);
283 lines
= single_set
? 20 : 23;
284 cols
= perm_gb_len
+ file_gb_len
+ 1 + 6;
288 /* shrink the right groupbox */
290 file_gb_len
= cols
- (perm_gb_len
+ 1 + 6);
294 create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
,
295 chmod_callback
, "[Chmod]", _("Chmod command"), DLG_CENTER
| DLG_REVERSE
);
297 for (i
= 0; i
< chmod_but_num
; i
++)
300 button_new (lines
- chmod_but
[i
].y
, ch_dlg
->cols
/ 2 + 1,
301 chmod_but
[i
].ret_cmd
, chmod_but
[i
].flags
, chmod_but
[i
].text
, 0));
306 button_new (lines
- chmod_but
[i
].y
, ch_dlg
->cols
/ 2 - chmod_but
[i
].len
,
307 chmod_but
[i
].ret_cmd
, chmod_but
[i
].flags
, chmod_but
[i
].text
, 0));
313 file_gb
= groupbox_new (PY
, PX
+ perm_gb_len
+ 1, check_perm_num
+ 2, file_gb_len
, _("File"));
314 add_widget (ch_dlg
, file_gb
);
316 for (i
= 0; i
< check_perm_num
; i
++)
318 check_perm
[i
].check
= check_new (PY
+ (check_perm_num
- i
), PX
+ 2,
319 (c_stat
& check_perm
[i
].mode
) != 0 ? 1 : 0,
321 add_widget (ch_dlg
, check_perm
[i
].check
);
324 add_widget (ch_dlg
, groupbox_new (PY
, PX
, check_perm_num
+ 2, perm_gb_len
, _("Permission")));
327 /* Do this at end to have a widget id in a simple way */
329 cols
= PX
+ perm_gb_len
+ 3;
330 c_fname
= str_trunc (fname
, file_gb_len
- 3);
331 add_widget (ch_dlg
, label_new (lines
, cols
, c_fname
));
332 c_fown
= str_trunc (get_owner (sf_stat
->st_uid
), file_gb_len
- 3);
333 add_widget (ch_dlg
, label_new (lines
+ 4, cols
, c_fown
));
334 c_fgrp
= str_trunc (get_group (sf_stat
->st_gid
), file_gb_len
- 3);
335 add_widget (ch_dlg
, label_new (lines
+ 6, cols
, c_fgrp
));
336 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
337 statl
= label_new (lines
+ 2, cols
, buffer
);
338 add_widget (ch_dlg
, statl
);
343 /* --------------------------------------------------------------------------------------------- */
349 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
353 /* --------------------------------------------------------------------------------------------- */
358 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
361 return current_panel
->dir
.list
[c_file
].fname
;
364 /* --------------------------------------------------------------------------------------------- */
367 do_chmod (struct stat
*sf
)
370 sf
->st_mode
&= and_mask
;
371 sf
->st_mode
|= or_mask
;
373 vpath
= vfs_path_from_str (current_panel
->dir
.list
[c_file
].fname
);
374 if (mc_chmod (vpath
, sf
->st_mode
) == -1)
375 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
376 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
378 vfs_path_free (vpath
);
379 do_file_mark (current_panel
, c_file
, 0);
382 /* --------------------------------------------------------------------------------------------- */
385 apply_mask (struct stat
*sf
)
398 fname
= next_file ();
399 vpath
= vfs_path_from_str (fname
);
400 ok
= (mc_stat (vpath
, sf
) == 0);
401 vfs_path_free (vpath
);
405 c_stat
= sf
->st_mode
;
409 while (current_panel
->marked
!= 0);
412 /* --------------------------------------------------------------------------------------------- */
413 /*** public functions ****************************************************************************/
414 /* --------------------------------------------------------------------------------------------- */
422 { /* do while any files remaining */
437 if (current_panel
->marked
!= 0)
438 fname
= next_file (); /* next marked file */
440 fname
= selection (current_panel
)->fname
; /* single file */
442 vpath
= vfs_path_from_str (fname
);
444 if (mc_stat (vpath
, &sf_stat
) != 0)
446 vfs_path_free (vpath
);
450 c_stat
= sf_stat
.st_mode
;
452 ch_dlg
= init_chmod (fname
, &sf_stat
);
455 result
= run_dlg (ch_dlg
);
460 if (mode_change
&& mc_chmod (vpath
, c_stat
) == -1)
461 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
462 fname
, unix_error_string (errno
));
472 and_mask
= or_mask
= 0;
473 and_mask
= ~and_mask
;
475 for (i
= 0; i
< check_perm_num
; i
++)
476 if (check_perm
[i
].selected
|| result
== B_ALL
)
478 if (check_perm
[i
].check
->state
& C_BOOL
)
479 or_mask
|= check_perm
[i
].mode
;
481 and_mask
&= ~check_perm
[i
].mode
;
484 apply_mask (&sf_stat
);
488 and_mask
= or_mask
= 0;
489 and_mask
= ~and_mask
;
491 for (i
= 0; i
< check_perm_num
; i
++)
492 if (check_perm
[i
].selected
)
493 or_mask
|= check_perm
[i
].mode
;
495 apply_mask (&sf_stat
);
499 and_mask
= or_mask
= 0;
500 and_mask
= ~and_mask
;
502 for (i
= 0; i
< check_perm_num
; i
++)
503 if (check_perm
[i
].selected
)
504 and_mask
&= ~check_perm
[i
].mode
;
506 apply_mask (&sf_stat
);
510 if (current_panel
->marked
!= 0 && result
!= B_CANCEL
)
512 do_file_mark (current_panel
, c_file
, 0);
516 vfs_path_free (vpath
);
518 destroy_dlg (ch_dlg
);
520 while (current_panel
->marked
!= 0 && !end_chmod
);
525 /* --------------------------------------------------------------------------------------------- */