vfs/vfs-impl.h: fixed missing includes
[free-mc.git] / src / achown.c
blob36cb4d2c4f590ddf9cf30a986d13f515e58f1d97
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.
20 /** \file achown.c
21 * \brief Source: Contains functions for advanced chowning
24 #include <config.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <pwd.h>
34 #include <grp.h>
36 #include "global.h"
38 #include "../src/tty/tty.h"
39 #include "../src/tty/key.h" /* XCTRL and ALT macros */
40 #include "../src/skin/skin.h"
42 #include "dialog.h"
43 #include "widget.h"
44 #include "wtools.h" /* For init_box_colors() */
45 #include "strutil.h"
47 #include "dir.h"
48 #include "panel.h" /* Needed for the externs */
49 #include "chmod.h"
50 #include "main.h" /* update_panels() */
51 #include "layout.h" /* repaint_screen() */
52 #include "achown.h"
54 #define BX 5
55 #define BY 6
57 #define TX 50
58 #define TY 2
60 #define BUTTONS 9
62 #define B_SETALL B_USER
63 #define B_SKIP (B_USER + 1)
65 #define B_OWN (B_USER + 3)
66 #define B_GRP (B_USER + 4)
67 #define B_OTH (B_USER + 5)
68 #define B_OUSER (B_USER + 6)
69 #define B_OGROUP (B_USER + 7)
71 static struct Dlg_head *ch_dlg;
73 static struct {
74 int ret_cmd, flags, y, x;
75 const char *text;
76 } chown_advanced_but [BUTTONS] = {
77 { B_CANCEL, NORMAL_BUTTON, 4, 53, N_("&Cancel") },
78 { B_ENTER, DEFPUSH_BUTTON,4, 40, N_("&Set") },
79 { B_SKIP, NORMAL_BUTTON, 4, 23, N_("S&kip") },
80 { B_SETALL, NORMAL_BUTTON, 4, 0, N_("Set &all")},
81 { B_ENTER, NARROW_BUTTON, 0, 47, ""},
82 { B_ENTER, NARROW_BUTTON, 0, 29, ""},
83 { B_ENTER, NARROW_BUTTON, 0, 19, " "},
84 { B_ENTER, NARROW_BUTTON, 0, 11, " "},
85 { B_ENTER, NARROW_BUTTON, 0, 3, " "}
88 static WButton *b_att[3]; /* permission */
89 static WButton *b_user, *b_group; /* owner */
91 static int files_on_begin; /* Number of files at startup */
92 static int flag_pos;
93 static int x_toggle;
94 static char ch_flags[11];
95 static const char ch_perm[] = "rwx";
96 static mode_t ch_cmode;
97 static struct stat *sf_stat;
98 static int need_update;
99 static int end_chown;
100 static int current_file;
101 static int single_set;
102 static char *fname;
104 static void update_ownership (void)
106 button_set_text (b_user, get_owner (sf_stat->st_uid));
107 button_set_text (b_group, get_group (sf_stat->st_gid));
111 static cb_ret_t inc_flag_pos (int f_pos)
113 if (flag_pos == 10) {
114 flag_pos = 0;
115 return MSG_NOT_HANDLED;
117 flag_pos++;
118 if (!(flag_pos % 3) || f_pos > 2)
119 return MSG_NOT_HANDLED;
120 return MSG_HANDLED;
123 static cb_ret_t dec_flag_pos (int f_pos)
125 if (!flag_pos) {
126 flag_pos = 10;
127 return MSG_NOT_HANDLED;
129 flag_pos--;
130 if (!((flag_pos + 1) % 3) || f_pos > 2)
131 return MSG_NOT_HANDLED;
132 return MSG_HANDLED;
135 static void set_perm_by_flags (char *s, int f_p)
137 int i;
139 for (i = 0; i < 3; i++) {
140 if (ch_flags[f_p + i] == '+')
141 s[i] = ch_perm[i];
142 else if (ch_flags[f_p + i] == '-')
143 s[i] = '-';
144 else
145 s[i] = (ch_cmode & (1 << (8 - f_p - i))) ? ch_perm[i] : '-';
149 static void update_permissions (void)
151 set_perm_by_flags (b_att[0]->text.start, 0);
152 set_perm_by_flags (b_att[1]->text.start, 3);
153 set_perm_by_flags (b_att[2]->text.start, 6);
156 static mode_t get_perm (char *s, int base)
158 mode_t m;
160 m = 0;
161 m |= (s[0] == '-') ? 0 :
162 ((s[0] == '+') ? (mode_t)(1 << (base + 2)) : (1 << (base + 2)) & ch_cmode);
164 m |= (s[1] == '-') ? 0 :
165 ((s[1] == '+') ? (mode_t)(1 << (base + 1)) : (1 << (base + 1)) & ch_cmode);
167 m |= (s[2] == '-') ? 0 :
168 ((s[2] == '+') ? (mode_t)(1 << base) : (1 << base) & ch_cmode);
170 return m;
173 static mode_t get_mode (void)
175 mode_t m;
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);
182 return m;
185 static void print_flags (void)
187 int i;
189 tty_setcolor (COLOR_NORMAL);
191 for (i = 0; i < 3; i++){
192 dlg_move (ch_dlg, BY+1, 9+i);
193 tty_print_char (ch_flags [i]);
196 for (i = 0; i < 3; i++){
197 dlg_move (ch_dlg, BY + 1, 17 + i);
198 tty_print_char (ch_flags [i+3]);
201 for (i = 0; i < 3; i++){
202 dlg_move (ch_dlg, BY + 1, 25 + i);
203 tty_print_char (ch_flags [i+6]);
206 update_permissions ();
208 for (i = 0; i < 15; i++){
209 dlg_move (ch_dlg, BY+1, 35+i);
210 tty_print_char (ch_flags[9]);
212 for (i = 0; i < 15; i++){
213 dlg_move (ch_dlg, BY + 1, 53 + i);
214 tty_print_char (ch_flags[10]);
218 static void update_mode (Dlg_head * h)
220 print_flags ();
221 tty_setcolor (COLOR_NORMAL);
222 dlg_move (h, BY + 2, 9);
223 tty_printf ("%12o", get_mode ());
224 send_message (h->current, WIDGET_FOCUS, 0);
227 static cb_ret_t
228 chl_callback (Dlg_head *h, dlg_msg_t msg, int parm)
230 switch (msg) {
231 case DLG_KEY:
232 switch (parm) {
233 case KEY_LEFT:
234 case KEY_RIGHT:
235 h->ret_value = parm;
236 dlg_stop (h);
239 default:
240 return default_dlg_callback (h, msg, parm);
244 static void
245 do_enter_key (Dlg_head * h, int f_pos)
247 Dlg_head *chl_dlg;
248 WListbox *chl_list;
249 struct passwd *chl_pass;
250 struct group *chl_grp;
251 WLEntry *fe;
252 int lxx, lyy, chl_end, b_pos;
253 int is_owner;
254 const char *title;
256 do {
257 is_owner = (f_pos == 3);
258 title = is_owner ? _("owner") : _("group");
260 lxx = (COLS - 74) / 2 + (is_owner ? 35 : 53);
261 lyy = (LINES - 13) / 2;
262 chl_end = 0;
264 chl_dlg =
265 create_dlg (lyy, lxx, 13, 17, dialog_colors, chl_callback,
266 "[Advanced Chown]", title, DLG_COMPACT | DLG_REVERSE);
268 /* get new listboxes */
269 chl_list = listbox_new (1, 1, 11, 15, NULL);
271 listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0,
272 "<Unknown>", NULL);
274 if (is_owner) {
275 /* get and put user names in the listbox */
276 setpwent ();
277 while ((chl_pass = getpwent ())) {
278 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0,
279 chl_pass->pw_name, NULL);
281 endpwent ();
282 fe = listbox_search_text (chl_list,
283 get_owner (sf_stat->st_uid));
284 } else {
285 /* get and put group names in the listbox */
286 setgrent ();
287 while ((chl_grp = getgrent ())) {
288 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0,
289 chl_grp->gr_name, NULL);
291 endgrent ();
292 fe = listbox_search_text (chl_list,
293 get_group (sf_stat->st_gid));
296 if (fe)
297 listbox_select_entry (chl_list, fe);
299 b_pos = chl_list->pos;
300 add_widget (chl_dlg, chl_list);
302 run_dlg (chl_dlg);
304 if (b_pos != chl_list->pos) {
305 int ok = 0;
306 if (is_owner) {
307 chl_pass = getpwnam (chl_list->current->text);
308 if (chl_pass) {
309 ok = 1;
310 sf_stat->st_uid = chl_pass->pw_uid;
312 } else {
313 chl_grp = getgrnam (chl_list->current->text);
314 if (chl_grp) {
315 sf_stat->st_gid = chl_grp->gr_gid;
316 ok = 1;
319 if (ok) {
320 ch_flags[f_pos + 6] = '+';
321 update_ownership ();
323 dlg_focus (h);
324 if (ok)
325 print_flags ();
327 if (chl_dlg->ret_value == KEY_LEFT) {
328 if (!is_owner)
329 chl_end = 1;
330 dlg_one_up (ch_dlg);
331 f_pos--;
332 } else if (chl_dlg->ret_value == KEY_RIGHT) {
333 if (is_owner)
334 chl_end = 1;
335 dlg_one_down (ch_dlg);
336 f_pos++;
338 /* Here we used to redraw the window */
339 destroy_dlg (chl_dlg);
340 } while (chl_end);
343 static void chown_refresh (void)
345 common_dialog_repaint (ch_dlg);
347 tty_setcolor (COLOR_NORMAL);
349 dlg_move (ch_dlg, BY - 1, 8);
350 tty_print_string (_("owner"));
351 dlg_move (ch_dlg, BY - 1, 16);
352 tty_print_string (_("group"));
353 dlg_move (ch_dlg, BY - 1, 24);
354 tty_print_string (_("other"));
356 dlg_move (ch_dlg, BY - 1, 35);
357 tty_print_string (_("owner"));
358 dlg_move (ch_dlg, BY - 1, 53);
359 tty_print_string (_("group"));
361 dlg_move (ch_dlg, 3, 4);
362 tty_print_string (_("On"));
363 dlg_move (ch_dlg, BY + 1, 4);
364 tty_print_string (_("Flag"));
365 dlg_move (ch_dlg, BY + 2, 4);
366 tty_print_string (_("Mode"));
368 if (!single_set){
369 dlg_move (ch_dlg, 3, 54);
370 tty_printf (_("%6d of %d"),
371 files_on_begin - (current_panel->marked) + 1,
372 files_on_begin);
375 print_flags ();
378 static void chown_info_update (void)
380 /* display file info */
381 tty_setcolor (COLOR_NORMAL);
383 /* name && mode */
384 dlg_move (ch_dlg, 3, 8);
385 tty_print_string (str_fit_to_term (fname, 45, J_LEFT_FIT));
386 dlg_move (ch_dlg, BY + 2, 9);
387 tty_printf ("%12o", get_mode ());
389 /* permissions */
390 update_permissions ();
393 static void b_setpos (int f_pos) {
394 b_att[0]->hotpos=-1;
395 b_att[1]->hotpos=-1;
396 b_att[2]->hotpos=-1;
397 b_att[f_pos]->hotpos = (flag_pos % 3);
400 static cb_ret_t
401 advanced_chown_callback (Dlg_head *h, dlg_msg_t msg, int parm)
403 int i = 0, f_pos = BUTTONS - h->current->dlg_id - single_set - 1;
405 switch (msg) {
406 case DLG_DRAW:
407 chown_refresh ();
408 chown_info_update ();
409 return MSG_HANDLED;
411 case DLG_POST_KEY:
412 if (f_pos < 3)
413 b_setpos (f_pos);
414 return MSG_HANDLED;
416 case DLG_FOCUS:
417 if (f_pos < 3) {
418 if ((flag_pos / 3) != f_pos)
419 flag_pos = f_pos * 3;
420 b_setpos (f_pos);
421 } else if (f_pos < 5)
422 flag_pos = f_pos + 6;
423 return MSG_HANDLED;
425 case DLG_KEY:
426 switch (parm) {
428 case XCTRL ('b'):
429 case KEY_LEFT:
430 if (f_pos < 5)
431 return (dec_flag_pos (f_pos));
432 break;
434 case XCTRL ('f'):
435 case KEY_RIGHT:
436 if (f_pos < 5)
437 return (inc_flag_pos (f_pos));
438 break;
440 case ' ':
441 if (f_pos < 3)
442 return MSG_HANDLED;
443 break;
445 case '\n':
446 case KEY_ENTER:
447 if (f_pos <= 2 || f_pos >= 5)
448 break;
449 do_enter_key (h, f_pos);
450 return MSG_HANDLED;
452 case ALT ('x'):
453 i++;
455 case ALT ('w'):
456 i++;
458 case ALT ('r'):
459 parm = i + 3;
460 for (i = 0; i < 3; i++)
461 ch_flags[i * 3 + parm - 3] =
462 (x_toggle & (1 << parm)) ? '-' : '+';
463 x_toggle ^= (1 << parm);
464 update_mode (h);
465 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
466 send_message (h->current, WIDGET_FOCUS, 0);
467 break;
469 case XCTRL ('x'):
470 i++;
472 case XCTRL ('w'):
473 i++;
475 case XCTRL ('r'):
476 parm = i;
477 for (i = 0; i < 3; i++)
478 ch_flags[i * 3 + parm] =
479 (x_toggle & (1 << parm)) ? '-' : '+';
480 x_toggle ^= (1 << parm);
481 update_mode (h);
482 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
483 send_message (h->current, WIDGET_FOCUS, 0);
484 break;
486 case 'x':
487 i++;
489 case 'w':
490 i++;
492 case 'r':
493 if (f_pos > 2)
494 break;
495 flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */
496 if (((WButton *) h->current)->text.start[(flag_pos % 3)] ==
497 '-')
498 ch_flags[flag_pos] = '+';
499 else
500 ch_flags[flag_pos] = '-';
501 update_mode (h);
502 break;
504 case '4':
505 i++;
507 case '2':
508 i++;
510 case '1':
511 if (f_pos > 2)
512 break;
513 flag_pos = i + f_pos * 3;
514 ch_flags[flag_pos] = '=';
515 update_mode (h);
516 break;
518 case '-':
519 if (f_pos > 2)
520 break;
522 case '*':
523 if (parm == '*')
524 parm = '=';
526 case '=':
527 case '+':
528 if (f_pos > 4)
529 break;
530 ch_flags[flag_pos] = parm;
531 update_mode (h);
532 advanced_chown_callback (h, DLG_KEY, KEY_RIGHT);
533 if (flag_pos > 8 || !(flag_pos % 3))
534 dlg_one_down (h);
536 break;
538 return MSG_NOT_HANDLED;
540 default:
541 return default_dlg_callback (h, msg, parm);
545 static void
546 init_chown_advanced (void)
548 int i;
549 enum { dlg_h = 13, dlg_w = 74, n_elem = 4 };
550 #ifdef ENABLE_NLS
551 static int i18n_len = 0;
553 if (i18n_len == 0) {
554 int dx, cx;
555 for (i = 0 ; i < n_elem ; i++) {
556 chown_advanced_but[i].text = _(chown_advanced_but[i].text);
557 i18n_len += str_term_width1 (chown_advanced_but[i].text) + 3;
558 if (DEFPUSH_BUTTON == chown_advanced_but[i].flags)
559 i18n_len += 2; /* "<>" */
561 cx = dx = (dlg_w - i18n_len - 2) / (n_elem + 1);
563 /* Reversed order */
564 for (i = n_elem - 1; i >= 0; i--) {
565 chown_advanced_but[i].x = cx;
566 cx += str_term_width1 (chown_advanced_but[i].text) + 3 + dx;
569 #endif /* ENABLE_NLS */
571 sf_stat = g_new (struct stat, 1);
572 do_refresh ();
573 end_chown = need_update = current_file = 0;
574 single_set = (current_panel->marked < 2) ? 2 : 0;
575 memset (ch_flags, '=', 11);
576 flag_pos = 0;
577 x_toggle = 070;
579 ch_dlg =
580 create_dlg (0, 0, dlg_h, dlg_w, dialog_colors, advanced_chown_callback,
581 "[Advanced Chown]", _(" Chown advanced command "),
582 DLG_CENTER | DLG_REVERSE);
584 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
585 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
586 (chown_advanced_but[i].text), 0
588 for (i = 0; i < BUTTONS - 5; i++)
589 if (!single_set || i < 2)
590 add_widget (ch_dlg, button_new (XTRACT (i)));
592 b_att[0] = button_new (XTRACT (8));
593 b_att[1] = button_new (XTRACT (7));
594 b_att[2] = button_new (XTRACT (6));
595 b_user = button_new (XTRACT (5));
596 b_group = button_new (XTRACT (4));
598 add_widget (ch_dlg, b_group);
599 add_widget (ch_dlg, b_user);
600 add_widget (ch_dlg, b_att[2]);
601 add_widget (ch_dlg, b_att[1]);
602 add_widget (ch_dlg, b_att[0]);
605 static void
606 chown_advanced_done (void)
608 g_free (sf_stat);
609 if (need_update)
610 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
611 repaint_screen ();
614 #if 0
615 static void do_chown (uid_t u, gid_t g)
617 chown (current_panel->dir.list[current_file].fname, u, g);
618 file_mark (current_panel, current_file, 0);
620 #endif
622 static char *next_file (void)
624 while (!current_panel->dir.list[current_file].f.marked)
625 current_file++;
627 return current_panel->dir.list[current_file].fname;
630 static void apply_advanced_chowns (struct stat *sf)
632 char *lc_fname;
633 gid_t a_gid = sf->st_gid;
634 uid_t a_uid = sf->st_uid;
636 lc_fname = current_panel->dir.list[current_file].fname;
637 need_update = end_chown = 1;
638 if (mc_chmod (lc_fname, get_mode ()) == -1)
639 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
640 lc_fname, unix_error_string (errno));
641 /* call mc_chown only, if mc_chmod didn't fail */
642 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? sf->st_uid : (uid_t) -1,
643 (ch_flags[10] == '+') ? sf->st_gid : (gid_t) -1) == -1)
644 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
645 lc_fname, unix_error_string (errno));
646 do_file_mark (current_panel, current_file, 0);
648 do {
649 lc_fname = next_file ();
651 if (mc_stat (lc_fname, sf) != 0)
652 break;
653 ch_cmode = sf->st_mode;
654 if (mc_chmod (lc_fname, get_mode ()) == -1)
655 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
656 lc_fname, unix_error_string (errno));
657 /* call mc_chown only, if mc_chmod didn't fail */
658 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? a_uid : (uid_t) -1,
659 (ch_flags[10] == '+') ? a_gid : (gid_t) -1) == -1)
660 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
661 lc_fname, unix_error_string (errno));
663 do_file_mark (current_panel, current_file, 0);
664 } while (current_panel->marked);
667 void
668 chown_advanced_cmd (void)
671 files_on_begin = current_panel->marked;
673 do { /* do while any files remaining */
674 init_chown_advanced ();
676 if (current_panel->marked)
677 fname = next_file (); /* next marked file */
678 else
679 fname = selection (current_panel)->fname; /* single file */
681 if (mc_stat (fname, sf_stat) != 0) { /* get status of file */
682 destroy_dlg (ch_dlg);
683 break;
685 ch_cmode = sf_stat->st_mode;
687 chown_refresh ();
689 update_ownership ();
691 /* game can begin */
692 run_dlg (ch_dlg);
694 switch (ch_dlg->ret_value) {
695 case B_CANCEL:
696 end_chown = 1;
697 break;
699 case B_ENTER:
700 need_update = 1;
701 if (mc_chmod (fname, get_mode ()) == -1)
702 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
703 fname, unix_error_string (errno));
704 /* call mc_chown only, if mc_chmod didn't fail */
705 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf_stat->st_uid : (uid_t) -1,
706 (ch_flags[10] == '+') ? sf_stat->st_gid : (gid_t) -1) == -1)
707 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
708 fname, unix_error_string (errno));
709 break;
710 case B_SETALL:
711 apply_advanced_chowns (sf_stat);
712 break;
714 case B_SKIP:
715 break;
719 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL) {
720 do_file_mark (current_panel, current_file, 0);
721 need_update = 1;
723 destroy_dlg (ch_dlg);
724 } while (current_panel->marked && !end_chown);
726 chown_advanced_done ();