1 /* Chown-advanced command -- for the Midnight Commander
2 Copyright (C) 1994, 1995 Radek Doulik
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* Needed for the extern declarations of integer parameters */
21 #include <sys/types.h>
22 #include <sys/param.h>
31 #include <stdlib.h> /* For malloc() */
32 #include <errno.h> /* For errno on SunOS systems */
35 #include "util.h" /* Needed for the externs */
40 #include "dialog.h" /* For do_refresh() */
41 #include "wtools.h" /* For init_box_colors() */
42 #include "key.h" /* XCTRL and ALT macros */
45 #include "panel.h" /* Needed for the externs */
49 #include "../vfs/vfs.h"
59 #define B_SETALL B_USER
60 #define B_SKIP B_USER + 1
62 #define B_OWN B_USER + 3
63 #define B_GRP B_USER + 4
64 #define B_OTH B_USER + 5
65 #define B_OUSER B_USER + 6
66 #define B_OGROUP B_USER + 7
69 int ret_cmd
, flags
, y
, x
;
71 } chown_advanced_but
[BUTTONS
] = {
72 { B_CANCEL
, NORMAL_BUTTON
, 4, 55, N_("&Cancel") },
73 { B_ENTER
, DEFPUSH_BUTTON
,4, 45, N_("&Set") },
74 { B_SKIP
, NORMAL_BUTTON
, 4, 36, N_("S&kip") },
75 { B_SETALL
, NORMAL_BUTTON
, 4, 24, N_("Set &all")},
76 { B_ENTER
, NARROW_BUTTON
, 0, 47, " "},
77 { B_ENTER
, NARROW_BUTTON
, 0, 29, " "},
78 { B_ENTER
, NARROW_BUTTON
, 0, 19, " "},
79 { B_ENTER
, NARROW_BUTTON
, 0, 11, " "},
80 { B_ENTER
, NARROW_BUTTON
, 0, 3, " "},
83 WButton
*b_att
[3]; /* permission */
84 WButton
*b_user
, *b_group
; /* owner */
86 static int files_on_begin
; /* Number of files at startup */
89 static char ch_flags
[11];
90 static char *ch_perm
= "rwx";
91 static umode_t ch_cmode
;
93 static int need_update
;
95 static int current_file
;
96 static int single_set
;
99 static void get_ownership ()
100 { /* set buttons - ownership */
103 name_t
= name_trunc (get_owner (sf_stat
->st_uid
), 15);
104 memset (b_user
->text
, ' ', 15);
105 strncpy (b_user
->text
, name_t
, strlen (name_t
));
106 name_t
= name_trunc (get_group (sf_stat
->st_gid
), 15);
107 memset (b_group
->text
, ' ', 15);
108 strncpy (b_group
->text
, name_t
, strlen (name_t
));
112 static int inc_flag_pos (int f_pos
)
114 if (flag_pos
== 10) {
119 if (!(flag_pos
% 3) || f_pos
> 2)
124 static int dec_flag_pos (int f_pos
)
131 if (!((flag_pos
+ 1) % 3) || f_pos
> 2)
136 static void set_perm_by_flags (char *s
, int f_p
)
140 for (i
= 0; i
< 3; i
++)
141 if (ch_flags
[f_p
+ i
] == '+')
143 else if (ch_flags
[f_p
+ i
] == '-')
146 s
[i
] = (ch_cmode
& (1 << (8 - f_p
- i
))) ? ch_perm
[i
] : '-';
149 static void set_perm (char *s
, int p
)
151 s
[0] = (p
& 4) ? 'r' : '-';
152 s
[1] = (p
& 2) ? 'w' : '-';
153 s
[2] = (p
& 1) ? 'x' : '-';
156 static umode_t
get_perm (char *s
, int base
)
161 m
|= (s
[0] == '-') ? 0 :
162 ((s
[0] == '+') ? (1 << (base
+ 2)) : (1 << (base
+ 2)) & ch_cmode
);
164 m
|= (s
[1] == '-') ? 0 :
165 ((s
[1] == '+') ? (1 << (base
+ 1)) : (1 << (base
+ 1)) & ch_cmode
);
167 m
|= (s
[2] == '-') ? 0 :
168 ((s
[2] == '+') ? (1 << base
) : (1 << base
) & ch_cmode
);
173 static umode_t
get_mode ()
177 m
= ch_cmode
^ (ch_cmode
& 0777);
178 m
|= get_perm (ch_flags
, 6);
179 m
|= get_perm (ch_flags
+ 3, 3);
180 m
|= get_perm (ch_flags
+ 6, 0);
185 static void print_flags (void)
189 attrset (COLOR_NORMAL
);
191 for (i
= 0; i
< 3; i
++){
192 dlg_move (ch_dlg
, BY
+1, 9+i
);
193 addch (ch_flags
[i
]);
196 for (i
= 0; i
< 3; i
++){
197 dlg_move (ch_dlg
, BY
+ 1, 17 + i
);
198 addch (ch_flags
[i
+3]);
201 for (i
= 0; i
< 3; i
++){
202 dlg_move (ch_dlg
, BY
+ 1, 25 + i
);
203 addch (ch_flags
[i
+6]);
206 set_perm_by_flags (b_att
[0]->text
, 0);
207 set_perm_by_flags (b_att
[1]->text
, 3);
208 set_perm_by_flags (b_att
[2]->text
, 6);
210 for (i
= 0; i
< 15; i
++){
211 dlg_move (ch_dlg
, BY
+1, 35+i
);
214 for (i
= 0; i
< 15; i
++){
215 dlg_move (ch_dlg
, BY
+ 1, 53 + i
);
216 addch (ch_flags
[10]);
220 static void update_mode (Dlg_head
* h
)
223 attrset (COLOR_NORMAL
);
224 dlg_move (h
, BY
+ 2, 9);
225 printw ("%12o", get_mode ());
226 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
229 static int l_call (void *data
)
234 static int chl_callback (Dlg_head
* h
, int Par
, int Msg
)
238 attrset (COLOR_NORMAL
);
240 draw_box (h
, 0, 0, 13, 17);
254 static void do_enter_key (Dlg_head
*h
, int f_pos
)
258 struct passwd
*chl_pass
;
259 struct group
*chl_grp
;
261 int lxx
, lyy
, chl_end
, b_pos
;
264 lxx
= (COLS
- 74) / 2 + ((f_pos
== 3) ? 35 : 53);
265 lyy
= (LINES
- 13) / 2;
268 chl_dlg
= create_dlg (lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
269 "[Chown-advanced]", "achown_enter", DLG_NONE
);
271 /* get new listboxes */
272 chl_list
= listbox_new (1, 1, 15, 11, 0, l_call
, NULL
);
274 listbox_add_item (chl_list
, 0, 0, "<Unknown>", NULL
);
277 /* get and put user names in the listbox */
279 while ((chl_pass
= getpwent ()))
280 listbox_add_item (chl_list
, 0, 0, chl_pass
->pw_name
, NULL
);
282 fe
= listbox_search_text (chl_list
, get_owner (sf_stat
->st_uid
));
286 /* get and put group names in the listbox */
288 while ((chl_grp
= getgrent ())) {
289 listbox_add_item (chl_list
, 0, 0, chl_grp
->gr_name
, NULL
);
292 fe
= listbox_search_text (chl_list
, get_group (sf_stat
->st_gid
));
296 listbox_select_entry (chl_list
, fe
);
298 b_pos
= chl_list
->pos
;
299 add_widget (chl_dlg
, chl_list
);
303 if (b_pos
!= chl_list
->pos
){
306 chl_pass
= getpwnam (chl_list
->current
->text
);
309 sf_stat
->st_uid
= chl_pass
->pw_uid
;
312 chl_grp
= getgrnam (chl_list
->current
->text
);
314 sf_stat
->st_gid
= chl_grp
->gr_gid
;
319 ch_flags
[f_pos
+ 6] = '+';
326 if (chl_dlg
->ret_value
== KEY_LEFT
){
331 } else if (chl_dlg
->ret_value
== KEY_RIGHT
) {
334 dlg_one_down (ch_dlg
);
337 /* Here we used to redraw the window */
338 destroy_dlg (chl_dlg
);
342 static void chown_refresh (void)
344 attrset (COLOR_NORMAL
);
347 draw_box (ch_dlg
, 1, 2, 11, 70);
349 dlg_move (ch_dlg
, BY
- 1, 8);
351 dlg_move (ch_dlg
, BY
- 1, 16);
353 dlg_move (ch_dlg
, BY
- 1, 24);
356 dlg_move (ch_dlg
, BY
- 1, 35);
358 dlg_move (ch_dlg
, BY
- 1, 53);
361 dlg_move (ch_dlg
, 3, 4);
363 dlg_move (ch_dlg
, BY
+ 1, 4);
365 dlg_move (ch_dlg
, BY
+ 2, 4);
370 dlg_move (ch_dlg
, 3, 54);
371 printw (_("%6d of %d"), files_on_begin
- (cpanel
->marked
) + 1,
377 attrset (COLOR_HOT_NORMAL
);
378 dlg_move (ch_dlg
, 1, 24);
379 addstr (_(" Chown advanced command "));
382 static void chown_info_update ()
384 /* display file info */
385 attrset (COLOR_NORMAL
);
388 dlg_move (ch_dlg
, 3, 8);
389 printw ("%s", name_trunc (fname
, 45));
390 dlg_move (ch_dlg
, BY
+ 2, 9);
391 printw ("%12o", get_mode ());
394 set_perm (b_att
[0]->text
, sf_stat
->st_mode
>> 6);
395 set_perm (b_att
[1]->text
, sf_stat
->st_mode
>> 3);
396 set_perm (b_att
[2]->text
, sf_stat
->st_mode
);
399 static void b_setpos (int f_pos
) {
403 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
406 static int advanced_chown_callback (Dlg_head
* h
, int Par
, int Msg
)
408 int i
= 0, f_pos
= BUTTONS
- h
->current
->dlg_id
- single_set
- 1;
413 chown_info_update ();
423 if ((flag_pos
/ 3) != f_pos
)
424 flag_pos
= f_pos
* 3;
426 } else if (f_pos
< 5)
427 flag_pos
= f_pos
+ 6;
436 return (dec_flag_pos (f_pos
));
442 return (inc_flag_pos (f_pos
));
452 if (f_pos
<= 2 || f_pos
>= 5)
454 do_enter_key (h
, f_pos
);
465 for (i
= 0; i
< 3; i
++)
466 ch_flags
[i
* 3 + Par
- 3] = (x_toggle
& (1 << Par
)) ? '-' : '+';
467 x_toggle
^= (1 << Par
);
469 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
470 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
481 for (i
= 0; i
< 3; i
++)
482 ch_flags
[i
* 3 + Par
] = (x_toggle
& (1 << Par
)) ? '-' : '+';
483 x_toggle
^= (1 << Par
);
485 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
486 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
498 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,Par)-ch_perm); */
499 if (((WButton
*) h
->current
->widget
)->text
[(flag_pos
% 3)] == '-')
500 ch_flags
[flag_pos
] = '+';
502 ch_flags
[flag_pos
] = '-';
515 flag_pos
= i
+ f_pos
* 3;
516 ch_flags
[flag_pos
] = '=';
532 ch_flags
[flag_pos
] = Par
;
534 advanced_chown_callback (h
, KEY_RIGHT
, DLG_KEY
);
535 if (flag_pos
>8 || !(flag_pos
%3)) dlg_one_down (h
);
544 static void init_chown_advanced (void)
548 sf_stat
= (struct stat
*) malloc (sizeof (struct stat
));
550 end_chown
= need_update
= current_file
= 0;
551 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
552 memset (ch_flags
, '=', 11);
556 ch_dlg
= create_dlg (0, 0, 13, 74, dialog_colors
, advanced_chown_callback
,
557 "[Chown-advanced]", "achown", DLG_CENTER
);
559 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
560 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, chown_advanced_but[i].text, \
563 for (i
= 0; i
< BUTTONS
- 5; i
++)
564 if (!single_set
|| i
< 2)
565 add_widget (ch_dlg
, button_new (XTRACT (i
)));
567 b_att
[0] = button_new (XTRACT (8));
568 b_att
[1] = button_new (XTRACT (7));
569 b_att
[2] = button_new (XTRACT (6));
570 b_user
= button_new (XTRACT (5));
571 b_group
= button_new (XTRACT (4));
573 add_widget (ch_dlg
, b_group
);
574 add_widget (ch_dlg
, b_user
);
575 add_widget (ch_dlg
, b_att
[2]);
576 add_widget (ch_dlg
, b_att
[1]);
577 add_widget (ch_dlg
, b_att
[0]);
580 void chown_advanced_done (void)
584 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
589 static inline void do_chown (uid_t u
, gid_t g
)
591 chown (cpanel
->dir
.list
[current_file
].fname
, u
, g
);
592 file_mark (cpanel
, current_file
, 0);
596 static char *next_file (void)
598 while (!cpanel
->dir
.list
[current_file
].f
.marked
)
601 return cpanel
->dir
.list
[current_file
].fname
;
604 static void apply_advanced_chowns (struct stat
*sf
)
607 gid_t a_gid
= sf
->st_gid
;
608 uid_t a_uid
= sf
->st_uid
;
610 fname
= cpanel
->dir
.list
[current_file
].fname
;
611 need_update
= end_chown
= 1;
612 if (mc_chmod (fname
, get_mode ()) == -1)
613 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
614 fname
, unix_error_string (errno
));
615 /* call mc_chown only, if mc_chmod didn't fail */
616 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf
->st_uid
: -1,
617 (ch_flags
[10] == '+') ? sf
->st_gid
: -1) == -1)
618 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
619 fname
, unix_error_string (errno
));
620 do_file_mark (cpanel
, current_file
, 0);
623 fname
= next_file ();
625 if (!stat_file (fname
, sf
))
627 ch_cmode
= sf
->st_mode
;
628 if (mc_chmod (fname
, get_mode ()) == -1)
629 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
630 fname
, unix_error_string (errno
));
631 /* call mc_chown only, if mc_chmod didn't fail */
632 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? a_uid
: -1, (ch_flags
[10] == '+') ? a_gid
: -1) == -1)
633 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
634 fname
, unix_error_string (errno
));
636 do_file_mark (cpanel
, current_file
, 0);
637 } while (cpanel
->marked
);
640 void chown_advanced_cmd (void)
643 files_on_begin
= cpanel
->marked
;
645 if (!vfs_current_is_local ()) {
646 if (vfs_current_is_extfs ()) {
647 message (1, _(" Oops... "),
648 _(" I can't run the Advanced Chown command on an extfs "));
650 } else if (vfs_current_is_tarfs ()) {
651 message (1, _(" Oops... "),
652 _(" I can't run the Advanced Chown command on a tarfs "));
657 do { /* do while any files remaining */
658 init_chown_advanced ();
661 fname
= next_file (); /* next marked file */
663 fname
= selection (cpanel
)->fname
; /* single file */
665 if (!stat_file (fname
, sf_stat
)){ /* get status of file */
666 destroy_dlg (ch_dlg
);
669 ch_cmode
= sf_stat
->st_mode
;
678 switch (ch_dlg
->ret_value
) {
685 if (mc_chmod (fname
, get_mode ()) == -1)
686 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
687 fname
, unix_error_string (errno
));
688 /* call mc_chown only, if mc_chmod didn't fail */
689 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: -1, (ch_flags
[10] == '+') ? sf_stat
->st_gid
: -1) == -1)
690 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
691 fname
, unix_error_string (errno
));
694 apply_advanced_chowns (sf_stat
);
702 if (cpanel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
) {
703 do_file_mark (cpanel
, current_file
, 0);
706 destroy_dlg (ch_dlg
);
707 } while (cpanel
->marked
&& !end_chown
);
709 chown_advanced_done ();