1 /* Chown-advanced command -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 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: Contains functions for advanced chowning
30 #include <sys/types.h>
38 #include "../src/tty/tty.h"
39 #include "../src/tty/key.h" /* XCTRL and ALT macros */
40 #include "../src/skin/skin.h"
44 #include "wtools.h" /* For init_box_colors() */
48 #include "panel.h" /* Needed for the externs */
50 #include "main.h" /* update_panels() */
51 #include "layout.h" /* repaint_screen() */
62 #define B_SETALL B_USER
63 #define B_SKIP (B_USER + 1)
65 #define B_OWN (B_USER + 3)
66 #define B_GRP (B_USER + 4)
67 #define B_OTH (B_USER + 5)
68 #define B_OUSER (B_USER + 6)
69 #define B_OGROUP (B_USER + 7)
71 static struct Dlg_head
*ch_dlg
;
74 int ret_cmd
, flags
, y
, x
;
76 } chown_advanced_but
[BUTTONS
] = {
77 { B_CANCEL
, NORMAL_BUTTON
, 4, 53, N_("&Cancel") },
78 { B_ENTER
, DEFPUSH_BUTTON
,4, 40, N_("&Set") },
79 { B_SKIP
, NORMAL_BUTTON
, 4, 23, N_("S&kip") },
80 { B_SETALL
, NORMAL_BUTTON
, 4, 0, N_("Set &all")},
81 { B_ENTER
, NARROW_BUTTON
, 0, 47, ""},
82 { B_ENTER
, NARROW_BUTTON
, 0, 29, ""},
83 { B_ENTER
, NARROW_BUTTON
, 0, 19, " "},
84 { B_ENTER
, NARROW_BUTTON
, 0, 11, " "},
85 { B_ENTER
, NARROW_BUTTON
, 0, 3, " "}
88 static WButton
*b_att
[3]; /* permission */
89 static WButton
*b_user
, *b_group
; /* owner */
91 static int files_on_begin
; /* Number of files at startup */
94 static char ch_flags
[11];
95 static const char ch_perm
[] = "rwx";
96 static mode_t ch_cmode
;
97 static struct stat
*sf_stat
;
98 static int need_update
;
100 static int current_file
;
101 static int single_set
;
104 static void update_ownership (void)
106 button_set_text (b_user
, get_owner (sf_stat
->st_uid
));
107 button_set_text (b_group
, get_group (sf_stat
->st_gid
));
111 static cb_ret_t
inc_flag_pos (int f_pos
)
113 if (flag_pos
== 10) {
115 return MSG_NOT_HANDLED
;
118 if (!(flag_pos
% 3) || f_pos
> 2)
119 return MSG_NOT_HANDLED
;
123 static cb_ret_t
dec_flag_pos (int f_pos
)
127 return MSG_NOT_HANDLED
;
130 if (!((flag_pos
+ 1) % 3) || f_pos
> 2)
131 return MSG_NOT_HANDLED
;
135 static void set_perm_by_flags (char *s
, int f_p
)
139 for (i
= 0; i
< 3; i
++) {
140 if (ch_flags
[f_p
+ i
] == '+')
142 else if (ch_flags
[f_p
+ i
] == '-')
145 s
[i
] = (ch_cmode
& (1 << (8 - f_p
- i
))) ? ch_perm
[i
] : '-';
149 static void update_permissions (void)
151 set_perm_by_flags (b_att
[0]->text
.start
, 0);
152 set_perm_by_flags (b_att
[1]->text
.start
, 3);
153 set_perm_by_flags (b_att
[2]->text
.start
, 6);
156 static mode_t
get_perm (char *s
, int base
)
161 m
|= (s
[0] == '-') ? 0 :
162 ((s
[0] == '+') ? (mode_t
)(1 << (base
+ 2)) : (1 << (base
+ 2)) & ch_cmode
);
164 m
|= (s
[1] == '-') ? 0 :
165 ((s
[1] == '+') ? (mode_t
)(1 << (base
+ 1)) : (1 << (base
+ 1)) & ch_cmode
);
167 m
|= (s
[2] == '-') ? 0 :
168 ((s
[2] == '+') ? (mode_t
)(1 << base
) : (1 << base
) & ch_cmode
);
173 static mode_t
get_mode (void)
177 m
= ch_cmode
^ (ch_cmode
& 0777);
178 m
|= get_perm (ch_flags
, 6);
179 m
|= get_perm (ch_flags
+ 3, 3);
180 m
|= get_perm (ch_flags
+ 6, 0);
185 static void print_flags (void)
189 tty_setcolor (COLOR_NORMAL
);
191 for (i
= 0; i
< 3; i
++){
192 dlg_move (ch_dlg
, BY
+1, 9+i
);
193 tty_print_char (ch_flags
[i
]);
196 for (i
= 0; i
< 3; i
++){
197 dlg_move (ch_dlg
, BY
+ 1, 17 + i
);
198 tty_print_char (ch_flags
[i
+3]);
201 for (i
= 0; i
< 3; i
++){
202 dlg_move (ch_dlg
, BY
+ 1, 25 + i
);
203 tty_print_char (ch_flags
[i
+6]);
206 update_permissions ();
208 for (i
= 0; i
< 15; i
++){
209 dlg_move (ch_dlg
, BY
+1, 35+i
);
210 tty_print_char (ch_flags
[9]);
212 for (i
= 0; i
< 15; i
++){
213 dlg_move (ch_dlg
, BY
+ 1, 53 + i
);
214 tty_print_char (ch_flags
[10]);
218 static void update_mode (Dlg_head
* h
)
221 tty_setcolor (COLOR_NORMAL
);
222 dlg_move (h
, BY
+ 2, 9);
223 tty_printf ("%12o", get_mode ());
224 send_message (h
->current
, WIDGET_FOCUS
, 0);
228 chl_callback (Dlg_head
*h
, Widget
*sender
,
229 dlg_msg_t msg
, int parm
, void *data
)
241 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
246 do_enter_key (Dlg_head
* h
, int f_pos
)
250 struct passwd
*chl_pass
;
251 struct group
*chl_grp
;
253 int lxx
, lyy
, chl_end
, b_pos
;
258 is_owner
= (f_pos
== 3);
259 title
= is_owner
? _("owner") : _("group");
261 lxx
= (COLS
- 74) / 2 + (is_owner
? 35 : 53);
262 lyy
= (LINES
- 13) / 2;
266 create_dlg (lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
267 "[Advanced Chown]", title
, DLG_COMPACT
| DLG_REVERSE
);
269 /* get new listboxes */
270 chl_list
= listbox_new (1, 1, 11, 15, NULL
);
272 listbox_add_item (chl_list
, LISTBOX_APPEND_AT_END
, 0,
276 /* get and put user names in the listbox */
278 while ((chl_pass
= getpwent ())) {
279 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0,
280 chl_pass
->pw_name
, NULL
);
283 fe
= listbox_search_text (chl_list
,
284 get_owner (sf_stat
->st_uid
));
286 /* get and put group names in the listbox */
288 while ((chl_grp
= getgrent ())) {
289 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0,
290 chl_grp
->gr_name
, NULL
);
293 fe
= listbox_search_text (chl_list
,
294 get_group (sf_stat
->st_gid
));
298 listbox_select_entry (chl_list
, fe
);
300 b_pos
= chl_list
->pos
;
301 add_widget (chl_dlg
, chl_list
);
305 if (b_pos
!= chl_list
->pos
) {
308 chl_pass
= getpwnam (chl_list
->current
->text
);
311 sf_stat
->st_uid
= chl_pass
->pw_uid
;
314 chl_grp
= getgrnam (chl_list
->current
->text
);
316 sf_stat
->st_gid
= chl_grp
->gr_gid
;
321 ch_flags
[f_pos
+ 6] = '+';
328 if (chl_dlg
->ret_value
== KEY_LEFT
) {
333 } else if (chl_dlg
->ret_value
== KEY_RIGHT
) {
336 dlg_one_down (ch_dlg
);
339 /* Here we used to redraw the window */
340 destroy_dlg (chl_dlg
);
344 static void chown_refresh (void)
346 common_dialog_repaint (ch_dlg
);
348 tty_setcolor (COLOR_NORMAL
);
350 dlg_move (ch_dlg
, BY
- 1, 8);
351 tty_print_string (_("owner"));
352 dlg_move (ch_dlg
, BY
- 1, 16);
353 tty_print_string (_("group"));
354 dlg_move (ch_dlg
, BY
- 1, 24);
355 tty_print_string (_("other"));
357 dlg_move (ch_dlg
, BY
- 1, 35);
358 tty_print_string (_("owner"));
359 dlg_move (ch_dlg
, BY
- 1, 53);
360 tty_print_string (_("group"));
362 dlg_move (ch_dlg
, 3, 4);
363 tty_print_string (_("On"));
364 dlg_move (ch_dlg
, BY
+ 1, 4);
365 tty_print_string (_("Flag"));
366 dlg_move (ch_dlg
, BY
+ 2, 4);
367 tty_print_string (_("Mode"));
370 dlg_move (ch_dlg
, 3, 54);
371 tty_printf (_("%6d of %d"),
372 files_on_begin
- (current_panel
->marked
) + 1,
379 static void chown_info_update (void)
381 /* display file info */
382 tty_setcolor (COLOR_NORMAL
);
385 dlg_move (ch_dlg
, 3, 8);
386 tty_print_string (str_fit_to_term (fname
, 45, J_LEFT_FIT
));
387 dlg_move (ch_dlg
, BY
+ 2, 9);
388 tty_printf ("%12o", get_mode ());
391 update_permissions ();
394 static void b_setpos (int f_pos
) {
398 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
402 advanced_chown_callback (Dlg_head
*h
, Widget
*sender
,
403 dlg_msg_t msg
, int parm
, void *data
)
405 int i
= 0, f_pos
= BUTTONS
- h
->current
->dlg_id
- single_set
- 1;
410 chown_info_update ();
420 if ((flag_pos
/ 3) != f_pos
)
421 flag_pos
= f_pos
* 3;
423 } else if (f_pos
< 5)
424 flag_pos
= f_pos
+ 6;
433 return (dec_flag_pos (f_pos
));
439 return (inc_flag_pos (f_pos
));
449 if (f_pos
<= 2 || f_pos
>= 5)
451 do_enter_key (h
, f_pos
);
462 for (i
= 0; i
< 3; i
++)
463 ch_flags
[i
* 3 + parm
- 3] =
464 (x_toggle
& (1 << parm
)) ? '-' : '+';
465 x_toggle
^= (1 << parm
);
467 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
468 send_message (h
->current
, WIDGET_FOCUS
, 0);
479 for (i
= 0; i
< 3; i
++)
480 ch_flags
[i
* 3 + parm
] =
481 (x_toggle
& (1 << parm
)) ? '-' : '+';
482 x_toggle
^= (1 << parm
);
484 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
485 send_message (h
->current
, WIDGET_FOCUS
, 0);
497 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,parm)-ch_perm); */
498 if (((WButton
*) h
->current
)->text
.start
[(flag_pos
% 3)] ==
500 ch_flags
[flag_pos
] = '+';
502 ch_flags
[flag_pos
] = '-';
515 flag_pos
= i
+ f_pos
* 3;
516 ch_flags
[flag_pos
] = '=';
531 ch_flags
[flag_pos
] = parm
;
533 advanced_chown_callback (h
, sender
, DLG_KEY
, KEY_RIGHT
, NULL
);
534 if (flag_pos
> 8 || !(flag_pos
% 3))
539 return MSG_NOT_HANDLED
;
542 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
547 init_chown_advanced (void)
550 enum { dlg_h
= 13, dlg_w
= 74, n_elem
= 4 };
552 static int i18n_len
= 0;
556 for (i
= 0 ; i
< n_elem
; i
++) {
557 chown_advanced_but
[i
].text
= _(chown_advanced_but
[i
].text
);
558 i18n_len
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3;
559 if (DEFPUSH_BUTTON
== chown_advanced_but
[i
].flags
)
560 i18n_len
+= 2; /* "<>" */
562 cx
= dx
= (dlg_w
- i18n_len
- 2) / (n_elem
+ 1);
565 for (i
= n_elem
- 1; i
>= 0; i
--) {
566 chown_advanced_but
[i
].x
= cx
;
567 cx
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3 + dx
;
570 #endif /* ENABLE_NLS */
572 sf_stat
= g_new (struct stat
, 1);
574 end_chown
= need_update
= current_file
= 0;
575 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
576 memset (ch_flags
, '=', 11);
581 create_dlg (0, 0, dlg_h
, dlg_w
, dialog_colors
, advanced_chown_callback
,
582 "[Advanced Chown]", _(" Chown advanced command "),
583 DLG_CENTER
| DLG_REVERSE
);
585 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
586 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
587 (chown_advanced_but[i].text), 0
589 for (i
= 0; i
< BUTTONS
- 5; i
++)
590 if (!single_set
|| i
< 2)
591 add_widget (ch_dlg
, button_new (XTRACT (i
)));
593 b_att
[0] = button_new (XTRACT (8));
594 b_att
[1] = button_new (XTRACT (7));
595 b_att
[2] = button_new (XTRACT (6));
596 b_user
= button_new (XTRACT (5));
597 b_group
= button_new (XTRACT (4));
599 add_widget (ch_dlg
, b_group
);
600 add_widget (ch_dlg
, b_user
);
601 add_widget (ch_dlg
, b_att
[2]);
602 add_widget (ch_dlg
, b_att
[1]);
603 add_widget (ch_dlg
, b_att
[0]);
607 chown_advanced_done (void)
611 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
616 static void do_chown (uid_t u
, gid_t g
)
618 chown (current_panel
->dir
.list
[current_file
].fname
, u
, g
);
619 file_mark (current_panel
, current_file
, 0);
623 static char *next_file (void)
625 while (!current_panel
->dir
.list
[current_file
].f
.marked
)
628 return current_panel
->dir
.list
[current_file
].fname
;
631 static void apply_advanced_chowns (struct stat
*sf
)
634 gid_t a_gid
= sf
->st_gid
;
635 uid_t a_uid
= sf
->st_uid
;
637 lc_fname
= current_panel
->dir
.list
[current_file
].fname
;
638 need_update
= end_chown
= 1;
639 if (mc_chmod (lc_fname
, get_mode ()) == -1)
640 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
641 lc_fname
, unix_error_string (errno
));
642 /* call mc_chown only, if mc_chmod didn't fail */
643 else if (mc_chown (lc_fname
, (ch_flags
[9] == '+') ? sf
->st_uid
: (uid_t
) -1,
644 (ch_flags
[10] == '+') ? sf
->st_gid
: (gid_t
) -1) == -1)
645 message (D_ERROR
, MSG_ERROR
, _(" Cannot chown \"%s\" \n %s "),
646 lc_fname
, unix_error_string (errno
));
647 do_file_mark (current_panel
, current_file
, 0);
650 lc_fname
= next_file ();
652 if (mc_stat (lc_fname
, sf
) != 0)
654 ch_cmode
= sf
->st_mode
;
655 if (mc_chmod (lc_fname
, get_mode ()) == -1)
656 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
657 lc_fname
, unix_error_string (errno
));
658 /* call mc_chown only, if mc_chmod didn't fail */
659 else if (mc_chown (lc_fname
, (ch_flags
[9] == '+') ? a_uid
: (uid_t
) -1,
660 (ch_flags
[10] == '+') ? a_gid
: (gid_t
) -1) == -1)
661 message (D_ERROR
, MSG_ERROR
, _(" Cannot chown \"%s\" \n %s "),
662 lc_fname
, unix_error_string (errno
));
664 do_file_mark (current_panel
, current_file
, 0);
665 } while (current_panel
->marked
);
669 chown_advanced_cmd (void)
672 files_on_begin
= current_panel
->marked
;
674 do { /* do while any files remaining */
675 init_chown_advanced ();
677 if (current_panel
->marked
)
678 fname
= next_file (); /* next marked file */
680 fname
= selection (current_panel
)->fname
; /* single file */
682 if (mc_stat (fname
, sf_stat
) != 0) { /* get status of file */
683 destroy_dlg (ch_dlg
);
686 ch_cmode
= sf_stat
->st_mode
;
695 switch (ch_dlg
->ret_value
) {
702 if (mc_chmod (fname
, get_mode ()) == -1)
703 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
704 fname
, unix_error_string (errno
));
705 /* call mc_chown only, if mc_chmod didn't fail */
706 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: (uid_t
) -1,
707 (ch_flags
[10] == '+') ? sf_stat
->st_gid
: (gid_t
) -1) == -1)
708 message (D_ERROR
, MSG_ERROR
, _(" Cannot chown \"%s\" \n %s "),
709 fname
, unix_error_string (errno
));
712 apply_advanced_chowns (sf_stat
);
720 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
) {
721 do_file_mark (current_panel
, current_file
, 0);
724 destroy_dlg (ch_dlg
);
725 } while (current_panel
->marked
&& !end_chown
);
727 chown_advanced_done ();