2 Chown-advanced command -- for the Midnight Commander
4 Copyright (C) 1994-2016
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: Contains functions for advanced chowning
33 #include <sys/types.h>
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/tty/key.h" /* XCTRL and ALT macros */
44 #include "lib/strutil.h"
45 #include "lib/vfs/vfs.h"
47 #include "lib/widget.h"
50 #include "midnight.h" /* current_panel */
55 /*** global variables ****************************************************************************/
57 /*** file scope macro definitions ****************************************************************/
63 #define BUTTONS_PERM 5
65 #define B_SETALL B_USER
66 #define B_SKIP (B_USER + 1)
68 /*** file scope type declarations ****************************************************************/
70 /*** file scope variables ************************************************************************/
72 static WDialog
*ch_dlg
;
81 } chown_advanced_but
[BUTTONS
] =
84 { 0, B_ENTER
, NARROW_BUTTON
, 3, 0, " "},
85 { 0, B_ENTER
, NARROW_BUTTON
, 11, 0, " "},
86 { 0, B_ENTER
, NARROW_BUTTON
, 19, 0, " "},
87 { 0, B_ENTER
, NARROW_BUTTON
, 29, 0, ""},
88 { 0, B_ENTER
, NARROW_BUTTON
, 47, 0, ""},
90 { 0, B_SETALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all")},
91 { 0, B_SKIP
, NORMAL_BUTTON
, 0, 0, N_("S&kip") },
92 { 0, B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("&Set") },
93 { 0, B_CANCEL
, NORMAL_BUTTON
, 0, 0, N_("&Cancel") }
97 static WButton
*b_att
[3]; /* permission */
98 static WButton
*b_user
, *b_group
; /* owner */
99 static WLabel
*l_filename
;
100 static WLabel
*l_mode
;
104 static char ch_flags
[11];
105 static const char ch_perm
[] = "rwx";
106 static mode_t ch_cmode
;
107 static struct stat
*sf_stat
;
108 static gboolean need_update
= FALSE
;
109 static gboolean end_chown
= FALSE
;
110 static int current_file
;
111 static gboolean single_set
= FALSE
;
114 /* --------------------------------------------------------------------------------------------- */
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) == 0 || 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) == 0 || 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 widget_move (ch_dlg
, BY
+ 1, 9 + i
);
231 tty_print_char (ch_flags
[i
]);
234 for (i
= 0; i
< 3; i
++)
236 widget_move (ch_dlg
, BY
+ 1, 17 + i
);
237 tty_print_char (ch_flags
[i
+ 3]);
240 for (i
= 0; i
< 3; i
++)
242 widget_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 widget_move (ch_dlg
, BY
+ 1, 35 + i
);
251 tty_print_char (ch_flags
[9]);
253 for (i
= 0; i
< 15; i
++)
255 widget_move (ch_dlg
, BY
+ 1, 53 + i
);
256 tty_print_char (ch_flags
[10]);
260 /* --------------------------------------------------------------------------------------------- */
263 chown_info_update (void)
265 char buffer
[BUF_SMALL
];
268 g_snprintf (buffer
, sizeof (buffer
), "Permissions (octal): %o", get_mode ());
269 label_set_text (l_mode
, buffer
);
272 update_permissions ();
275 /* --------------------------------------------------------------------------------------------- */
278 update_mode (WDialog
* h
)
281 chown_info_update ();
282 widget_set_state (WIDGET (h
->current
->data
), WST_FOCUSED
, TRUE
);
285 /* --------------------------------------------------------------------------------------------- */
288 chl_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
298 WDialog
*h
= DIALOG (w
);
308 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
312 /* --------------------------------------------------------------------------------------------- */
315 do_enter_key (WDialog
* h
, int f_pos
)
318 struct passwd
*chl_pass
;
319 struct group
*chl_grp
;
321 gboolean chl_end
, is_owner
;
330 is_owner
= (f_pos
== 3);
331 title
= is_owner
? _("owner") : _("group");
333 lxx
= (COLS
- 74) / 2 + (is_owner
? 35 : 53);
334 lyy
= (LINES
- 13) / 2;
338 dlg_create (TRUE
, lyy
, lxx
, 13, 17, WPOS_KEEP_DEFAULT
, TRUE
, dialog_colors
,
339 chl_callback
, NULL
, "[Advanced Chown]", title
);
341 /* get new listboxes */
342 chl_list
= listbox_new (1, 1, 11, 15, FALSE
, NULL
);
343 listbox_add_item (chl_list
, LISTBOX_APPEND_AT_END
, 0, "<Unknown>", NULL
, FALSE
);
346 /* get and put user names in the listbox */
348 while ((chl_pass
= getpwent ()) != NULL
)
349 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0, chl_pass
->pw_name
, NULL
,
352 fe
= listbox_search_text (chl_list
, get_owner (sf_stat
->st_uid
));
356 /* get and put group names in the listbox */
358 while ((chl_grp
= getgrent ()) != NULL
)
359 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0, chl_grp
->gr_name
, NULL
,
362 fe
= listbox_search_text (chl_list
, get_group (sf_stat
->st_gid
));
365 listbox_select_entry (chl_list
, fe
);
367 b_pos
= chl_list
->pos
;
368 add_widget (chl_dlg
, chl_list
);
370 result
= dlg_run (chl_dlg
);
372 if (result
!= B_CANCEL
)
374 if (b_pos
!= chl_list
->pos
)
379 listbox_get_current (chl_list
, &text
, NULL
);
382 chl_pass
= getpwnam (text
);
383 if (chl_pass
!= NULL
)
386 sf_stat
->st_uid
= chl_pass
->pw_uid
;
391 chl_grp
= getgrnam (text
);
394 sf_stat
->st_gid
= chl_grp
->gr_gid
;
400 ch_flags
[f_pos
+ 6] = '+';
407 if (result
== KEY_LEFT
)
414 else if (result
== KEY_RIGHT
)
418 dlg_one_down (ch_dlg
);
423 /* Here we used to redraw the window */
424 dlg_destroy (chl_dlg
);
429 /* --------------------------------------------------------------------------------------------- */
434 dlg_default_repaint (ch_dlg
);
436 tty_setcolor (COLOR_NORMAL
);
438 widget_move (ch_dlg
, BY
- 1, 8);
439 tty_print_string (_("owner"));
440 widget_move (ch_dlg
, BY
- 1, 16);
441 tty_print_string (_("group"));
442 widget_move (ch_dlg
, BY
- 1, 24);
443 tty_print_string (_("other"));
445 widget_move (ch_dlg
, BY
- 1, 35);
446 tty_print_string (_("owner"));
447 widget_move (ch_dlg
, BY
- 1, 53);
448 tty_print_string (_("group"));
450 widget_move (ch_dlg
, BY
+ 1, 3);
451 tty_print_string (_("Flag"));
456 /* --------------------------------------------------------------------------------------------- */
461 b_att
[0]->hotpos
= -1;
462 b_att
[1]->hotpos
= -1;
463 b_att
[2]->hotpos
= -1;
464 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
467 /* --------------------------------------------------------------------------------------------- */
470 advanced_chown_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
472 WDialog
*h
= DIALOG (w
);
477 id
= dlg_get_current_widget_id (h
);
479 for (i
= 0; i
< BUTTONS_PERM
; i
++)
480 if (chown_advanced_but
[i
].id
== id
)
490 chown_info_update ();
501 if ((flag_pos
/ 3) != f_pos
)
502 flag_pos
= f_pos
* 3;
505 else if (f_pos
< BUTTONS_PERM
)
506 flag_pos
= f_pos
+ 6;
514 if (f_pos
< BUTTONS_PERM
)
515 return (dec_flag_pos (f_pos
));
520 if (f_pos
< BUTTONS_PERM
)
521 return (inc_flag_pos (f_pos
));
531 if (f_pos
<= 2 || f_pos
>= BUTTONS_PERM
)
533 do_enter_key (h
, f_pos
);
544 for (i
= 0; i
< 3; i
++)
545 ch_flags
[i
* 3 + parm
- 3] = (x_toggle
& (1 << parm
)) ? '-' : '+';
546 x_toggle
^= (1 << parm
);
548 dlg_broadcast_msg (h
, MSG_DRAW
);
549 widget_set_state (WIDGET (h
->current
->data
), WST_FOCUSED
, TRUE
);
560 for (i
= 0; i
< 3; i
++)
561 ch_flags
[i
* 3 + parm
] = (x_toggle
& (1 << parm
)) ? '-' : '+';
562 x_toggle
^= (1 << parm
);
564 dlg_broadcast_msg (h
, MSG_DRAW
);
565 widget_set_state (WIDGET (h
->current
->data
), WST_FOCUSED
, TRUE
);
577 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,parm)-ch_perm); */
578 if (BUTTON (h
->current
->data
)->text
.start
[(flag_pos
% 3)] == '-')
579 ch_flags
[flag_pos
] = '+';
581 ch_flags
[flag_pos
] = '-';
594 flag_pos
= i
+ f_pos
* 3;
595 ch_flags
[flag_pos
] = '=';
612 ch_flags
[flag_pos
] = parm
;
614 send_message (h
, sender
, MSG_KEY
, KEY_RIGHT
, NULL
);
615 if (flag_pos
> 8 || (flag_pos
% 3) == 0)
623 return MSG_NOT_HANDLED
;
626 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
630 /* --------------------------------------------------------------------------------------------- */
633 init_chown_advanced (void)
640 static gboolean i18n
= FALSE
;
644 for (i
= BUTTONS_PERM
; i
< BUTTONS
; i
++)
647 chown_advanced_but
[i
].text
= _(chown_advanced_but
[i
].text
);
648 #endif /* ENABLE_NLS */
650 chown_advanced_but
[i
].len
= str_term_width1 (chown_advanced_but
[i
].text
) + 3;
651 if (chown_advanced_but
[i
].flags
== DEFPUSH_BUTTON
)
652 chown_advanced_but
[i
].len
+= 2; /* "<>" */
660 sf_stat
= g_new (struct stat
, 1);
662 end_chown
= need_update
= FALSE
;
663 single_set
= (current_panel
->marked
< 2);
664 memset (ch_flags
, '=', 11);
672 dlg_create (TRUE
, 0, 0, dlg_h
, dlg_w
, WPOS_CENTER
, FALSE
, dialog_colors
,
673 advanced_chown_callback
, NULL
, "[Advanced Chown]", _("Chown advanced command"));
676 l_filename
= label_new (2, 3, "");
677 add_widget (ch_dlg
, l_filename
);
679 add_widget (ch_dlg
, hline_new (3, -1, -1));
681 #define XTRACT(i,y) y, BX+chown_advanced_but[i].x, \
682 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
683 (chown_advanced_but[i].text), NULL
684 b_att
[0] = button_new (XTRACT (0, BY
));
685 chown_advanced_but
[0].id
= add_widget (ch_dlg
, b_att
[0]);
686 b_att
[1] = button_new (XTRACT (1, BY
));
687 chown_advanced_but
[1].id
= add_widget (ch_dlg
, b_att
[1]);
688 b_att
[2] = button_new (XTRACT (2, BY
));
689 chown_advanced_but
[2].id
= add_widget (ch_dlg
, b_att
[2]);
690 b_user
= button_new (XTRACT (3, BY
));
691 chown_advanced_but
[3].id
= add_widget (ch_dlg
, b_user
);
692 b_group
= button_new (XTRACT (4, BY
));
693 chown_advanced_but
[4].id
= add_widget (ch_dlg
, b_group
);
696 l_mode
= label_new (BY
+ 2, 3, "");
697 add_widget (ch_dlg
, l_mode
);
703 add_widget (ch_dlg
, hline_new (y
++, -1, -1));
704 chown_advanced_but
[i
].id
= add_widget (ch_dlg
,
706 WIDGET (ch_dlg
)->cols
/ 2 -
707 chown_advanced_but
[i
].len
,
708 chown_advanced_but
[i
].ret_cmd
,
709 chown_advanced_but
[i
].flags
,
710 chown_advanced_but
[i
].text
, NULL
));
712 chown_advanced_but
[i
].id
= add_widget (ch_dlg
,
713 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 + 1,
714 chown_advanced_but
[i
].ret_cmd
,
715 chown_advanced_but
[i
].flags
,
716 chown_advanced_but
[i
].text
, NULL
));
720 i
= BUTTONS_PERM
+ 2;
721 add_widget (ch_dlg
, hline_new (y
++, -1, -1));
722 chown_advanced_but
[i
].id
= add_widget (ch_dlg
,
724 WIDGET (ch_dlg
)->cols
/ 2 -
725 chown_advanced_but
[i
].len
,
726 chown_advanced_but
[i
].ret_cmd
,
727 chown_advanced_but
[i
].flags
,
728 chown_advanced_but
[i
].text
, NULL
));
730 chown_advanced_but
[i
].id
= add_widget (ch_dlg
,
731 button_new (y
, WIDGET (ch_dlg
)->cols
/ 2 + 1,
732 chown_advanced_but
[i
].ret_cmd
,
733 chown_advanced_but
[i
].flags
,
734 chown_advanced_but
[i
].text
, NULL
));
736 dlg_select_widget (b_att
[0]);
739 /* --------------------------------------------------------------------------------------------- */
742 chown_advanced_done (void)
746 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
750 /* --------------------------------------------------------------------------------------------- */
754 do_chown (uid_t u
, gid_t g
)
756 chown (current_panel
->dir
.list
[current_file
].fname
, u
, g
);
757 file_mark (current_panel
, current_file
, 0);
761 /* --------------------------------------------------------------------------------------------- */
766 while (!current_panel
->dir
.list
[current_file
].f
.marked
)
769 return current_panel
->dir
.list
[current_file
].fname
;
772 /* --------------------------------------------------------------------------------------------- */
775 apply_advanced_chowns (struct stat
*sf
)
779 gid_t a_gid
= sf
->st_gid
;
780 uid_t a_uid
= sf
->st_uid
;
782 lc_fname
= current_panel
->dir
.list
[current_file
].fname
;
783 vpath
= vfs_path_from_str (lc_fname
);
784 need_update
= end_chown
= TRUE
;
785 if (mc_chmod (vpath
, get_mode ()) == -1)
786 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
787 lc_fname
, unix_error_string (errno
));
788 /* call mc_chown only, if mc_chmod didn't fail */
789 else if (mc_chown (vpath
, (ch_flags
[9] == '+') ? sf
->st_uid
: (uid_t
) (-1),
790 (ch_flags
[10] == '+') ? sf
->st_gid
: (gid_t
) (-1)) == -1)
791 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
792 lc_fname
, unix_error_string (errno
));
793 do_file_mark (current_panel
, current_file
, 0);
794 vfs_path_free (vpath
);
798 lc_fname
= next_file ();
799 vpath
= vfs_path_from_str (lc_fname
);
801 if (mc_stat (vpath
, sf
) != 0)
803 vfs_path_free (vpath
);
807 ch_cmode
= sf
->st_mode
;
809 if (mc_chmod (vpath
, get_mode ()) == -1)
810 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
811 lc_fname
, unix_error_string (errno
));
812 /* call mc_chown only, if mc_chmod didn't fail */
813 else if (mc_chown (vpath
, (ch_flags
[9] == '+') ? a_uid
: (uid_t
) (-1),
814 (ch_flags
[10] == '+') ? a_gid
: (gid_t
) (-1)) == -1)
815 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
816 lc_fname
, unix_error_string (errno
));
818 do_file_mark (current_panel
, current_file
, 0);
819 vfs_path_free (vpath
);
821 while (current_panel
->marked
!= 0);
824 /* --------------------------------------------------------------------------------------------- */
825 /*** public functions ****************************************************************************/
826 /* --------------------------------------------------------------------------------------------- */
829 chown_advanced_cmd (void)
831 /* Number of files at startup */
834 files_on_begin
= MAX (1, current_panel
->marked
);
837 { /* do while any files remaining */
839 char buffer
[BUF_MEDIUM
];
843 init_chown_advanced ();
845 if (current_panel
->marked
)
846 fname
= next_file (); /* next marked file */
848 fname
= selection (current_panel
)->fname
; /* single file */
849 vpath
= vfs_path_from_str (fname
);
851 if (mc_stat (vpath
, sf_stat
) != 0)
852 { /* get status of file */
853 dlg_destroy (ch_dlg
);
854 vfs_path_free (vpath
);
858 ch_cmode
= sf_stat
->st_mode
;
860 file_idx
= files_on_begin
== 1 ? 1 : (files_on_begin
- current_panel
->marked
+ 1);
861 g_snprintf (buffer
, sizeof (buffer
), "%s (%d/%d)",
862 str_fit_to_term (fname
, WIDGET (ch_dlg
)->cols
- 20, J_LEFT_FIT
),
863 file_idx
, files_on_begin
);
864 label_set_text (l_filename
, buffer
);
868 result
= dlg_run (ch_dlg
);
878 if (mc_chmod (vpath
, get_mode ()) == -1)
879 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
880 fname
, unix_error_string (errno
));
881 /* call mc_chown only, if mc_chmod didn't fail */
883 (vpath
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: (uid_t
) (-1),
884 (ch_flags
[10] == '+') ? sf_stat
->st_gid
: (gid_t
) (-1)) == -1)
885 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"), fname
,
886 unix_error_string (errno
));
890 apply_advanced_chowns (sf_stat
);
898 if (current_panel
->marked
&& result
!= B_CANCEL
)
900 do_file_mark (current_panel
, current_file
, 0);
903 dlg_destroy (ch_dlg
);
904 vfs_path_free (vpath
);
906 while (current_panel
->marked
&& !end_chown
);
908 chown_advanced_done ();
911 /* --------------------------------------------------------------------------------------------- */