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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /* Needed for the extern declarations of integer parameters */
21 #include <sys/types.h>
22 #include <sys/param.h>
31 #include <errno.h> /* For errno on SunOS systems */
38 #include "dialog.h" /* For do_refresh() */
39 #include "wtools.h" /* For init_box_colors() */
40 #include "key.h" /* XCTRL and ALT macros */
43 #include "panel.h" /* Needed for the externs */
48 #include "../vfs/vfs.h"
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
68 int ret_cmd
, flags
, y
, x
;
70 } chown_advanced_but
[BUTTONS
] = {
71 { B_CANCEL
, NORMAL_BUTTON
, 4, 53, N_("&Cancel") },
72 { B_ENTER
, DEFPUSH_BUTTON
,4, 40, N_("&Set") },
73 { B_SKIP
, NORMAL_BUTTON
, 4, 23, N_("S&kip") },
74 { B_SETALL
, NORMAL_BUTTON
, 4, 0, N_("Set &all")},
75 { B_ENTER
, NARROW_BUTTON
, 0, 47, " "},
76 { B_ENTER
, NARROW_BUTTON
, 0, 29, " "},
77 { B_ENTER
, NARROW_BUTTON
, 0, 19, " "},
78 { B_ENTER
, NARROW_BUTTON
, 0, 11, " "},
79 { B_ENTER
, NARROW_BUTTON
, 0, 3, " "},
82 static WButton
*b_att
[3]; /* permission */
83 static WButton
*b_user
, *b_group
; /* owner */
85 static int files_on_begin
; /* Number of files at startup */
88 static char ch_flags
[11];
89 static char *ch_perm
= "rwx";
90 static umode_t ch_cmode
;
92 static int need_update
;
94 static int current_file
;
95 static int single_set
;
98 static void get_ownership ()
99 { /* set buttons - ownership */
102 name_t
= name_trunc (get_owner (sf_stat
->st_uid
), 15);
103 memset (b_user
->text
, ' ', 15);
104 strncpy (b_user
->text
, name_t
, strlen (name_t
));
105 name_t
= name_trunc (get_group (sf_stat
->st_gid
), 15);
106 memset (b_group
->text
, ' ', 15);
107 strncpy (b_group
->text
, name_t
, strlen (name_t
));
111 static int inc_flag_pos (int f_pos
)
113 if (flag_pos
== 10) {
118 if (!(flag_pos
% 3) || f_pos
> 2)
123 static int dec_flag_pos (int f_pos
)
130 if (!((flag_pos
+ 1) % 3) || f_pos
> 2)
135 static void set_perm_by_flags (char *s
, int f_p
)
139 for (i
= 0; i
< 3; i
++)
140 if (ch_flags
[f_p
+ i
] == '+')
142 else if (ch_flags
[f_p
+ i
] == '-')
145 s
[i
] = (ch_cmode
& (1 << (8 - f_p
- i
))) ? ch_perm
[i
] : '-';
148 static void set_perm (char *s
, int p
)
150 s
[0] = (p
& 4) ? 'r' : '-';
151 s
[1] = (p
& 2) ? 'w' : '-';
152 s
[2] = (p
& 1) ? 'x' : '-';
155 static umode_t
get_perm (char *s
, int base
)
160 m
|= (s
[0] == '-') ? 0 :
161 ((s
[0] == '+') ? (1 << (base
+ 2)) : (1 << (base
+ 2)) & ch_cmode
);
163 m
|= (s
[1] == '-') ? 0 :
164 ((s
[1] == '+') ? (1 << (base
+ 1)) : (1 << (base
+ 1)) & ch_cmode
);
166 m
|= (s
[2] == '-') ? 0 :
167 ((s
[2] == '+') ? (1 << base
) : (1 << base
) & ch_cmode
);
172 static umode_t
get_mode ()
176 m
= ch_cmode
^ (ch_cmode
& 0777);
177 m
|= get_perm (ch_flags
, 6);
178 m
|= get_perm (ch_flags
+ 3, 3);
179 m
|= get_perm (ch_flags
+ 6, 0);
184 static void print_flags (void)
188 attrset (COLOR_NORMAL
);
190 for (i
= 0; i
< 3; i
++){
191 dlg_move (ch_dlg
, BY
+1, 9+i
);
192 addch (ch_flags
[i
]);
195 for (i
= 0; i
< 3; i
++){
196 dlg_move (ch_dlg
, BY
+ 1, 17 + i
);
197 addch (ch_flags
[i
+3]);
200 for (i
= 0; i
< 3; i
++){
201 dlg_move (ch_dlg
, BY
+ 1, 25 + i
);
202 addch (ch_flags
[i
+6]);
205 set_perm_by_flags (b_att
[0]->text
, 0);
206 set_perm_by_flags (b_att
[1]->text
, 3);
207 set_perm_by_flags (b_att
[2]->text
, 6);
209 for (i
= 0; i
< 15; i
++){
210 dlg_move (ch_dlg
, BY
+1, 35+i
);
213 for (i
= 0; i
< 15; i
++){
214 dlg_move (ch_dlg
, BY
+ 1, 53 + i
);
215 addch (ch_flags
[10]);
219 static void update_mode (Dlg_head
* h
)
222 attrset (COLOR_NORMAL
);
223 dlg_move (h
, BY
+ 2, 9);
224 printw ("%12o", get_mode ());
225 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
228 static int l_call (void *data
)
233 static int chl_callback (Dlg_head
* h
, int Par
, int Msg
)
237 attrset (COLOR_NORMAL
);
239 draw_box (h
, 0, 0, 13, 17);
253 static void do_enter_key (Dlg_head
*h
, int f_pos
)
257 struct passwd
*chl_pass
;
258 struct group
*chl_grp
;
260 int lxx
, lyy
, chl_end
, b_pos
;
263 lxx
= (COLS
- 74) / 2 + ((f_pos
== 3) ? 35 : 53);
264 lyy
= (LINES
- 13) / 2;
267 chl_dlg
= create_dlg (lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
268 "[Chown-advanced]", "achown_enter", DLG_NONE
);
270 /* get new listboxes */
271 chl_list
= listbox_new (1, 1, 15, 11, 0, l_call
, NULL
);
273 listbox_add_item (chl_list
, 0, 0, "<Unknown>", NULL
);
276 /* get and put user names in the listbox */
278 while ((chl_pass
= getpwent ()))
279 listbox_add_item (chl_list
, 0, 0, chl_pass
->pw_name
, NULL
);
281 fe
= listbox_search_text (chl_list
, get_owner (sf_stat
->st_uid
));
285 /* get and put group names in the listbox */
287 while ((chl_grp
= getgrent ())) {
288 listbox_add_item (chl_list
, 0, 0, chl_grp
->gr_name
, NULL
);
291 fe
= listbox_search_text (chl_list
, get_group (sf_stat
->st_gid
));
295 listbox_select_entry (chl_list
, fe
);
297 b_pos
= chl_list
->pos
;
298 add_widget (chl_dlg
, chl_list
);
302 if (b_pos
!= chl_list
->pos
){
305 chl_pass
= getpwnam (chl_list
->current
->text
);
308 sf_stat
->st_uid
= chl_pass
->pw_uid
;
311 chl_grp
= getgrnam (chl_list
->current
->text
);
313 sf_stat
->st_gid
= chl_grp
->gr_gid
;
318 ch_flags
[f_pos
+ 6] = '+';
325 if (chl_dlg
->ret_value
== KEY_LEFT
){
330 } else if (chl_dlg
->ret_value
== KEY_RIGHT
) {
333 dlg_one_down (ch_dlg
);
336 /* Here we used to redraw the window */
337 destroy_dlg (chl_dlg
);
341 static void chown_refresh (void)
343 attrset (COLOR_NORMAL
);
346 draw_box (ch_dlg
, 1, 2, 11, 70);
348 dlg_move (ch_dlg
, BY
- 1, 8);
350 dlg_move (ch_dlg
, BY
- 1, 16);
352 dlg_move (ch_dlg
, BY
- 1, 24);
355 dlg_move (ch_dlg
, BY
- 1, 35);
357 dlg_move (ch_dlg
, BY
- 1, 53);
360 dlg_move (ch_dlg
, 3, 4);
362 dlg_move (ch_dlg
, BY
+ 1, 4);
364 dlg_move (ch_dlg
, BY
+ 2, 4);
369 dlg_move (ch_dlg
, 3, 54);
370 printw (_("%6d of %d"), files_on_begin
- (cpanel
->marked
) + 1,
376 attrset (COLOR_HOT_NORMAL
);
377 dlg_move (ch_dlg
, 1, 24);
378 addstr (_(" Chown advanced command "));
381 static void chown_info_update ()
383 /* display file info */
384 attrset (COLOR_NORMAL
);
387 dlg_move (ch_dlg
, 3, 8);
388 printw ("%s", name_trunc (fname
, 45));
389 dlg_move (ch_dlg
, BY
+ 2, 9);
390 printw ("%12o", get_mode ());
393 set_perm (b_att
[0]->text
, sf_stat
->st_mode
>> 6);
394 set_perm (b_att
[1]->text
, sf_stat
->st_mode
>> 3);
395 set_perm (b_att
[2]->text
, sf_stat
->st_mode
);
398 static void b_setpos (int f_pos
) {
402 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
405 static int advanced_chown_callback (Dlg_head
* h
, int Par
, int Msg
)
407 int i
= 0, f_pos
= BUTTONS
- h
->current
->dlg_id
- single_set
- 1;
412 chown_info_update ();
422 if ((flag_pos
/ 3) != f_pos
)
423 flag_pos
= f_pos
* 3;
425 } else if (f_pos
< 5)
426 flag_pos
= f_pos
+ 6;
435 return (dec_flag_pos (f_pos
));
441 return (inc_flag_pos (f_pos
));
451 if (f_pos
<= 2 || f_pos
>= 5)
453 do_enter_key (h
, f_pos
);
464 for (i
= 0; i
< 3; i
++)
465 ch_flags
[i
* 3 + Par
- 3] = (x_toggle
& (1 << Par
)) ? '-' : '+';
466 x_toggle
^= (1 << Par
);
468 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
469 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
480 for (i
= 0; i
< 3; i
++)
481 ch_flags
[i
* 3 + Par
] = (x_toggle
& (1 << Par
)) ? '-' : '+';
482 x_toggle
^= (1 << Par
);
484 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
485 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
497 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,Par)-ch_perm); */
498 if (((WButton
*) h
->current
->widget
)->text
[(flag_pos
% 3)] == '-')
499 ch_flags
[flag_pos
] = '+';
501 ch_flags
[flag_pos
] = '-';
514 flag_pos
= i
+ f_pos
* 3;
515 ch_flags
[flag_pos
] = '=';
531 ch_flags
[flag_pos
] = Par
;
533 advanced_chown_callback (h
, KEY_RIGHT
, DLG_KEY
);
534 if (flag_pos
>8 || !(flag_pos
%3)) dlg_one_down (h
);
543 static void init_chown_advanced (void)
547 sf_stat
= g_new (struct stat
, 1);
549 end_chown
= need_update
= current_file
= 0;
550 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
551 memset (ch_flags
, '=', 11);
555 ch_dlg
= create_dlg (0, 0, 13, 74, dialog_colors
, advanced_chown_callback
,
556 "[Chown-advanced]", "achown", DLG_CENTER
);
558 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
559 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, _(chown_advanced_but[i].text), \
562 for (i
= 0; i
< BUTTONS
- 5; i
++)
563 if (!single_set
|| i
< 2)
564 add_widget (ch_dlg
, button_new (XTRACT (i
)));
566 b_att
[0] = button_new (XTRACT (8));
567 b_att
[1] = button_new (XTRACT (7));
568 b_att
[2] = button_new (XTRACT (6));
569 b_user
= button_new (XTRACT (5));
570 b_group
= button_new (XTRACT (4));
572 add_widget (ch_dlg
, b_group
);
573 add_widget (ch_dlg
, b_user
);
574 add_widget (ch_dlg
, b_att
[2]);
575 add_widget (ch_dlg
, b_att
[1]);
576 add_widget (ch_dlg
, b_att
[0]);
580 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
);
641 chown_advanced_cmd (void)
644 files_on_begin
= cpanel
->marked
;
647 /* I think that this code is trying to be much too clever -- pavel@ucw.cz */
648 if (!vfs_current_is_local ()) {
649 if (vfs_current_is_extfs ()) {
650 message (1, _(" Oops... "),
651 _(" I can't run the Advanced Chown command on an extfs "));
653 } else if (vfs_current_is_tarfs ()) {
654 message (1, _(" Oops... "),
655 _(" I can't run the Advanced Chown command on a tarfs "));
661 do { /* do while any files remaining */
662 init_chown_advanced ();
665 fname
= next_file (); /* next marked file */
667 fname
= selection (cpanel
)->fname
; /* single file */
669 if (!stat_file (fname
, sf_stat
)){ /* get status of file */
670 destroy_dlg (ch_dlg
);
673 ch_cmode
= sf_stat
->st_mode
;
682 switch (ch_dlg
->ret_value
) {
689 if (mc_chmod (fname
, get_mode ()) == -1)
690 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
691 fname
, unix_error_string (errno
));
692 /* call mc_chown only, if mc_chmod didn't fail */
693 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: -1, (ch_flags
[10] == '+') ? sf_stat
->st_gid
: -1) == -1)
694 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
695 fname
, unix_error_string (errno
));
698 apply_advanced_chowns (sf_stat
);
706 if (cpanel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
) {
707 do_file_mark (cpanel
, current_file
, 0);
710 destroy_dlg (ch_dlg
);
711 } while (cpanel
->marked
&& !end_chown
);
713 chown_advanced_done ();