2 Chown-advanced command -- for the Midnight Commander
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
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: Contains functions for advanced chowning
34 #include <sys/types.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/strutil.h"
46 #include "lib/vfs/vfs.h"
48 #include "lib/widget.h"
51 #include "midnight.h" /* current_panel */
56 /*** global variables ****************************************************************************/
58 /*** file scope macro definitions ****************************************************************/
68 #define B_SETALL B_USER
69 #define B_SKIP (B_USER + 1)
71 #define B_OWN (B_USER + 3)
72 #define B_GRP (B_USER + 4)
73 #define B_OTH (B_USER + 5)
74 #define B_OUSER (B_USER + 6)
75 #define B_OGROUP (B_USER + 7)
77 /*** file scope type declarations ****************************************************************/
79 /*** file scope variables ************************************************************************/
81 static struct Dlg_head
*ch_dlg
;
85 int ret_cmd
, flags
, y
, x
;
87 } chown_advanced_but
[BUTTONS
] =
90 { B_CANCEL
, NORMAL_BUTTON
, 4, 53, N_("&Cancel") },
91 { B_ENTER
, DEFPUSH_BUTTON
,4, 40, N_("&Set") },
92 { B_SKIP
, NORMAL_BUTTON
, 4, 23, N_("S&kip") },
93 { B_SETALL
, NORMAL_BUTTON
, 4, 0, N_("Set &all")},
94 { B_ENTER
, NARROW_BUTTON
, 0, 47, ""},
95 { B_ENTER
, NARROW_BUTTON
, 0, 29, ""},
96 { B_ENTER
, NARROW_BUTTON
, 0, 19, " "},
97 { B_ENTER
, NARROW_BUTTON
, 0, 11, " "},
98 { B_ENTER
, NARROW_BUTTON
, 0, 3, " "}
102 static WButton
*b_att
[3]; /* permission */
103 static WButton
*b_user
, *b_group
; /* owner */
105 static int files_on_begin
; /* Number of files at startup */
108 static char ch_flags
[11];
109 static const char ch_perm
[] = "rwx";
110 static mode_t ch_cmode
;
111 static struct stat
*sf_stat
;
112 static int need_update
;
113 static int end_chown
;
114 static int current_file
;
115 static int single_set
;
118 /*** file scope functions ************************************************************************/
119 /* --------------------------------------------------------------------------------------------- */
122 update_ownership (void)
124 button_set_text (b_user
, get_owner (sf_stat
->st_uid
));
125 button_set_text (b_group
, get_group (sf_stat
->st_gid
));
128 /* --------------------------------------------------------------------------------------------- */
131 inc_flag_pos (int f_pos
)
136 return MSG_NOT_HANDLED
;
139 if (!(flag_pos
% 3) || f_pos
> 2)
140 return MSG_NOT_HANDLED
;
144 /* --------------------------------------------------------------------------------------------- */
147 dec_flag_pos (int f_pos
)
152 return MSG_NOT_HANDLED
;
155 if (!((flag_pos
+ 1) % 3) || f_pos
> 2)
156 return MSG_NOT_HANDLED
;
160 /* --------------------------------------------------------------------------------------------- */
163 set_perm_by_flags (char *s
, int f_p
)
167 for (i
= 0; i
< 3; i
++)
169 if (ch_flags
[f_p
+ i
] == '+')
171 else if (ch_flags
[f_p
+ i
] == '-')
174 s
[i
] = (ch_cmode
& (1 << (8 - f_p
- i
))) ? ch_perm
[i
] : '-';
178 /* --------------------------------------------------------------------------------------------- */
181 update_permissions (void)
183 set_perm_by_flags (b_att
[0]->text
.start
, 0);
184 set_perm_by_flags (b_att
[1]->text
.start
, 3);
185 set_perm_by_flags (b_att
[2]->text
.start
, 6);
188 /* --------------------------------------------------------------------------------------------- */
191 get_perm (char *s
, int base
)
196 m
|= (s
[0] == '-') ? 0 :
197 ((s
[0] == '+') ? (mode_t
) (1 << (base
+ 2)) : (1 << (base
+ 2)) & ch_cmode
);
199 m
|= (s
[1] == '-') ? 0 :
200 ((s
[1] == '+') ? (mode_t
) (1 << (base
+ 1)) : (1 << (base
+ 1)) & ch_cmode
);
202 m
|= (s
[2] == '-') ? 0 : ((s
[2] == '+') ? (mode_t
) (1 << base
) : (1 << base
) & ch_cmode
);
207 /* --------------------------------------------------------------------------------------------- */
214 m
= ch_cmode
^ (ch_cmode
& 0777);
215 m
|= get_perm (ch_flags
, 6);
216 m
|= get_perm (ch_flags
+ 3, 3);
217 m
|= get_perm (ch_flags
+ 6, 0);
222 /* --------------------------------------------------------------------------------------------- */
229 tty_setcolor (COLOR_NORMAL
);
231 for (i
= 0; i
< 3; i
++)
233 dlg_move (ch_dlg
, BY
+ 1, 9 + i
);
234 tty_print_char (ch_flags
[i
]);
237 for (i
= 0; i
< 3; i
++)
239 dlg_move (ch_dlg
, BY
+ 1, 17 + i
);
240 tty_print_char (ch_flags
[i
+ 3]);
243 for (i
= 0; i
< 3; i
++)
245 dlg_move (ch_dlg
, BY
+ 1, 25 + i
);
246 tty_print_char (ch_flags
[i
+ 6]);
249 update_permissions ();
251 for (i
= 0; i
< 15; i
++)
253 dlg_move (ch_dlg
, BY
+ 1, 35 + i
);
254 tty_print_char (ch_flags
[9]);
256 for (i
= 0; i
< 15; i
++)
258 dlg_move (ch_dlg
, BY
+ 1, 53 + i
);
259 tty_print_char (ch_flags
[10]);
263 /* --------------------------------------------------------------------------------------------- */
266 update_mode (Dlg_head
* h
)
269 tty_setcolor (COLOR_NORMAL
);
270 dlg_move (h
, BY
+ 2, 9);
271 tty_printf ("%12o", get_mode ());
272 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
275 /* --------------------------------------------------------------------------------------------- */
278 chl_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
292 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
296 /* --------------------------------------------------------------------------------------------- */
299 do_enter_key (Dlg_head
* h
, int f_pos
)
303 struct passwd
*chl_pass
;
304 struct group
*chl_grp
;
306 int lxx
, lyy
, chl_end
, b_pos
;
312 is_owner
= (f_pos
== 3);
313 title
= is_owner
? _("owner") : _("group");
315 lxx
= (COLS
- 74) / 2 + (is_owner
? 35 : 53);
316 lyy
= (LINES
- 13) / 2;
320 create_dlg (TRUE
, lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
321 "[Advanced Chown]", title
, DLG_COMPACT
| DLG_REVERSE
);
323 /* get new listboxes */
324 chl_list
= listbox_new (1, 1, 11, 15, FALSE
, NULL
);
326 listbox_add_item (chl_list
, LISTBOX_APPEND_AT_END
, 0, "<Unknown>", NULL
);
330 /* get and put user names in the listbox */
332 while ((chl_pass
= getpwent ()))
334 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0, chl_pass
->pw_name
, NULL
);
337 fe
= listbox_search_text (chl_list
, get_owner (sf_stat
->st_uid
));
341 /* get and put group names in the listbox */
343 while ((chl_grp
= getgrent ()))
345 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0, chl_grp
->gr_name
, NULL
);
348 fe
= listbox_search_text (chl_list
, get_group (sf_stat
->st_gid
));
351 listbox_select_entry (chl_list
, fe
);
353 b_pos
= chl_list
->pos
;
354 add_widget (chl_dlg
, chl_list
);
358 if (b_pos
!= chl_list
->pos
)
363 listbox_get_current (chl_list
, &text
, NULL
);
366 chl_pass
= getpwnam (text
);
370 sf_stat
->st_uid
= chl_pass
->pw_uid
;
375 chl_grp
= getgrnam (text
);
378 sf_stat
->st_gid
= chl_grp
->gr_gid
;
384 ch_flags
[f_pos
+ 6] = '+';
391 if (chl_dlg
->ret_value
== KEY_LEFT
)
398 else if (chl_dlg
->ret_value
== KEY_RIGHT
)
402 dlg_one_down (ch_dlg
);
405 /* Here we used to redraw the window */
406 destroy_dlg (chl_dlg
);
411 /* --------------------------------------------------------------------------------------------- */
416 common_dialog_repaint (ch_dlg
);
418 tty_setcolor (COLOR_NORMAL
);
420 dlg_move (ch_dlg
, BY
- 1, 8);
421 tty_print_string (_("owner"));
422 dlg_move (ch_dlg
, BY
- 1, 16);
423 tty_print_string (_("group"));
424 dlg_move (ch_dlg
, BY
- 1, 24);
425 tty_print_string (_("other"));
427 dlg_move (ch_dlg
, BY
- 1, 35);
428 tty_print_string (_("owner"));
429 dlg_move (ch_dlg
, BY
- 1, 53);
430 tty_print_string (_("group"));
432 dlg_move (ch_dlg
, 3, 4);
433 tty_print_string (_("On"));
434 dlg_move (ch_dlg
, BY
+ 1, 4);
435 tty_print_string (_("Flag"));
436 dlg_move (ch_dlg
, BY
+ 2, 4);
437 tty_print_string (_("Mode"));
441 dlg_move (ch_dlg
, 3, 54);
442 tty_printf (_("%6d of %d"), files_on_begin
- (current_panel
->marked
) + 1, files_on_begin
);
448 /* --------------------------------------------------------------------------------------------- */
451 chown_info_update (void)
453 /* display file info */
454 tty_setcolor (COLOR_NORMAL
);
457 dlg_move (ch_dlg
, 3, 8);
458 tty_print_string (str_fit_to_term (fname
, 45, J_LEFT_FIT
));
459 dlg_move (ch_dlg
, BY
+ 2, 9);
460 tty_printf ("%12o", get_mode ());
463 update_permissions ();
466 /* --------------------------------------------------------------------------------------------- */
471 b_att
[0]->hotpos
= -1;
472 b_att
[1]->hotpos
= -1;
473 b_att
[2]->hotpos
= -1;
474 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
477 /* --------------------------------------------------------------------------------------------- */
480 advanced_chown_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
482 int i
= 0, f_pos
= BUTTONS
- dlg_get_current_widget_id (h
) - single_set
- 1;
488 chown_info_update ();
499 if ((flag_pos
/ 3) != f_pos
)
500 flag_pos
= f_pos
* 3;
504 flag_pos
= f_pos
+ 6;
514 return (dec_flag_pos (f_pos
));
520 return (inc_flag_pos (f_pos
));
530 if (f_pos
<= 2 || f_pos
>= 5)
532 do_enter_key (h
, f_pos
);
543 for (i
= 0; i
< 3; i
++)
544 ch_flags
[i
* 3 + parm
- 3] = (x_toggle
& (1 << parm
)) ? '-' : '+';
545 x_toggle
^= (1 << parm
);
547 dlg_broadcast_msg (h
, WIDGET_DRAW
, FALSE
);
548 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
559 for (i
= 0; i
< 3; i
++)
560 ch_flags
[i
* 3 + parm
] = (x_toggle
& (1 << parm
)) ? '-' : '+';
561 x_toggle
^= (1 << parm
);
563 dlg_broadcast_msg (h
, WIDGET_DRAW
, FALSE
);
564 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
576 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,parm)-ch_perm); */
577 if (((WButton
*) h
->current
->data
)->text
.start
[(flag_pos
% 3)] == '-')
578 ch_flags
[flag_pos
] = '+';
580 ch_flags
[flag_pos
] = '-';
593 flag_pos
= i
+ f_pos
* 3;
594 ch_flags
[flag_pos
] = '=';
610 ch_flags
[flag_pos
] = parm
;
612 advanced_chown_callback (h
, sender
, DLG_KEY
, KEY_RIGHT
, NULL
);
613 if (flag_pos
> 8 || !(flag_pos
% 3))
618 return MSG_NOT_HANDLED
;
621 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
625 /* --------------------------------------------------------------------------------------------- */
628 init_chown_advanced (void)
632 { dlg_h
= 13, dlg_w
= 74, n_elem
= 4 };
634 static int i18n_len
= 0;
639 for (i
= 0; i
< n_elem
; i
++)
641 chown_advanced_but
[i
].text
= _(chown_advanced_but
[i
].text
);
642 i18n_len
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3;
643 if (DEFPUSH_BUTTON
== chown_advanced_but
[i
].flags
)
644 i18n_len
+= 2; /* "<>" */
646 cx
= dx
= (dlg_w
- i18n_len
- 2) / (n_elem
+ 1);
649 for (i
= n_elem
- 1; i
>= 0; i
--)
651 chown_advanced_but
[i
].x
= cx
;
652 cx
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3 + dx
;
655 #endif /* ENABLE_NLS */
657 sf_stat
= g_new (struct stat
, 1);
659 end_chown
= need_update
= current_file
= 0;
660 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
661 memset (ch_flags
, '=', 11);
666 create_dlg (TRUE
, 0, 0, dlg_h
, dlg_w
, dialog_colors
, advanced_chown_callback
,
667 "[Advanced Chown]", _("Chown advanced command"), DLG_CENTER
| DLG_REVERSE
);
669 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
670 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
671 (chown_advanced_but[i].text), 0
673 for (i
= 0; i
< BUTTONS
- 5; i
++)
674 if (!single_set
|| i
< 2)
675 add_widget (ch_dlg
, button_new (XTRACT (i
)));
677 b_att
[0] = button_new (XTRACT (8));
678 b_att
[1] = button_new (XTRACT (7));
679 b_att
[2] = button_new (XTRACT (6));
680 b_user
= button_new (XTRACT (5));
681 b_group
= button_new (XTRACT (4));
683 add_widget (ch_dlg
, b_group
);
684 add_widget (ch_dlg
, b_user
);
685 add_widget (ch_dlg
, b_att
[2]);
686 add_widget (ch_dlg
, b_att
[1]);
687 add_widget (ch_dlg
, b_att
[0]);
690 /* --------------------------------------------------------------------------------------------- */
693 chown_advanced_done (void)
697 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
701 /* --------------------------------------------------------------------------------------------- */
705 do_chown (uid_t u
, gid_t g
)
707 chown (current_panel
->dir
.list
[current_file
].fname
, u
, g
);
708 file_mark (current_panel
, current_file
, 0);
712 /* --------------------------------------------------------------------------------------------- */
717 while (!current_panel
->dir
.list
[current_file
].f
.marked
)
720 return current_panel
->dir
.list
[current_file
].fname
;
723 /* --------------------------------------------------------------------------------------------- */
726 apply_advanced_chowns (struct stat
*sf
)
729 gid_t a_gid
= sf
->st_gid
;
730 uid_t a_uid
= sf
->st_uid
;
732 lc_fname
= current_panel
->dir
.list
[current_file
].fname
;
733 need_update
= end_chown
= 1;
734 if (mc_chmod (lc_fname
, get_mode ()) == -1)
735 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
736 lc_fname
, unix_error_string (errno
));
737 /* call mc_chown only, if mc_chmod didn't fail */
738 else if (mc_chown (lc_fname
, (ch_flags
[9] == '+') ? sf
->st_uid
: (uid_t
) - 1,
739 (ch_flags
[10] == '+') ? sf
->st_gid
: (gid_t
) - 1) == -1)
740 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
741 lc_fname
, unix_error_string (errno
));
742 do_file_mark (current_panel
, current_file
, 0);
748 lc_fname
= next_file ();
749 vpath
= vfs_path_from_str (lc_fname
);
751 if (mc_stat (vpath
, sf
) != 0)
753 vfs_path_free (vpath
);
756 ch_cmode
= sf
->st_mode
;
757 if (mc_chmod (lc_fname
, get_mode ()) == -1)
758 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
759 lc_fname
, unix_error_string (errno
));
760 /* call mc_chown only, if mc_chmod didn't fail */
761 else if (mc_chown (lc_fname
, (ch_flags
[9] == '+') ? a_uid
: (uid_t
) - 1,
762 (ch_flags
[10] == '+') ? a_gid
: (gid_t
) - 1) == -1)
763 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
764 lc_fname
, unix_error_string (errno
));
766 do_file_mark (current_panel
, current_file
, 0);
767 vfs_path_free (vpath
);
769 while (current_panel
->marked
);
772 /* --------------------------------------------------------------------------------------------- */
773 /*** public functions ****************************************************************************/
774 /* --------------------------------------------------------------------------------------------- */
777 chown_advanced_cmd (void)
780 files_on_begin
= current_panel
->marked
;
783 { /* do while any files remaining */
785 init_chown_advanced ();
787 if (current_panel
->marked
)
788 fname
= next_file (); /* next marked file */
790 fname
= selection (current_panel
)->fname
; /* single file */
791 vpath
= vfs_path_from_str (fname
);
793 if (mc_stat (vpath
, sf_stat
) != 0)
794 { /* get status of file */
795 destroy_dlg (ch_dlg
);
796 vfs_path_free (vpath
);
799 ch_cmode
= sf_stat
->st_mode
;
808 switch (ch_dlg
->ret_value
)
816 if (mc_chmod (fname
, get_mode ()) == -1)
817 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
818 fname
, unix_error_string (errno
));
819 /* call mc_chown only, if mc_chmod didn't fail */
820 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: (uid_t
) - 1,
821 (ch_flags
[10] == '+') ? sf_stat
->st_gid
: (gid_t
) - 1) == -1)
822 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
823 fname
, unix_error_string (errno
));
826 apply_advanced_chowns (sf_stat
);
834 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
)
836 do_file_mark (current_panel
, current_file
, 0);
839 destroy_dlg (ch_dlg
);
840 vfs_path_free (vpath
);
843 while (current_panel
->marked
&& !end_chown
);
845 chown_advanced_done ();
848 /* --------------------------------------------------------------------------------------------- */