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>
28 #include <errno.h> /* For errno on SunOS systems */
36 #include "dialog.h" /* For do_refresh() */
37 #include "wtools.h" /* For init_box_colors() */
38 #include "key.h" /* XCTRL and ALT macros */
41 #include "panel.h" /* Needed for the externs */
46 #include "../vfs/vfs.h"
56 #define B_SETALL B_USER
57 #define B_SKIP B_USER + 1
59 #define B_OWN B_USER + 3
60 #define B_GRP B_USER + 4
61 #define B_OTH B_USER + 5
62 #define B_OUSER B_USER + 6
63 #define B_OGROUP B_USER + 7
65 static struct Dlg_head
*ch_dlg
;
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 const char ch_perm
[] = "rwx";
90 static umode_t ch_cmode
;
91 static struct stat
*sf_stat
;
92 static int need_update
;
94 static int current_file
;
95 static int single_set
;
98 static void get_ownership (void)
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
] : '-';
149 static umode_t
get_perm (char *s
, int base
)
154 m
|= (s
[0] == '-') ? 0 :
155 ((s
[0] == '+') ? (1 << (base
+ 2)) : (1 << (base
+ 2)) & ch_cmode
);
157 m
|= (s
[1] == '-') ? 0 :
158 ((s
[1] == '+') ? (1 << (base
+ 1)) : (1 << (base
+ 1)) & ch_cmode
);
160 m
|= (s
[2] == '-') ? 0 :
161 ((s
[2] == '+') ? (1 << base
) : (1 << base
) & ch_cmode
);
166 static umode_t
get_mode (void)
170 m
= ch_cmode
^ (ch_cmode
& 0777);
171 m
|= get_perm (ch_flags
, 6);
172 m
|= get_perm (ch_flags
+ 3, 3);
173 m
|= get_perm (ch_flags
+ 6, 0);
178 static void print_flags (void)
182 attrset (COLOR_NORMAL
);
184 for (i
= 0; i
< 3; i
++){
185 dlg_move (ch_dlg
, BY
+1, 9+i
);
186 addch (ch_flags
[i
]);
189 for (i
= 0; i
< 3; i
++){
190 dlg_move (ch_dlg
, BY
+ 1, 17 + i
);
191 addch (ch_flags
[i
+3]);
194 for (i
= 0; i
< 3; i
++){
195 dlg_move (ch_dlg
, BY
+ 1, 25 + i
);
196 addch (ch_flags
[i
+6]);
199 set_perm_by_flags (b_att
[0]->text
, 0);
200 set_perm_by_flags (b_att
[1]->text
, 3);
201 set_perm_by_flags (b_att
[2]->text
, 6);
203 for (i
= 0; i
< 15; i
++){
204 dlg_move (ch_dlg
, BY
+1, 35+i
);
207 for (i
= 0; i
< 15; i
++){
208 dlg_move (ch_dlg
, BY
+ 1, 53 + i
);
209 addch (ch_flags
[10]);
213 static void update_mode (Dlg_head
* h
)
216 attrset (COLOR_NORMAL
);
217 dlg_move (h
, BY
+ 2, 9);
218 printw ("%12o", get_mode ());
219 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
222 static int l_call (void *data
)
227 static int chl_callback (Dlg_head
* h
, int Par
, int Msg
)
231 common_dialog_repaint (h
);
245 static void do_enter_key (Dlg_head
*h
, int f_pos
)
249 struct passwd
*chl_pass
;
250 struct group
*chl_grp
;
252 int lxx
, lyy
, chl_end
, b_pos
;
255 lxx
= (COLS
- 74) / 2 + ((f_pos
== 3) ? 35 : 53);
256 lyy
= (LINES
- 13) / 2;
259 chl_dlg
= create_dlg (lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
260 "[Advanced Chown]", "achown_enter", DLG_COMPACT
);
262 /* get new listboxes */
263 chl_list
= listbox_new (1, 1, 15, 11, 0, l_call
, NULL
);
265 listbox_add_item (chl_list
, 0, 0, "<Unknown>", NULL
);
268 /* get and put user names in the listbox */
270 while ((chl_pass
= getpwent ()))
271 listbox_add_item (chl_list
, 0, 0, chl_pass
->pw_name
, NULL
);
273 fe
= listbox_search_text (chl_list
, get_owner (sf_stat
->st_uid
));
277 /* get and put group names in the listbox */
279 while ((chl_grp
= getgrent ())) {
280 listbox_add_item (chl_list
, 0, 0, chl_grp
->gr_name
, NULL
);
283 fe
= listbox_search_text (chl_list
, get_group (sf_stat
->st_gid
));
287 listbox_select_entry (chl_list
, fe
);
289 b_pos
= chl_list
->pos
;
290 add_widget (chl_dlg
, chl_list
);
294 if (b_pos
!= chl_list
->pos
){
297 chl_pass
= getpwnam (chl_list
->current
->text
);
300 sf_stat
->st_uid
= chl_pass
->pw_uid
;
303 chl_grp
= getgrnam (chl_list
->current
->text
);
305 sf_stat
->st_gid
= chl_grp
->gr_gid
;
310 ch_flags
[f_pos
+ 6] = '+';
317 if (chl_dlg
->ret_value
== KEY_LEFT
){
322 } else if (chl_dlg
->ret_value
== KEY_RIGHT
) {
325 dlg_one_down (ch_dlg
);
328 /* Here we used to redraw the window */
329 destroy_dlg (chl_dlg
);
333 static void chown_refresh (void)
335 common_dialog_repaint (ch_dlg
);
337 attrset (COLOR_NORMAL
);
339 dlg_move (ch_dlg
, BY
- 1, 8);
341 dlg_move (ch_dlg
, BY
- 1, 16);
343 dlg_move (ch_dlg
, BY
- 1, 24);
346 dlg_move (ch_dlg
, BY
- 1, 35);
348 dlg_move (ch_dlg
, BY
- 1, 53);
351 dlg_move (ch_dlg
, 3, 4);
353 dlg_move (ch_dlg
, BY
+ 1, 4);
355 dlg_move (ch_dlg
, BY
+ 2, 4);
359 dlg_move (ch_dlg
, 3, 54);
360 printw (_("%6d of %d"), files_on_begin
- (cpanel
->marked
) + 1,
367 static void chown_info_update (void)
369 /* display file info */
370 attrset (COLOR_NORMAL
);
373 dlg_move (ch_dlg
, 3, 8);
374 printw ("%s", name_trunc (fname
, 45));
375 dlg_move (ch_dlg
, BY
+ 2, 9);
376 printw ("%12o", get_mode ());
379 set_perm_by_flags (b_att
[0]->text
, 0);
380 set_perm_by_flags (b_att
[1]->text
, 3);
381 set_perm_by_flags (b_att
[2]->text
, 6);
384 static void b_setpos (int f_pos
) {
388 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
391 static int advanced_chown_callback (Dlg_head
* h
, int Par
, int Msg
)
393 int i
= 0, f_pos
= BUTTONS
- h
->current
->dlg_id
- single_set
- 1;
398 chown_info_update ();
408 if ((flag_pos
/ 3) != f_pos
)
409 flag_pos
= f_pos
* 3;
411 } else if (f_pos
< 5)
412 flag_pos
= f_pos
+ 6;
421 return (dec_flag_pos (f_pos
));
427 return (inc_flag_pos (f_pos
));
437 if (f_pos
<= 2 || f_pos
>= 5)
439 do_enter_key (h
, f_pos
);
450 for (i
= 0; i
< 3; i
++)
451 ch_flags
[i
* 3 + Par
- 3] = (x_toggle
& (1 << Par
)) ? '-' : '+';
452 x_toggle
^= (1 << Par
);
454 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
455 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
466 for (i
= 0; i
< 3; i
++)
467 ch_flags
[i
* 3 + Par
] = (x_toggle
& (1 << Par
)) ? '-' : '+';
468 x_toggle
^= (1 << Par
);
470 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
471 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
483 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,Par)-ch_perm); */
484 if (((WButton
*) h
->current
->widget
)->text
[(flag_pos
% 3)] == '-')
485 ch_flags
[flag_pos
] = '+';
487 ch_flags
[flag_pos
] = '-';
500 flag_pos
= i
+ f_pos
* 3;
501 ch_flags
[flag_pos
] = '=';
517 ch_flags
[flag_pos
] = Par
;
519 advanced_chown_callback (h
, KEY_RIGHT
, DLG_KEY
);
520 if (flag_pos
>8 || !(flag_pos
%3)) dlg_one_down (h
);
529 static void init_chown_advanced (void)
533 sf_stat
= g_new (struct stat
, 1);
535 end_chown
= need_update
= current_file
= 0;
536 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
537 memset (ch_flags
, '=', 11);
541 ch_dlg
= create_dlg (0, 0, 13, 74, dialog_colors
, advanced_chown_callback
,
542 "[Advanced Chown]", "achown", DLG_CENTER
);
543 x_set_dialog_title (ch_dlg
, _(" Chown advanced command "));
545 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
546 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, _(chown_advanced_but[i].text), \
549 for (i
= 0; i
< BUTTONS
- 5; i
++)
550 if (!single_set
|| i
< 2)
551 add_widget (ch_dlg
, button_new (XTRACT (i
)));
553 b_att
[0] = button_new (XTRACT (8));
554 b_att
[1] = button_new (XTRACT (7));
555 b_att
[2] = button_new (XTRACT (6));
556 b_user
= button_new (XTRACT (5));
557 b_group
= button_new (XTRACT (4));
559 add_widget (ch_dlg
, b_group
);
560 add_widget (ch_dlg
, b_user
);
561 add_widget (ch_dlg
, b_att
[2]);
562 add_widget (ch_dlg
, b_att
[1]);
563 add_widget (ch_dlg
, b_att
[0]);
567 chown_advanced_done (void)
571 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
576 static inline void do_chown (uid_t u
, gid_t g
)
578 chown (cpanel
->dir
.list
[current_file
].fname
, u
, g
);
579 file_mark (cpanel
, current_file
, 0);
583 static char *next_file (void)
585 while (!cpanel
->dir
.list
[current_file
].f
.marked
)
588 return cpanel
->dir
.list
[current_file
].fname
;
591 static void apply_advanced_chowns (struct stat
*sf
)
594 gid_t a_gid
= sf
->st_gid
;
595 uid_t a_uid
= sf
->st_uid
;
597 fname
= cpanel
->dir
.list
[current_file
].fname
;
598 need_update
= end_chown
= 1;
599 if (mc_chmod (fname
, get_mode ()) == -1)
600 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
601 fname
, unix_error_string (errno
));
602 /* call mc_chown only, if mc_chmod didn't fail */
603 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf
->st_uid
: -1,
604 (ch_flags
[10] == '+') ? sf
->st_gid
: -1) == -1)
605 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
606 fname
, unix_error_string (errno
));
607 do_file_mark (cpanel
, current_file
, 0);
610 fname
= next_file ();
612 if (!stat_file (fname
, sf
))
614 ch_cmode
= sf
->st_mode
;
615 if (mc_chmod (fname
, get_mode ()) == -1)
616 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
617 fname
, unix_error_string (errno
));
618 /* call mc_chown only, if mc_chmod didn't fail */
619 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? a_uid
: -1, (ch_flags
[10] == '+') ? a_gid
: -1) == -1)
620 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
621 fname
, unix_error_string (errno
));
623 do_file_mark (cpanel
, current_file
, 0);
624 } while (cpanel
->marked
);
628 chown_advanced_cmd (void)
631 files_on_begin
= cpanel
->marked
;
633 do { /* do while any files remaining */
634 init_chown_advanced ();
637 fname
= next_file (); /* next marked file */
639 fname
= selection (cpanel
)->fname
; /* single file */
641 if (!stat_file (fname
, sf_stat
)){ /* get status of file */
642 destroy_dlg (ch_dlg
);
645 ch_cmode
= sf_stat
->st_mode
;
654 switch (ch_dlg
->ret_value
) {
661 if (mc_chmod (fname
, get_mode ()) == -1)
662 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
663 fname
, unix_error_string (errno
));
664 /* call mc_chown only, if mc_chmod didn't fail */
665 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: -1, (ch_flags
[10] == '+') ? sf_stat
->st_gid
: -1) == -1)
666 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
667 fname
, unix_error_string (errno
));
670 apply_advanced_chowns (sf_stat
);
678 if (cpanel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
) {
679 do_file_mark (cpanel
, current_file
, 0);
682 destroy_dlg (ch_dlg
);
683 } while (cpanel
->marked
&& !end_chown
);
685 chown_advanced_done ();