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>
40 #include "wtools.h" /* For init_box_colors() */
41 #include "key.h" /* XCTRL and ALT macros */
45 #include "panel.h" /* Needed for the externs */
58 #define B_SETALL B_USER
59 #define B_SKIP (B_USER + 1)
61 #define B_OWN (B_USER + 3)
62 #define B_GRP (B_USER + 4)
63 #define B_OTH (B_USER + 5)
64 #define B_OUSER (B_USER + 6)
65 #define B_OGROUP (B_USER + 7)
67 static struct Dlg_head
*ch_dlg
;
70 int ret_cmd
, flags
, y
, x
;
72 } chown_advanced_but
[BUTTONS
] = {
73 { B_CANCEL
, NORMAL_BUTTON
, 4, 53, N_("&Cancel") },
74 { B_ENTER
, DEFPUSH_BUTTON
,4, 40, N_("&Set") },
75 { B_SKIP
, NORMAL_BUTTON
, 4, 23, N_("S&kip") },
76 { B_SETALL
, NORMAL_BUTTON
, 4, 0, N_("Set &all")},
77 { B_ENTER
, NARROW_BUTTON
, 0, 47, ""},
78 { B_ENTER
, NARROW_BUTTON
, 0, 29, ""},
79 { B_ENTER
, NARROW_BUTTON
, 0, 19, " "},
80 { B_ENTER
, NARROW_BUTTON
, 0, 11, " "},
81 { B_ENTER
, NARROW_BUTTON
, 0, 3, " "}
84 static WButton
*b_att
[3]; /* permission */
85 static WButton
*b_user
, *b_group
; /* owner */
87 static int files_on_begin
; /* Number of files at startup */
90 static char ch_flags
[11];
91 static const char ch_perm
[] = "rwx";
92 static mode_t ch_cmode
;
93 static struct stat
*sf_stat
;
94 static int need_update
;
96 static int current_file
;
97 static int single_set
;
100 static void update_ownership (void)
102 button_set_text (b_user
, get_owner (sf_stat
->st_uid
));
103 button_set_text (b_group
, get_group (sf_stat
->st_gid
));
107 static cb_ret_t
inc_flag_pos (int f_pos
)
109 if (flag_pos
== 10) {
111 return MSG_NOT_HANDLED
;
114 if (!(flag_pos
% 3) || f_pos
> 2)
115 return MSG_NOT_HANDLED
;
119 static cb_ret_t
dec_flag_pos (int f_pos
)
123 return MSG_NOT_HANDLED
;
126 if (!((flag_pos
+ 1) % 3) || f_pos
> 2)
127 return MSG_NOT_HANDLED
;
131 static void set_perm_by_flags (char *s
, int f_p
)
135 for (i
= 0; i
< 3; i
++) {
136 if (ch_flags
[f_p
+ i
] == '+')
138 else if (ch_flags
[f_p
+ i
] == '-')
141 s
[i
] = (ch_cmode
& (1 << (8 - f_p
- i
))) ? ch_perm
[i
] : '-';
145 static void update_permissions (void)
147 set_perm_by_flags (b_att
[0]->text
.start
, 0);
148 set_perm_by_flags (b_att
[1]->text
.start
, 3);
149 set_perm_by_flags (b_att
[2]->text
.start
, 6);
152 static mode_t
get_perm (char *s
, int base
)
157 m
|= (s
[0] == '-') ? 0 :
158 ((s
[0] == '+') ? (mode_t
)(1 << (base
+ 2)) : (1 << (base
+ 2)) & ch_cmode
);
160 m
|= (s
[1] == '-') ? 0 :
161 ((s
[1] == '+') ? (mode_t
)(1 << (base
+ 1)) : (1 << (base
+ 1)) & ch_cmode
);
163 m
|= (s
[2] == '-') ? 0 :
164 ((s
[2] == '+') ? (mode_t
)(1 << base
) : (1 << base
) & ch_cmode
);
169 static mode_t
get_mode (void)
173 m
= ch_cmode
^ (ch_cmode
& 0777);
174 m
|= get_perm (ch_flags
, 6);
175 m
|= get_perm (ch_flags
+ 3, 3);
176 m
|= get_perm (ch_flags
+ 6, 0);
181 static void print_flags (void)
185 attrset (COLOR_NORMAL
);
187 for (i
= 0; i
< 3; i
++){
188 dlg_move (ch_dlg
, BY
+1, 9+i
);
189 addch (ch_flags
[i
]);
192 for (i
= 0; i
< 3; i
++){
193 dlg_move (ch_dlg
, BY
+ 1, 17 + i
);
194 addch (ch_flags
[i
+3]);
197 for (i
= 0; i
< 3; i
++){
198 dlg_move (ch_dlg
, BY
+ 1, 25 + i
);
199 addch (ch_flags
[i
+6]);
202 update_permissions ();
204 for (i
= 0; i
< 15; i
++){
205 dlg_move (ch_dlg
, BY
+1, 35+i
);
208 for (i
= 0; i
< 15; i
++){
209 dlg_move (ch_dlg
, BY
+ 1, 53 + i
);
210 addch (ch_flags
[10]);
214 static void update_mode (Dlg_head
* h
)
217 attrset (COLOR_NORMAL
);
218 dlg_move (h
, BY
+ 2, 9);
219 tty_printf ("%12o", get_mode ());
220 send_message (h
->current
, WIDGET_FOCUS
, 0);
224 chl_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
236 return default_dlg_callback (h
, msg
, parm
);
241 do_enter_key (Dlg_head
* h
, int f_pos
)
245 struct passwd
*chl_pass
;
246 struct group
*chl_grp
;
248 int lxx
, lyy
, chl_end
, b_pos
;
253 is_owner
= (f_pos
== 3);
254 title
= is_owner
? _("owner") : _("group");
256 lxx
= (COLS
- 74) / 2 + (is_owner
? 35 : 53);
257 lyy
= (LINES
- 13) / 2;
261 create_dlg (lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
262 "[Advanced Chown]", title
, DLG_COMPACT
| DLG_REVERSE
);
264 /* get new listboxes */
265 chl_list
= listbox_new (1, 1, 11, 15, NULL
);
267 listbox_add_item (chl_list
, LISTBOX_APPEND_AT_END
, 0,
271 /* get and put user names in the listbox */
273 while ((chl_pass
= getpwent ())) {
274 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0,
275 chl_pass
->pw_name
, NULL
);
278 fe
= listbox_search_text (chl_list
,
279 get_owner (sf_stat
->st_uid
));
281 /* get and put group names in the listbox */
283 while ((chl_grp
= getgrent ())) {
284 listbox_add_item (chl_list
, LISTBOX_APPEND_SORTED
, 0,
285 chl_grp
->gr_name
, NULL
);
288 fe
= listbox_search_text (chl_list
,
289 get_group (sf_stat
->st_gid
));
293 listbox_select_entry (chl_list
, fe
);
295 b_pos
= chl_list
->pos
;
296 add_widget (chl_dlg
, chl_list
);
300 if (b_pos
!= chl_list
->pos
) {
303 chl_pass
= getpwnam (chl_list
->current
->text
);
306 sf_stat
->st_uid
= chl_pass
->pw_uid
;
309 chl_grp
= getgrnam (chl_list
->current
->text
);
311 sf_stat
->st_gid
= chl_grp
->gr_gid
;
316 ch_flags
[f_pos
+ 6] = '+';
323 if (chl_dlg
->ret_value
== KEY_LEFT
) {
328 } else if (chl_dlg
->ret_value
== KEY_RIGHT
) {
331 dlg_one_down (ch_dlg
);
334 /* Here we used to redraw the window */
335 destroy_dlg (chl_dlg
);
339 static void chown_refresh (void)
341 common_dialog_repaint (ch_dlg
);
343 attrset (COLOR_NORMAL
);
345 dlg_move (ch_dlg
, BY
- 1, 8);
347 dlg_move (ch_dlg
, BY
- 1, 16);
349 dlg_move (ch_dlg
, BY
- 1, 24);
352 dlg_move (ch_dlg
, BY
- 1, 35);
354 dlg_move (ch_dlg
, BY
- 1, 53);
357 dlg_move (ch_dlg
, 3, 4);
359 dlg_move (ch_dlg
, BY
+ 1, 4);
361 dlg_move (ch_dlg
, BY
+ 2, 4);
365 dlg_move (ch_dlg
, 3, 54);
366 tty_printf (_("%6d of %d"),
367 files_on_begin
- (current_panel
->marked
) + 1,
374 static void chown_info_update (void)
376 /* display file info */
377 attrset (COLOR_NORMAL
);
380 dlg_move (ch_dlg
, 3, 8);
381 addstr (str_fit_to_term (fname
, 45, J_LEFT_FIT
));
382 dlg_move (ch_dlg
, BY
+ 2, 9);
383 tty_printf ("%12o", get_mode ());
386 update_permissions ();
389 static void b_setpos (int f_pos
) {
393 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
397 advanced_chown_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
399 int i
= 0, f_pos
= BUTTONS
- h
->current
->dlg_id
- single_set
- 1;
404 chown_info_update ();
414 if ((flag_pos
/ 3) != f_pos
)
415 flag_pos
= f_pos
* 3;
417 } else if (f_pos
< 5)
418 flag_pos
= f_pos
+ 6;
427 return (dec_flag_pos (f_pos
));
433 return (inc_flag_pos (f_pos
));
443 if (f_pos
<= 2 || f_pos
>= 5)
445 do_enter_key (h
, f_pos
);
456 for (i
= 0; i
< 3; i
++)
457 ch_flags
[i
* 3 + parm
- 3] =
458 (x_toggle
& (1 << parm
)) ? '-' : '+';
459 x_toggle
^= (1 << parm
);
461 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
462 send_message (h
->current
, WIDGET_FOCUS
, 0);
473 for (i
= 0; i
< 3; i
++)
474 ch_flags
[i
* 3 + parm
] =
475 (x_toggle
& (1 << parm
)) ? '-' : '+';
476 x_toggle
^= (1 << parm
);
478 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
479 send_message (h
->current
, WIDGET_FOCUS
, 0);
491 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,parm)-ch_perm); */
492 if (((WButton
*) h
->current
)->text
.start
[(flag_pos
% 3)] ==
494 ch_flags
[flag_pos
] = '+';
496 ch_flags
[flag_pos
] = '-';
509 flag_pos
= i
+ f_pos
* 3;
510 ch_flags
[flag_pos
] = '=';
526 ch_flags
[flag_pos
] = parm
;
528 advanced_chown_callback (h
, DLG_KEY
, KEY_RIGHT
);
529 if (flag_pos
> 8 || !(flag_pos
% 3))
534 return MSG_NOT_HANDLED
;
537 return default_dlg_callback (h
, msg
, parm
);
542 init_chown_advanced (void)
545 enum { dlg_h
= 13, dlg_w
= 74, n_elem
= 4 };
547 static int i18n_len
= 0;
551 for (i
= 0 ; i
< n_elem
; i
++) {
552 chown_advanced_but
[i
].text
= _(chown_advanced_but
[i
].text
);
553 i18n_len
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3;
554 if (DEFPUSH_BUTTON
== chown_advanced_but
[i
].flags
)
555 i18n_len
+= 2; /* "<>" */
557 cx
= dx
= (dlg_w
- i18n_len
- 2) / (n_elem
+ 1);
560 for (i
= n_elem
- 1; i
>= 0; i
--) {
561 chown_advanced_but
[i
].x
= cx
;
562 cx
+= str_term_width1 (chown_advanced_but
[i
].text
) + 3 + dx
;
565 #endif /* ENABLE_NLS */
567 sf_stat
= g_new (struct stat
, 1);
569 end_chown
= need_update
= current_file
= 0;
570 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
571 memset (ch_flags
, '=', 11);
576 create_dlg (0, 0, dlg_h
, dlg_w
, dialog_colors
, advanced_chown_callback
,
577 "[Advanced Chown]", _(" Chown advanced command "),
578 DLG_CENTER
| DLG_REVERSE
);
580 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
581 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
582 (chown_advanced_but[i].text), 0
584 for (i
= 0; i
< BUTTONS
- 5; i
++)
585 if (!single_set
|| i
< 2)
586 add_widget (ch_dlg
, button_new (XTRACT (i
)));
588 b_att
[0] = button_new (XTRACT (8));
589 b_att
[1] = button_new (XTRACT (7));
590 b_att
[2] = button_new (XTRACT (6));
591 b_user
= button_new (XTRACT (5));
592 b_group
= button_new (XTRACT (4));
594 add_widget (ch_dlg
, b_group
);
595 add_widget (ch_dlg
, b_user
);
596 add_widget (ch_dlg
, b_att
[2]);
597 add_widget (ch_dlg
, b_att
[1]);
598 add_widget (ch_dlg
, b_att
[0]);
602 chown_advanced_done (void)
606 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
611 static inline void do_chown (uid_t u
, gid_t g
)
613 chown (current_panel
->dir
.list
[current_file
].fname
, u
, g
);
614 file_mark (current_panel
, current_file
, 0);
618 static char *next_file (void)
620 while (!current_panel
->dir
.list
[current_file
].f
.marked
)
623 return current_panel
->dir
.list
[current_file
].fname
;
626 static void apply_advanced_chowns (struct stat
*sf
)
629 gid_t a_gid
= sf
->st_gid
;
630 uid_t a_uid
= sf
->st_uid
;
632 fname
= current_panel
->dir
.list
[current_file
].fname
;
633 need_update
= end_chown
= 1;
634 if (mc_chmod (fname
, get_mode ()) == -1)
635 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
636 fname
, unix_error_string (errno
));
637 /* call mc_chown only, if mc_chmod didn't fail */
638 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf
->st_uid
: (uid_t
) -1,
639 (ch_flags
[10] == '+') ? sf
->st_gid
: (gid_t
) -1) == -1)
640 message (D_ERROR
, MSG_ERROR
, _(" Cannot chown \"%s\" \n %s "),
641 fname
, unix_error_string (errno
));
642 do_file_mark (current_panel
, current_file
, 0);
645 fname
= next_file ();
647 if (mc_stat (fname
, sf
) != 0)
649 ch_cmode
= sf
->st_mode
;
650 if (mc_chmod (fname
, get_mode ()) == -1)
651 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
652 fname
, unix_error_string (errno
));
653 /* call mc_chown only, if mc_chmod didn't fail */
654 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? a_uid
: (uid_t
) -1,
655 (ch_flags
[10] == '+') ? a_gid
: (gid_t
) -1) == -1)
656 message (D_ERROR
, MSG_ERROR
, _(" Cannot chown \"%s\" \n %s "),
657 fname
, unix_error_string (errno
));
659 do_file_mark (current_panel
, current_file
, 0);
660 } while (current_panel
->marked
);
664 chown_advanced_cmd (void)
667 files_on_begin
= current_panel
->marked
;
669 do { /* do while any files remaining */
670 init_chown_advanced ();
672 if (current_panel
->marked
)
673 fname
= next_file (); /* next marked file */
675 fname
= selection (current_panel
)->fname
; /* single file */
677 if (mc_stat (fname
, sf_stat
) != 0) { /* get status of file */
678 destroy_dlg (ch_dlg
);
681 ch_cmode
= sf_stat
->st_mode
;
690 switch (ch_dlg
->ret_value
) {
697 if (mc_chmod (fname
, get_mode ()) == -1)
698 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
699 fname
, unix_error_string (errno
));
700 /* call mc_chown only, if mc_chmod didn't fail */
701 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: (uid_t
) -1,
702 (ch_flags
[10] == '+') ? sf_stat
->st_gid
: (gid_t
) -1) == -1)
703 message (D_ERROR
, MSG_ERROR
, _(" Cannot chown \"%s\" \n %s "),
704 fname
, unix_error_string (errno
));
707 apply_advanced_chowns (sf_stat
);
715 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
) {
716 do_file_mark (current_panel
, current_file
, 0);
719 destroy_dlg (ch_dlg
);
720 } while (current_panel
->marked
&& !end_chown
);
722 chown_advanced_done ();