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
);
246 do_enter_key (Dlg_head
* h
, int f_pos
)
250 struct passwd
*chl_pass
;
251 struct group
*chl_grp
;
253 int lxx
, lyy
, chl_end
, b_pos
;
258 is_owner
= (f_pos
== 3);
259 title
= is_owner
? _("owner") : _("group");
261 lxx
= (COLS
- 74) / 2 + (is_owner
? 35 : 53);
262 lyy
= (LINES
- 13) / 2;
266 create_dlg (lyy
, lxx
, 13, 17, dialog_colors
, chl_callback
,
267 "[Advanced Chown]", title
, DLG_COMPACT
);
269 /* get new listboxes */
270 chl_list
= listbox_new (1, 1, 15, 11, 0, l_call
, NULL
);
272 listbox_add_item (chl_list
, 0, 0, "<Unknown>", NULL
);
275 /* get and put user names in the listbox */
277 while ((chl_pass
= getpwent ()))
278 listbox_add_item (chl_list
, 0, 0, chl_pass
->pw_name
, NULL
);
280 fe
= listbox_search_text (chl_list
,
281 get_owner (sf_stat
->st_uid
));
283 /* get and put group names in the listbox */
285 while ((chl_grp
= getgrent ())) {
286 listbox_add_item (chl_list
, 0, 0, chl_grp
->gr_name
, NULL
);
289 fe
= listbox_search_text (chl_list
,
290 get_group (sf_stat
->st_gid
));
294 listbox_select_entry (chl_list
, fe
);
296 b_pos
= chl_list
->pos
;
297 add_widget (chl_dlg
, chl_list
);
301 if (b_pos
!= chl_list
->pos
) {
304 chl_pass
= getpwnam (chl_list
->current
->text
);
307 sf_stat
->st_uid
= chl_pass
->pw_uid
;
310 chl_grp
= getgrnam (chl_list
->current
->text
);
312 sf_stat
->st_gid
= chl_grp
->gr_gid
;
317 ch_flags
[f_pos
+ 6] = '+';
324 if (chl_dlg
->ret_value
== KEY_LEFT
) {
329 } else if (chl_dlg
->ret_value
== KEY_RIGHT
) {
332 dlg_one_down (ch_dlg
);
335 /* Here we used to redraw the window */
336 destroy_dlg (chl_dlg
);
340 static void chown_refresh (void)
342 common_dialog_repaint (ch_dlg
);
344 attrset (COLOR_NORMAL
);
346 dlg_move (ch_dlg
, BY
- 1, 8);
348 dlg_move (ch_dlg
, BY
- 1, 16);
350 dlg_move (ch_dlg
, BY
- 1, 24);
353 dlg_move (ch_dlg
, BY
- 1, 35);
355 dlg_move (ch_dlg
, BY
- 1, 53);
358 dlg_move (ch_dlg
, 3, 4);
360 dlg_move (ch_dlg
, BY
+ 1, 4);
362 dlg_move (ch_dlg
, BY
+ 2, 4);
366 dlg_move (ch_dlg
, 3, 54);
367 printw (_("%6d of %d"), files_on_begin
- (cpanel
->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 printw ("%s", name_trunc (fname
, 45));
382 dlg_move (ch_dlg
, BY
+ 2, 9);
383 printw ("%12o", get_mode ());
386 set_perm_by_flags (b_att
[0]->text
, 0);
387 set_perm_by_flags (b_att
[1]->text
, 3);
388 set_perm_by_flags (b_att
[2]->text
, 6);
391 static void b_setpos (int f_pos
) {
395 b_att
[f_pos
]->hotpos
= (flag_pos
% 3);
398 static int advanced_chown_callback (Dlg_head
* h
, int Par
, int Msg
)
400 int i
= 0, f_pos
= BUTTONS
- h
->current
->dlg_id
- single_set
- 1;
405 chown_info_update ();
415 if ((flag_pos
/ 3) != f_pos
)
416 flag_pos
= f_pos
* 3;
418 } else if (f_pos
< 5)
419 flag_pos
= f_pos
+ 6;
428 return (dec_flag_pos (f_pos
));
434 return (inc_flag_pos (f_pos
));
444 if (f_pos
<= 2 || f_pos
>= 5)
446 do_enter_key (h
, f_pos
);
457 for (i
= 0; i
< 3; i
++)
458 ch_flags
[i
* 3 + Par
- 3] = (x_toggle
& (1 << Par
)) ? '-' : '+';
459 x_toggle
^= (1 << Par
);
461 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
462 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
473 for (i
= 0; i
< 3; i
++)
474 ch_flags
[i
* 3 + Par
] = (x_toggle
& (1 << Par
)) ? '-' : '+';
475 x_toggle
^= (1 << Par
);
477 dlg_broadcast_msg (h
, WIDGET_DRAW
, 0);
478 send_message (h
, h
->current
->widget
, WIDGET_FOCUS
, 0);
490 flag_pos
= f_pos
* 3 + i
; /* (strchr(ch_perm,Par)-ch_perm); */
491 if (((WButton
*) h
->current
->widget
)->text
[(flag_pos
% 3)] == '-')
492 ch_flags
[flag_pos
] = '+';
494 ch_flags
[flag_pos
] = '-';
507 flag_pos
= i
+ f_pos
* 3;
508 ch_flags
[flag_pos
] = '=';
524 ch_flags
[flag_pos
] = Par
;
526 advanced_chown_callback (h
, KEY_RIGHT
, DLG_KEY
);
527 if (flag_pos
>8 || !(flag_pos
%3)) dlg_one_down (h
);
537 init_chown_advanced (void)
541 sf_stat
= g_new (struct stat
, 1);
543 end_chown
= need_update
= current_file
= 0;
544 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
545 memset (ch_flags
, '=', 11);
550 create_dlg (0, 0, 13, 74, dialog_colors
, advanced_chown_callback
,
551 "[Advanced Chown]", _(" Chown advanced command "),
554 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
555 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, _(chown_advanced_but[i].text), \
558 for (i
= 0; i
< BUTTONS
- 5; i
++)
559 if (!single_set
|| i
< 2)
560 add_widget (ch_dlg
, button_new (XTRACT (i
)));
562 b_att
[0] = button_new (XTRACT (8));
563 b_att
[1] = button_new (XTRACT (7));
564 b_att
[2] = button_new (XTRACT (6));
565 b_user
= button_new (XTRACT (5));
566 b_group
= button_new (XTRACT (4));
568 add_widget (ch_dlg
, b_group
);
569 add_widget (ch_dlg
, b_user
);
570 add_widget (ch_dlg
, b_att
[2]);
571 add_widget (ch_dlg
, b_att
[1]);
572 add_widget (ch_dlg
, b_att
[0]);
576 chown_advanced_done (void)
580 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
585 static inline void do_chown (uid_t u
, gid_t g
)
587 chown (cpanel
->dir
.list
[current_file
].fname
, u
, g
);
588 file_mark (cpanel
, current_file
, 0);
592 static char *next_file (void)
594 while (!cpanel
->dir
.list
[current_file
].f
.marked
)
597 return cpanel
->dir
.list
[current_file
].fname
;
600 static void apply_advanced_chowns (struct stat
*sf
)
603 gid_t a_gid
= sf
->st_gid
;
604 uid_t a_uid
= sf
->st_uid
;
606 fname
= cpanel
->dir
.list
[current_file
].fname
;
607 need_update
= end_chown
= 1;
608 if (mc_chmod (fname
, get_mode ()) == -1)
609 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
610 fname
, unix_error_string (errno
));
611 /* call mc_chown only, if mc_chmod didn't fail */
612 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf
->st_uid
: -1,
613 (ch_flags
[10] == '+') ? sf
->st_gid
: -1) == -1)
614 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
615 fname
, unix_error_string (errno
));
616 do_file_mark (cpanel
, current_file
, 0);
619 fname
= next_file ();
621 if (!stat_file (fname
, sf
))
623 ch_cmode
= sf
->st_mode
;
624 if (mc_chmod (fname
, get_mode ()) == -1)
625 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
626 fname
, unix_error_string (errno
));
627 /* call mc_chown only, if mc_chmod didn't fail */
628 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? a_uid
: -1, (ch_flags
[10] == '+') ? a_gid
: -1) == -1)
629 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
630 fname
, unix_error_string (errno
));
632 do_file_mark (cpanel
, current_file
, 0);
633 } while (cpanel
->marked
);
637 chown_advanced_cmd (void)
640 files_on_begin
= cpanel
->marked
;
642 do { /* do while any files remaining */
643 init_chown_advanced ();
646 fname
= next_file (); /* next marked file */
648 fname
= selection (cpanel
)->fname
; /* single file */
650 if (!stat_file (fname
, sf_stat
)){ /* get status of file */
651 destroy_dlg (ch_dlg
);
654 ch_cmode
= sf_stat
->st_mode
;
663 switch (ch_dlg
->ret_value
) {
670 if (mc_chmod (fname
, get_mode ()) == -1)
671 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
672 fname
, unix_error_string (errno
));
673 /* call mc_chown only, if mc_chmod didn't fail */
674 else if (mc_chown (fname
, (ch_flags
[9] == '+') ? sf_stat
->st_uid
: -1, (ch_flags
[10] == '+') ? sf_stat
->st_gid
: -1) == -1)
675 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
676 fname
, unix_error_string (errno
));
679 apply_advanced_chowns (sf_stat
);
687 if (cpanel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
) {
688 do_file_mark (cpanel
, current_file
, 0);
691 destroy_dlg (ch_dlg
);
692 } while (cpanel
->marked
&& !end_chown
);
694 chown_advanced_done ();