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>
36 #include "lib/global.h"
38 #include "lib/tty/tty.h"
39 #include "lib/tty/key.h" /* XCTRL and ALT macros */
41 #include "lib/strutil.h"
42 #include "lib/vfs/mc-vfs/vfs.h"
44 #include "lib/widget.h"
47 #include "midnight.h" /* current_panel */
49 #include "layout.h" /* repaint_screen() */
53 /*** global variables ****************************************************************************/
55 /*** file scope macro definitions ****************************************************************/
65 #define B_SETALL B_USER
66 #define B_SKIP (B_USER + 1)
68 #define B_OWN (B_USER + 3)
69 #define B_GRP (B_USER + 4)
70 #define B_OTH (B_USER + 5)
71 #define B_OUSER (B_USER + 6)
72 #define B_OGROUP (B_USER + 7)
74 /*** file scope type declarations ****************************************************************/
76 /*** file scope variables ************************************************************************/
78 static struct Dlg_head
*ch_dlg
;
82 int ret_cmd
, flags
, y
, x
;
84 } chown_advanced_but
[BUTTONS
] =
87 { B_CANCEL
, NORMAL_BUTTON
, 4, 53, N_("&Cancel") },
88 { B_ENTER
, DEFPUSH_BUTTON
,4, 40, N_("&Set") },
89 { B_SKIP
, NORMAL_BUTTON
, 4, 23, N_("S&kip") },
90 { B_SETALL
, NORMAL_BUTTON
, 4, 0, N_("Set &all")},
91 { B_ENTER
, NARROW_BUTTON
, 0, 47, ""},
92 { B_ENTER
, NARROW_BUTTON
, 0, 29, ""},
93 { B_ENTER
, NARROW_BUTTON
, 0, 19, " "},
94 { B_ENTER
, NARROW_BUTTON
, 0, 11, " "},
95 { B_ENTER
, NARROW_BUTTON
, 0, 3, " "}
99 static WButton
*b_att
[3]; /* permission */
100 static WButton
*b_user
, *b_group
; /* owner */
102 static int files_on_begin
; /* Number of files at startup */
105 static char ch_flags
[11];
106 static const char ch_perm
[] = "rwx";
107 static mode_t ch_cmode
;
108 static struct stat
*sf_stat
;
109 static int need_update
;
110 static int end_chown
;
111 static int current_file
;
112 static int single_set
;
115 /*** file scope functions ************************************************************************/
116 /* --------------------------------------------------------------------------------------------- */
119 update_ownership (void)
121 button_set_text (b_user
, get_owner (sf_stat
->st_uid
));
122 button_set_text (b_group
, get_group (sf_stat
->st_gid
));
125 /* --------------------------------------------------------------------------------------------- */
128 inc_flag_pos (int f_pos
)
133 return MSG_NOT_HANDLED
;
136 if (!(flag_pos
% 3) || f_pos
> 2)
137 return MSG_NOT_HANDLED
;
141 /* --------------------------------------------------------------------------------------------- */
144 dec_flag_pos (int f_pos
)
149 return MSG_NOT_HANDLED
;
152 if (!((flag_pos
+ 1) % 3) || f_pos
> 2)
153 return MSG_NOT_HANDLED
;
157 /* --------------------------------------------------------------------------------------------- */
160 set_perm_by_flags (char *s
, int f_p
)
164 for (i
= 0; i
< 3; i
++)
166 if (ch_flags
[f_p
+ i
] == '+')
168 else if (ch_flags
[f_p
+ i
] == '-')
171 s
[i
] = (ch_cmode
& (1 << (8 - f_p
- i
))) ? ch_perm
[i
] : '-';
175 /* --------------------------------------------------------------------------------------------- */
178 update_permissions (void)
180 set_perm_by_flags (b_att
[0]->text
.start
, 0);
181 set_perm_by_flags (b_att
[1]->text
.start
, 3);
182 set_perm_by_flags (b_att
[2]->text
.start
, 6);
185 /* --------------------------------------------------------------------------------------------- */
188 get_perm (char *s
, int base
)
193 m
|= (s
[0] == '-') ? 0 :
194 ((s
[0] == '+') ? (mode_t
) (1 << (base
+ 2)) : (1 << (base
+ 2)) & ch_cmode
);
196 m
|= (s
[1] == '-') ? 0 :
197 ((s
[1] == '+') ? (mode_t
) (1 << (base
+ 1)) : (1 << (base
+ 1)) & ch_cmode
);
199 m
|= (s
[2] == '-') ? 0 : ((s
[2] == '+') ? (mode_t
) (1 << base
) : (1 << base
) & ch_cmode
);
204 /* --------------------------------------------------------------------------------------------- */
211 m
= ch_cmode
^ (ch_cmode
& 0777);
212 m
|= get_perm (ch_flags
, 6);
213 m
|= get_perm (ch_flags
+ 3, 3);
214 m
|= get_perm (ch_flags
+ 6, 0);
219 /* --------------------------------------------------------------------------------------------- */
226 tty_setcolor (COLOR_NORMAL
);
228 for (i
= 0; i
< 3; i
++)
230 dlg_move (ch_dlg
, BY
+ 1, 9 + i
);
231 tty_print_char (ch_flags
[i
]);
234 for (i
= 0; i
< 3; i
++)
236 dlg_move (ch_dlg
, BY
+ 1, 17 + i
);
237 tty_print_char (ch_flags
[i
+ 3]);
240 for (i
= 0; i
< 3; i
++)
242 dlg_move (ch_dlg
, BY
+ 1, 25 + i
);
243 tty_print_char (ch_flags
[i
+ 6]);
246 update_permissions ();
248 for (i
= 0; i
< 15; i
++)
250 dlg_move (ch_dlg
, BY
+ 1, 35 + i
);
251 tty_print_char (ch_flags
[9]);
253 for (i
= 0; i
< 15; i
++)
255 dlg_move (ch_dlg
, BY
+ 1, 53 + i
);
256 tty_print_char (ch_flags
[10]);
260 /* --------------------------------------------------------------------------------------------- */
263 update_mode (Dlg_head
* h
)
266 tty_setcolor (COLOR_NORMAL
);
267 dlg_move (h
, BY
+ 2, 9);
268 tty_printf ("%12o", get_mode ());
269 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
272 /* --------------------------------------------------------------------------------------------- */
275 chl_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
289 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
293 /* --------------------------------------------------------------------------------------------- */
296 do_enter_key (Dlg_head
* h
, int f_pos
)
300 struct passwd
*chl_pass
;
301 struct group
*chl_grp
;
303 int lxx
, lyy
, chl_end
, b_pos
;
309 is_owner
= (f_pos
== 3);
310 title
= is_owner
? _("owner") : _("group");
312 lxx
= (COLS
- 74) / 2 + (is_owner
? 35 : 53);
313 lyy
= (LINES
- 13) / 2;
317 create_dlg (TRUE
, lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
318 "[Advanced Chown]", title
, DLG_COMPACT
| DLG_REVERSE
);
320 /* get new listboxes */
321 chl_list
= listbox_new (1, 1, 11, 15, FALSE
, NULL
);
323 listbox_add_item (chl_list
, LISTBOX_APPEND_AT_END
, 0, "<Unknown>", NULL
);
327 /* get and put user names in the listbox */
329 while ((chl_pass
= getpwent ()))
331 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0, chl_pass
->pw_name
, NULL
);
334 fe
= listbox_search_text (chl_list
, get_owner (sf_stat
->st_uid
));
338 /* get and put group names in the listbox */
340 while ((chl_grp
= getgrent ()))
342 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0, chl_grp
->gr_name
, NULL
);
345 fe
= listbox_search_text (chl_list
, get_group (sf_stat
->st_gid
));
348 listbox_select_entry (chl_list
, fe
);
350 b_pos
= chl_list
->pos
;
351 add_widget (chl_dlg
, chl_list
);
355 if (b_pos
!= chl_list
->pos
)
360 listbox_get_current (chl_list
, &text
, NULL
);
363 chl_pass
= getpwnam (text
);
367 sf_stat
->st_uid
= chl_pass
->pw_uid
;
372 chl_grp
= getgrnam (text
);
375 sf_stat
->st_gid
= chl_grp
->gr_gid
;
381 ch_flags
[f_pos
+ 6] = '+';
388 if (chl_dlg
->ret_value
== KEY_LEFT
)
395 else if (chl_dlg
->ret_value
== KEY_RIGHT
)
399 dlg_one_down (ch_dlg
);
402 /* Here we used to redraw the window */
403 destroy_dlg (chl_dlg
);
408 /* --------------------------------------------------------------------------------------------- */
413 common_dialog_repaint (ch_dlg
);
415 tty_setcolor (COLOR_NORMAL
);
417 dlg_move (ch_dlg
, BY
- 1, 8);
418 tty_print_string (_("owner"));
419 dlg_move (ch_dlg
, BY
- 1, 16);
420 tty_print_string (_("group"));
421 dlg_move (ch_dlg
, BY
- 1, 24);
422 tty_print_string (_("other"));
424 dlg_move (ch_dlg
, BY
- 1, 35);
425 tty_print_string (_("owner"));
426 dlg_move (ch_dlg
, BY
- 1, 53);
427 tty_print_string (_("group"));
429 dlg_move (ch_dlg
, 3, 4);
430 tty_print_string (_("On"));
431 dlg_move (ch_dlg
, BY
+ 1, 4);
432 tty_print_string (_("Flag"));
433 dlg_move (ch_dlg
, BY
+ 2, 4);
434 tty_print_string (_("Mode"));
438 dlg_move (ch_dlg
, 3, 54);
439 tty_printf (_("%6d of %d"), files_on_begin
- (current_panel
->marked
) + 1, files_on_begin
);
445 /* --------------------------------------------------------------------------------------------- */
448 chown_info_update (void)
450 /* display file info */
451 tty_setcolor (COLOR_NORMAL
);
454 dlg_move (ch_dlg
, 3, 8);
455 tty_print_string (str_fit_to_term (fname
, 45, J_LEFT_FIT
));
456 dlg_move (ch_dlg
, BY
+ 2, 9);
457 tty_printf ("%12o", get_mode ());
460 update_permissions ();
463 /* --------------------------------------------------------------------------------------------- */
468 b_att
[0]->hotpos
= -1;
469 b_att
[1]->hotpos
= -1;
470 b_att
[2]->hotpos
= -1;
471 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
474 /* --------------------------------------------------------------------------------------------- */
477 advanced_chown_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
479 int i
= 0, f_pos
= BUTTONS
- dlg_get_current_widget_id (h
) - single_set
- 1;
485 chown_info_update ();
496 if ((flag_pos
/ 3) != f_pos
)
497 flag_pos
= f_pos
* 3;
501 flag_pos
= f_pos
+ 6;
511 return (dec_flag_pos (f_pos
));
517 return (inc_flag_pos (f_pos
));
527 if (f_pos
<= 2 || f_pos
>= 5)
529 do_enter_key (h
, f_pos
);
540 for (i
= 0; i
< 3; i
++)
541 ch_flags
[i
* 3 + parm
- 3] = (x_toggle
& (1 << parm
)) ? '-' : '+';
542 x_toggle
^= (1 << parm
);
544 dlg_broadcast_msg (h
, WIDGET_DRAW
, FALSE
);
545 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
556 for (i
= 0; i
< 3; i
++)
557 ch_flags
[i
* 3 + parm
] = (x_toggle
& (1 << parm
)) ? '-' : '+';
558 x_toggle
^= (1 << parm
);
560 dlg_broadcast_msg (h
, WIDGET_DRAW
, FALSE
);
561 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
573 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,parm)-ch_perm); */
574 if (((WButton
*) h
->current
->data
)->text
.start
[(flag_pos
% 3)] == '-')
575 ch_flags
[flag_pos
] = '+';
577 ch_flags
[flag_pos
] = '-';
590 flag_pos
= i
+ f_pos
* 3;
591 ch_flags
[flag_pos
] = '=';
607 ch_flags
[flag_pos
] = parm
;
609 advanced_chown_callback (h
, sender
, DLG_KEY
, KEY_RIGHT
, NULL
);
610 if (flag_pos
> 8 || !(flag_pos
% 3))
615 return MSG_NOT_HANDLED
;
618 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
622 /* --------------------------------------------------------------------------------------------- */
625 init_chown_advanced (void)
629 { dlg_h
= 13, dlg_w
= 74, n_elem
= 4 };
631 static int i18n_len
= 0;
636 for (i
= 0; i
< n_elem
; i
++)
638 chown_advanced_but
[i
].text
= _(chown_advanced_but
[i
].text
);
639 i18n_len
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3;
640 if (DEFPUSH_BUTTON
== chown_advanced_but
[i
].flags
)
641 i18n_len
+= 2; /* "<>" */
643 cx
= dx
= (dlg_w
- i18n_len
- 2) / (n_elem
+ 1);
646 for (i
= n_elem
- 1; i
>= 0; i
--)
648 chown_advanced_but
[i
].x
= cx
;
649 cx
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3 + dx
;
652 #endif /* ENABLE_NLS */
654 sf_stat
= g_new (struct stat
, 1);
656 end_chown
= need_update
= current_file
= 0;
657 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
658 memset (ch_flags
, '=', 11);
663 create_dlg (TRUE
, 0, 0, dlg_h
, dlg_w
, dialog_colors
, advanced_chown_callback
,
664 "[Advanced Chown]", _("Chown advanced command"), DLG_CENTER
| DLG_REVERSE
);
666 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
667 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
668 (chown_advanced_but[i].text), 0
670 for (i
= 0; i
< BUTTONS
- 5; i
++)
671 if (!single_set
|| i
< 2)
672 add_widget (ch_dlg
, button_new (XTRACT (i
)));
674 b_att
[0] = button_new (XTRACT (8));
675 b_att
[1] = button_new (XTRACT (7));
676 b_att
[2] = button_new (XTRACT (6));
677 b_user
= button_new (XTRACT (5));
678 b_group
= button_new (XTRACT (4));
680 add_widget (ch_dlg
, b_group
);
681 add_widget (ch_dlg
, b_user
);
682 add_widget (ch_dlg
, b_att
[2]);
683 add_widget (ch_dlg
, b_att
[1]);
684 add_widget (ch_dlg
, b_att
[0]);
687 /* --------------------------------------------------------------------------------------------- */
690 chown_advanced_done (void)
694 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
698 /* --------------------------------------------------------------------------------------------- */
702 do_chown (uid_t u
, gid_t g
)
704 chown (current_panel
->dir
.list
[current_file
].fname
, u
, g
);
705 file_mark (current_panel
, current_file
, 0);
709 /* --------------------------------------------------------------------------------------------- */
714 while (!current_panel
->dir
.list
[current_file
].f
.marked
)
717 return current_panel
->dir
.list
[current_file
].fname
;
720 /* --------------------------------------------------------------------------------------------- */
723 apply_advanced_chowns (struct stat
*sf
)
726 gid_t a_gid
= sf
->st_gid
;
727 uid_t a_uid
= sf
->st_uid
;
729 lc_fname
= current_panel
->dir
.list
[current_file
].fname
;
730 need_update
= end_chown
= 1;
731 if (mc_chmod (lc_fname
, get_mode ()) == -1)
732 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
733 lc_fname
, unix_error_string (errno
));
734 /* call mc_chown only, if mc_chmod didn't fail */
735 else if (mc_chown (lc_fname
, (ch_flags
[9] == '+') ? sf
->st_uid
: (uid_t
) - 1,
736 (ch_flags
[10] == '+') ? sf
->st_gid
: (gid_t
) - 1) == -1)
737 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
738 lc_fname
, unix_error_string (errno
));
739 do_file_mark (current_panel
, current_file
, 0);
743 lc_fname
= next_file ();
745 if (mc_stat (lc_fname
, sf
) != 0)
747 ch_cmode
= sf
->st_mode
;
748 if (mc_chmod (lc_fname
, get_mode ()) == -1)
749 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
750 lc_fname
, unix_error_string (errno
));
751 /* call mc_chown only, if mc_chmod didn't fail */
752 else if (mc_chown (lc_fname
, (ch_flags
[9] == '+') ? a_uid
: (uid_t
) - 1,
753 (ch_flags
[10] == '+') ? a_gid
: (gid_t
) - 1) == -1)
754 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
755 lc_fname
, unix_error_string (errno
));
757 do_file_mark (current_panel
, current_file
, 0);
759 while (current_panel
->marked
);
762 /* --------------------------------------------------------------------------------------------- */
763 /*** public functions ****************************************************************************/
764 /* --------------------------------------------------------------------------------------------- */
767 chown_advanced_cmd (void)
770 files_on_begin
= current_panel
->marked
;
773 { /* do while any files remaining */
774 init_chown_advanced ();
776 if (current_panel
->marked
)
777 fname
= next_file (); /* next marked file */
779 fname
= selection (current_panel
)->fname
; /* single file */
781 if (mc_stat (fname
, sf_stat
) != 0)
782 { /* get status of file */
783 destroy_dlg (ch_dlg
);
786 ch_cmode
= sf_stat
->st_mode
;
795 switch (ch_dlg
->ret_value
)
803 if (mc_chmod (fname
, get_mode ()) == -1)
804 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
805 fname
, unix_error_string (errno
));
806 /* call mc_chown only, if mc_chmod didn't fail */
807 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: (uid_t
) - 1,
808 (ch_flags
[10] == '+') ? sf_stat
->st_gid
: (gid_t
) - 1) == -1)
809 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
810 fname
, unix_error_string (errno
));
813 apply_advanced_chowns (sf_stat
);
821 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
)
823 do_file_mark (current_panel
, current_file
, 0);
826 destroy_dlg (ch_dlg
);
828 while (current_panel
->marked
&& !end_chown
);
830 chown_advanced_done ();
833 /* --------------------------------------------------------------------------------------------- */