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/vfs/mc-vfs/vfs.h"
42 #include "lib/strutil.h"
46 #include "wtools.h" /* For init_box_colors() */
49 #include "panel.h" /* Needed for the externs */
51 #include "main.h" /* update_panels() */
52 #include "layout.h" /* repaint_screen() */
63 #define B_SETALL B_USER
64 #define B_SKIP (B_USER + 1)
66 #define B_OWN (B_USER + 3)
67 #define B_GRP (B_USER + 4)
68 #define B_OTH (B_USER + 5)
69 #define B_OUSER (B_USER + 6)
70 #define B_OGROUP (B_USER + 7)
72 static struct Dlg_head
*ch_dlg
;
76 int ret_cmd
, flags
, y
, x
;
78 } chown_advanced_but
[BUTTONS
] =
81 { B_CANCEL
, NORMAL_BUTTON
, 4, 53, N_("&Cancel") },
82 { B_ENTER
, DEFPUSH_BUTTON
,4, 40, N_("&Set") },
83 { B_SKIP
, NORMAL_BUTTON
, 4, 23, N_("S&kip") },
84 { B_SETALL
, NORMAL_BUTTON
, 4, 0, N_("Set &all")},
85 { B_ENTER
, NARROW_BUTTON
, 0, 47, ""},
86 { B_ENTER
, NARROW_BUTTON
, 0, 29, ""},
87 { B_ENTER
, NARROW_BUTTON
, 0, 19, " "},
88 { B_ENTER
, NARROW_BUTTON
, 0, 11, " "},
89 { B_ENTER
, NARROW_BUTTON
, 0, 3, " "}
93 static WButton
*b_att
[3]; /* permission */
94 static WButton
*b_user
, *b_group
; /* owner */
96 static int files_on_begin
; /* Number of files at startup */
99 static char ch_flags
[11];
100 static const char ch_perm
[] = "rwx";
101 static mode_t ch_cmode
;
102 static struct stat
*sf_stat
;
103 static int need_update
;
104 static int end_chown
;
105 static int current_file
;
106 static int single_set
;
110 update_ownership (void)
112 button_set_text (b_user
, get_owner (sf_stat
->st_uid
));
113 button_set_text (b_group
, get_group (sf_stat
->st_gid
));
118 inc_flag_pos (int f_pos
)
123 return MSG_NOT_HANDLED
;
126 if (!(flag_pos
% 3) || f_pos
> 2)
127 return MSG_NOT_HANDLED
;
132 dec_flag_pos (int f_pos
)
137 return MSG_NOT_HANDLED
;
140 if (!((flag_pos
+ 1) % 3) || f_pos
> 2)
141 return MSG_NOT_HANDLED
;
146 set_perm_by_flags (char *s
, int f_p
)
150 for (i
= 0; i
< 3; i
++)
152 if (ch_flags
[f_p
+ i
] == '+')
154 else if (ch_flags
[f_p
+ i
] == '-')
157 s
[i
] = (ch_cmode
& (1 << (8 - f_p
- i
))) ? ch_perm
[i
] : '-';
162 update_permissions (void)
164 set_perm_by_flags (b_att
[0]->text
.start
, 0);
165 set_perm_by_flags (b_att
[1]->text
.start
, 3);
166 set_perm_by_flags (b_att
[2]->text
.start
, 6);
170 get_perm (char *s
, int base
)
175 m
|= (s
[0] == '-') ? 0 :
176 ((s
[0] == '+') ? (mode_t
) (1 << (base
+ 2)) : (1 << (base
+ 2)) & ch_cmode
);
178 m
|= (s
[1] == '-') ? 0 :
179 ((s
[1] == '+') ? (mode_t
) (1 << (base
+ 1)) : (1 << (base
+ 1)) & ch_cmode
);
181 m
|= (s
[2] == '-') ? 0 : ((s
[2] == '+') ? (mode_t
) (1 << base
) : (1 << base
) & ch_cmode
);
191 m
= ch_cmode
^ (ch_cmode
& 0777);
192 m
|= get_perm (ch_flags
, 6);
193 m
|= get_perm (ch_flags
+ 3, 3);
194 m
|= get_perm (ch_flags
+ 6, 0);
204 tty_setcolor (COLOR_NORMAL
);
206 for (i
= 0; i
< 3; i
++)
208 dlg_move (ch_dlg
, BY
+ 1, 9 + i
);
209 tty_print_char (ch_flags
[i
]);
212 for (i
= 0; i
< 3; i
++)
214 dlg_move (ch_dlg
, BY
+ 1, 17 + i
);
215 tty_print_char (ch_flags
[i
+ 3]);
218 for (i
= 0; i
< 3; i
++)
220 dlg_move (ch_dlg
, BY
+ 1, 25 + i
);
221 tty_print_char (ch_flags
[i
+ 6]);
224 update_permissions ();
226 for (i
= 0; i
< 15; i
++)
228 dlg_move (ch_dlg
, BY
+ 1, 35 + i
);
229 tty_print_char (ch_flags
[9]);
231 for (i
= 0; i
< 15; i
++)
233 dlg_move (ch_dlg
, BY
+ 1, 53 + i
);
234 tty_print_char (ch_flags
[10]);
239 update_mode (Dlg_head
* h
)
242 tty_setcolor (COLOR_NORMAL
);
243 dlg_move (h
, BY
+ 2, 9);
244 tty_printf ("%12o", get_mode ());
245 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
249 chl_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
263 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
268 do_enter_key (Dlg_head
* h
, int f_pos
)
272 struct passwd
*chl_pass
;
273 struct group
*chl_grp
;
275 int lxx
, lyy
, chl_end
, b_pos
;
281 is_owner
= (f_pos
== 3);
282 title
= is_owner
? _("owner") : _("group");
284 lxx
= (COLS
- 74) / 2 + (is_owner
? 35 : 53);
285 lyy
= (LINES
- 13) / 2;
289 create_dlg (TRUE
, lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
290 "[Advanced Chown]", title
, DLG_COMPACT
| DLG_REVERSE
);
292 /* get new listboxes */
293 chl_list
= listbox_new (1, 1, 11, 15, FALSE
, NULL
);
295 listbox_add_item (chl_list
, LISTBOX_APPEND_AT_END
, 0, "<Unknown>", NULL
);
299 /* get and put user names in the listbox */
301 while ((chl_pass
= getpwent ()))
303 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0, chl_pass
->pw_name
, NULL
);
306 fe
= listbox_search_text (chl_list
, get_owner (sf_stat
->st_uid
));
310 /* get and put group names in the listbox */
312 while ((chl_grp
= getgrent ()))
314 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0, chl_grp
->gr_name
, NULL
);
317 fe
= listbox_search_text (chl_list
, get_group (sf_stat
->st_gid
));
320 listbox_select_entry (chl_list
, fe
);
322 b_pos
= chl_list
->pos
;
323 add_widget (chl_dlg
, chl_list
);
327 if (b_pos
!= chl_list
->pos
)
332 listbox_get_current (chl_list
, &text
, NULL
);
335 chl_pass
= getpwnam (text
);
339 sf_stat
->st_uid
= chl_pass
->pw_uid
;
344 chl_grp
= getgrnam (text
);
347 sf_stat
->st_gid
= chl_grp
->gr_gid
;
353 ch_flags
[f_pos
+ 6] = '+';
360 if (chl_dlg
->ret_value
== KEY_LEFT
)
367 else if (chl_dlg
->ret_value
== KEY_RIGHT
)
371 dlg_one_down (ch_dlg
);
374 /* Here we used to redraw the window */
375 destroy_dlg (chl_dlg
);
383 common_dialog_repaint (ch_dlg
);
385 tty_setcolor (COLOR_NORMAL
);
387 dlg_move (ch_dlg
, BY
- 1, 8);
388 tty_print_string (_("owner"));
389 dlg_move (ch_dlg
, BY
- 1, 16);
390 tty_print_string (_("group"));
391 dlg_move (ch_dlg
, BY
- 1, 24);
392 tty_print_string (_("other"));
394 dlg_move (ch_dlg
, BY
- 1, 35);
395 tty_print_string (_("owner"));
396 dlg_move (ch_dlg
, BY
- 1, 53);
397 tty_print_string (_("group"));
399 dlg_move (ch_dlg
, 3, 4);
400 tty_print_string (_("On"));
401 dlg_move (ch_dlg
, BY
+ 1, 4);
402 tty_print_string (_("Flag"));
403 dlg_move (ch_dlg
, BY
+ 2, 4);
404 tty_print_string (_("Mode"));
408 dlg_move (ch_dlg
, 3, 54);
409 tty_printf (_("%6d of %d"), files_on_begin
- (current_panel
->marked
) + 1, files_on_begin
);
416 chown_info_update (void)
418 /* display file info */
419 tty_setcolor (COLOR_NORMAL
);
422 dlg_move (ch_dlg
, 3, 8);
423 tty_print_string (str_fit_to_term (fname
, 45, J_LEFT_FIT
));
424 dlg_move (ch_dlg
, BY
+ 2, 9);
425 tty_printf ("%12o", get_mode ());
428 update_permissions ();
434 b_att
[0]->hotpos
= -1;
435 b_att
[1]->hotpos
= -1;
436 b_att
[2]->hotpos
= -1;
437 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
441 advanced_chown_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
443 int i
= 0, f_pos
= BUTTONS
- dlg_get_current_widget_id (h
) - single_set
- 1;
449 chown_info_update ();
460 if ((flag_pos
/ 3) != f_pos
)
461 flag_pos
= f_pos
* 3;
465 flag_pos
= f_pos
+ 6;
475 return (dec_flag_pos (f_pos
));
481 return (inc_flag_pos (f_pos
));
491 if (f_pos
<= 2 || f_pos
>= 5)
493 do_enter_key (h
, f_pos
);
504 for (i
= 0; i
< 3; i
++)
505 ch_flags
[i
* 3 + parm
- 3] = (x_toggle
& (1 << parm
)) ? '-' : '+';
506 x_toggle
^= (1 << parm
);
508 dlg_broadcast_msg (h
, WIDGET_DRAW
, FALSE
);
509 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
520 for (i
= 0; i
< 3; i
++)
521 ch_flags
[i
* 3 + parm
] = (x_toggle
& (1 << parm
)) ? '-' : '+';
522 x_toggle
^= (1 << parm
);
524 dlg_broadcast_msg (h
, WIDGET_DRAW
, FALSE
);
525 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
537 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,parm)-ch_perm); */
538 if (((WButton
*) h
->current
->data
)->text
.start
[(flag_pos
% 3)] == '-')
539 ch_flags
[flag_pos
] = '+';
541 ch_flags
[flag_pos
] = '-';
554 flag_pos
= i
+ f_pos
* 3;
555 ch_flags
[flag_pos
] = '=';
571 ch_flags
[flag_pos
] = parm
;
573 advanced_chown_callback (h
, sender
, DLG_KEY
, KEY_RIGHT
, NULL
);
574 if (flag_pos
> 8 || !(flag_pos
% 3))
579 return MSG_NOT_HANDLED
;
582 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
587 init_chown_advanced (void)
591 { dlg_h
= 13, dlg_w
= 74, n_elem
= 4 };
593 static int i18n_len
= 0;
598 for (i
= 0; i
< n_elem
; i
++)
600 chown_advanced_but
[i
].text
= _(chown_advanced_but
[i
].text
);
601 i18n_len
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3;
602 if (DEFPUSH_BUTTON
== chown_advanced_but
[i
].flags
)
603 i18n_len
+= 2; /* "<>" */
605 cx
= dx
= (dlg_w
- i18n_len
- 2) / (n_elem
+ 1);
608 for (i
= n_elem
- 1; i
>= 0; i
--)
610 chown_advanced_but
[i
].x
= cx
;
611 cx
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3 + dx
;
614 #endif /* ENABLE_NLS */
616 sf_stat
= g_new (struct stat
, 1);
618 end_chown
= need_update
= current_file
= 0;
619 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
620 memset (ch_flags
, '=', 11);
625 create_dlg (TRUE
, 0, 0, dlg_h
, dlg_w
, dialog_colors
, advanced_chown_callback
,
626 "[Advanced Chown]", _("Chown advanced command"), DLG_CENTER
| DLG_REVERSE
);
628 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
629 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
630 (chown_advanced_but[i].text), 0
632 for (i
= 0; i
< BUTTONS
- 5; i
++)
633 if (!single_set
|| i
< 2)
634 add_widget (ch_dlg
, button_new (XTRACT (i
)));
636 b_att
[0] = button_new (XTRACT (8));
637 b_att
[1] = button_new (XTRACT (7));
638 b_att
[2] = button_new (XTRACT (6));
639 b_user
= button_new (XTRACT (5));
640 b_group
= button_new (XTRACT (4));
642 add_widget (ch_dlg
, b_group
);
643 add_widget (ch_dlg
, b_user
);
644 add_widget (ch_dlg
, b_att
[2]);
645 add_widget (ch_dlg
, b_att
[1]);
646 add_widget (ch_dlg
, b_att
[0]);
650 chown_advanced_done (void)
654 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
660 do_chown (uid_t u
, gid_t g
)
662 chown (current_panel
->dir
.list
[current_file
].fname
, u
, g
);
663 file_mark (current_panel
, current_file
, 0);
670 while (!current_panel
->dir
.list
[current_file
].f
.marked
)
673 return current_panel
->dir
.list
[current_file
].fname
;
677 apply_advanced_chowns (struct stat
*sf
)
680 gid_t a_gid
= sf
->st_gid
;
681 uid_t a_uid
= sf
->st_uid
;
683 lc_fname
= current_panel
->dir
.list
[current_file
].fname
;
684 need_update
= end_chown
= 1;
685 if (mc_chmod (lc_fname
, get_mode ()) == -1)
686 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
687 lc_fname
, unix_error_string (errno
));
688 /* call mc_chown only, if mc_chmod didn't fail */
689 else if (mc_chown (lc_fname
, (ch_flags
[9] == '+') ? sf
->st_uid
: (uid_t
) - 1,
690 (ch_flags
[10] == '+') ? sf
->st_gid
: (gid_t
) - 1) == -1)
691 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
692 lc_fname
, unix_error_string (errno
));
693 do_file_mark (current_panel
, current_file
, 0);
697 lc_fname
= next_file ();
699 if (mc_stat (lc_fname
, sf
) != 0)
701 ch_cmode
= sf
->st_mode
;
702 if (mc_chmod (lc_fname
, get_mode ()) == -1)
703 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
704 lc_fname
, unix_error_string (errno
));
705 /* call mc_chown only, if mc_chmod didn't fail */
706 else if (mc_chown (lc_fname
, (ch_flags
[9] == '+') ? a_uid
: (uid_t
) - 1,
707 (ch_flags
[10] == '+') ? a_gid
: (gid_t
) - 1) == -1)
708 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
709 lc_fname
, unix_error_string (errno
));
711 do_file_mark (current_panel
, current_file
, 0);
713 while (current_panel
->marked
);
717 chown_advanced_cmd (void)
720 files_on_begin
= current_panel
->marked
;
723 { /* do while any files remaining */
724 init_chown_advanced ();
726 if (current_panel
->marked
)
727 fname
= next_file (); /* next marked file */
729 fname
= selection (current_panel
)->fname
; /* single file */
731 if (mc_stat (fname
, sf_stat
) != 0)
732 { /* get status of file */
733 destroy_dlg (ch_dlg
);
736 ch_cmode
= sf_stat
->st_mode
;
745 switch (ch_dlg
->ret_value
)
753 if (mc_chmod (fname
, get_mode ()) == -1)
754 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
755 fname
, unix_error_string (errno
));
756 /* call mc_chown only, if mc_chmod didn't fail */
757 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: (uid_t
) - 1,
758 (ch_flags
[10] == '+') ? sf_stat
->st_gid
: (gid_t
) - 1) == -1)
759 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
760 fname
, unix_error_string (errno
));
763 apply_advanced_chowns (sf_stat
);
771 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
)
773 do_file_mark (current_panel
, current_file
, 0);
776 destroy_dlg (ch_dlg
);
778 while (current_panel
->marked
&& !end_chown
);
780 chown_advanced_done ();