Merge remote branch 'osp/only-directories' into homework
[pantumic.git] / src / filemanager / achown.c
blobf390c54461e392a84cce133192bcbde131786023
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 "lib/global.h"
38 #include "lib/tty/tty.h"
39 #include "lib/tty/key.h" /* XCTRL and ALT macros */
40 #include "lib/skin.h"
41 #include "lib/strutil.h"
42 #include "lib/vfs/mc-vfs/vfs.h"
43 #include "lib/util.h"
44 #include "lib/widget.h"
46 #include "dir.h"
47 #include "midnight.h" /* current_panel */
48 #include "chmod.h"
49 #include "layout.h" /* repaint_screen() */
51 #include "achown.h"
53 /*** global variables ****************************************************************************/
55 /*** file scope macro definitions ****************************************************************/
57 #define BX 5
58 #define BY 6
60 #define TX 50
61 #define TY 2
63 #define BUTTONS 9
65 #define B_SETALL B_USER
66 #define B_SKIP (B_USER + 1)
68 #define B_OWN (B_USER + 3)
69 #define B_GRP (B_USER + 4)
70 #define B_OTH (B_USER + 5)
71 #define B_OUSER (B_USER + 6)
72 #define B_OGROUP (B_USER + 7)
74 /*** file scope type declarations ****************************************************************/
76 /*** file scope variables ************************************************************************/
78 static struct Dlg_head *ch_dlg;
80 static struct
82 int ret_cmd, flags, y, x;
83 const char *text;
84 } chown_advanced_but[BUTTONS] =
86 /* *INDENT-OFF* */
87 { B_CANCEL, NORMAL_BUTTON, 4, 53, N_("&Cancel") },
88 { B_ENTER, DEFPUSH_BUTTON,4, 40, N_("&Set") },
89 { B_SKIP, NORMAL_BUTTON, 4, 23, N_("S&kip") },
90 { B_SETALL, NORMAL_BUTTON, 4, 0, N_("Set &all")},
91 { B_ENTER, NARROW_BUTTON, 0, 47, ""},
92 { B_ENTER, NARROW_BUTTON, 0, 29, ""},
93 { B_ENTER, NARROW_BUTTON, 0, 19, " "},
94 { B_ENTER, NARROW_BUTTON, 0, 11, " "},
95 { B_ENTER, NARROW_BUTTON, 0, 3, " "}
96 /* *INDENT-ON* */
99 static WButton *b_att[3]; /* permission */
100 static WButton *b_user, *b_group; /* owner */
102 static int files_on_begin; /* Number of files at startup */
103 static int flag_pos;
104 static int x_toggle;
105 static char ch_flags[11];
106 static const char ch_perm[] = "rwx";
107 static mode_t ch_cmode;
108 static struct stat *sf_stat;
109 static int need_update;
110 static int end_chown;
111 static int current_file;
112 static int single_set;
113 static char *fname;
115 /*** file scope functions ************************************************************************/
116 /* --------------------------------------------------------------------------------------------- */
118 static void
119 update_ownership (void)
121 button_set_text (b_user, get_owner (sf_stat->st_uid));
122 button_set_text (b_group, get_group (sf_stat->st_gid));
125 /* --------------------------------------------------------------------------------------------- */
127 static cb_ret_t
128 inc_flag_pos (int f_pos)
130 if (flag_pos == 10)
132 flag_pos = 0;
133 return MSG_NOT_HANDLED;
135 flag_pos++;
136 if (!(flag_pos % 3) || f_pos > 2)
137 return MSG_NOT_HANDLED;
138 return MSG_HANDLED;
141 /* --------------------------------------------------------------------------------------------- */
143 static cb_ret_t
144 dec_flag_pos (int f_pos)
146 if (!flag_pos)
148 flag_pos = 10;
149 return MSG_NOT_HANDLED;
151 flag_pos--;
152 if (!((flag_pos + 1) % 3) || f_pos > 2)
153 return MSG_NOT_HANDLED;
154 return MSG_HANDLED;
157 /* --------------------------------------------------------------------------------------------- */
159 static void
160 set_perm_by_flags (char *s, int f_p)
162 int i;
164 for (i = 0; i < 3; i++)
166 if (ch_flags[f_p + i] == '+')
167 s[i] = ch_perm[i];
168 else if (ch_flags[f_p + i] == '-')
169 s[i] = '-';
170 else
171 s[i] = (ch_cmode & (1 << (8 - f_p - i))) ? ch_perm[i] : '-';
175 /* --------------------------------------------------------------------------------------------- */
177 static void
178 update_permissions (void)
180 set_perm_by_flags (b_att[0]->text.start, 0);
181 set_perm_by_flags (b_att[1]->text.start, 3);
182 set_perm_by_flags (b_att[2]->text.start, 6);
185 /* --------------------------------------------------------------------------------------------- */
187 static mode_t
188 get_perm (char *s, int base)
190 mode_t m;
192 m = 0;
193 m |= (s[0] == '-') ? 0 :
194 ((s[0] == '+') ? (mode_t) (1 << (base + 2)) : (1 << (base + 2)) & ch_cmode);
196 m |= (s[1] == '-') ? 0 :
197 ((s[1] == '+') ? (mode_t) (1 << (base + 1)) : (1 << (base + 1)) & ch_cmode);
199 m |= (s[2] == '-') ? 0 : ((s[2] == '+') ? (mode_t) (1 << base) : (1 << base) & ch_cmode);
201 return m;
204 /* --------------------------------------------------------------------------------------------- */
206 static mode_t
207 get_mode (void)
209 mode_t m;
211 m = ch_cmode ^ (ch_cmode & 0777);
212 m |= get_perm (ch_flags, 6);
213 m |= get_perm (ch_flags + 3, 3);
214 m |= get_perm (ch_flags + 6, 0);
216 return m;
219 /* --------------------------------------------------------------------------------------------- */
221 static void
222 print_flags (void)
224 int i;
226 tty_setcolor (COLOR_NORMAL);
228 for (i = 0; i < 3; i++)
230 dlg_move (ch_dlg, BY + 1, 9 + i);
231 tty_print_char (ch_flags[i]);
234 for (i = 0; i < 3; i++)
236 dlg_move (ch_dlg, BY + 1, 17 + i);
237 tty_print_char (ch_flags[i + 3]);
240 for (i = 0; i < 3; i++)
242 dlg_move (ch_dlg, BY + 1, 25 + i);
243 tty_print_char (ch_flags[i + 6]);
246 update_permissions ();
248 for (i = 0; i < 15; i++)
250 dlg_move (ch_dlg, BY + 1, 35 + i);
251 tty_print_char (ch_flags[9]);
253 for (i = 0; i < 15; i++)
255 dlg_move (ch_dlg, BY + 1, 53 + i);
256 tty_print_char (ch_flags[10]);
260 /* --------------------------------------------------------------------------------------------- */
262 static void
263 update_mode (Dlg_head * h)
265 print_flags ();
266 tty_setcolor (COLOR_NORMAL);
267 dlg_move (h, BY + 2, 9);
268 tty_printf ("%12o", get_mode ());
269 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
272 /* --------------------------------------------------------------------------------------------- */
274 static cb_ret_t
275 chl_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
277 switch (msg)
279 case DLG_KEY:
280 switch (parm)
282 case KEY_LEFT:
283 case KEY_RIGHT:
284 h->ret_value = parm;
285 dlg_stop (h);
288 default:
289 return default_dlg_callback (h, sender, msg, parm, data);
293 /* --------------------------------------------------------------------------------------------- */
295 static void
296 do_enter_key (Dlg_head * h, int f_pos)
298 Dlg_head *chl_dlg;
299 WListbox *chl_list;
300 struct passwd *chl_pass;
301 struct group *chl_grp;
302 int fe;
303 int lxx, lyy, chl_end, b_pos;
304 int is_owner;
305 const char *title;
309 is_owner = (f_pos == 3);
310 title = is_owner ? _("owner") : _("group");
312 lxx = (COLS - 74) / 2 + (is_owner ? 35 : 53);
313 lyy = (LINES - 13) / 2;
314 chl_end = 0;
316 chl_dlg =
317 create_dlg (TRUE, lyy, lxx, 13, 17, dialog_colors, chl_callback,
318 "[Advanced Chown]", title, DLG_COMPACT | DLG_REVERSE);
320 /* get new listboxes */
321 chl_list = listbox_new (1, 1, 11, 15, FALSE, NULL);
323 listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0, "<Unknown>", NULL);
325 if (is_owner)
327 /* get and put user names in the listbox */
328 setpwent ();
329 while ((chl_pass = getpwent ()))
331 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0, chl_pass->pw_name, NULL);
333 endpwent ();
334 fe = listbox_search_text (chl_list, get_owner (sf_stat->st_uid));
336 else
338 /* get and put group names in the listbox */
339 setgrent ();
340 while ((chl_grp = getgrent ()))
342 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0, chl_grp->gr_name, NULL);
344 endgrent ();
345 fe = listbox_search_text (chl_list, get_group (sf_stat->st_gid));
348 listbox_select_entry (chl_list, fe);
350 b_pos = chl_list->pos;
351 add_widget (chl_dlg, chl_list);
353 run_dlg (chl_dlg);
355 if (b_pos != chl_list->pos)
357 int ok = 0;
358 char *text;
360 listbox_get_current (chl_list, &text, NULL);
361 if (is_owner)
363 chl_pass = getpwnam (text);
364 if (chl_pass)
366 ok = 1;
367 sf_stat->st_uid = chl_pass->pw_uid;
370 else
372 chl_grp = getgrnam (text);
373 if (chl_grp)
375 sf_stat->st_gid = chl_grp->gr_gid;
376 ok = 1;
379 if (ok)
381 ch_flags[f_pos + 6] = '+';
382 update_ownership ();
384 dlg_focus (h);
385 if (ok)
386 print_flags ();
388 if (chl_dlg->ret_value == KEY_LEFT)
390 if (!is_owner)
391 chl_end = 1;
392 dlg_one_up (ch_dlg);
393 f_pos--;
395 else if (chl_dlg->ret_value == KEY_RIGHT)
397 if (is_owner)
398 chl_end = 1;
399 dlg_one_down (ch_dlg);
400 f_pos++;
402 /* Here we used to redraw the window */
403 destroy_dlg (chl_dlg);
405 while (chl_end);
408 /* --------------------------------------------------------------------------------------------- */
410 static void
411 chown_refresh (void)
413 common_dialog_repaint (ch_dlg);
415 tty_setcolor (COLOR_NORMAL);
417 dlg_move (ch_dlg, BY - 1, 8);
418 tty_print_string (_("owner"));
419 dlg_move (ch_dlg, BY - 1, 16);
420 tty_print_string (_("group"));
421 dlg_move (ch_dlg, BY - 1, 24);
422 tty_print_string (_("other"));
424 dlg_move (ch_dlg, BY - 1, 35);
425 tty_print_string (_("owner"));
426 dlg_move (ch_dlg, BY - 1, 53);
427 tty_print_string (_("group"));
429 dlg_move (ch_dlg, 3, 4);
430 tty_print_string (_("On"));
431 dlg_move (ch_dlg, BY + 1, 4);
432 tty_print_string (_("Flag"));
433 dlg_move (ch_dlg, BY + 2, 4);
434 tty_print_string (_("Mode"));
436 if (!single_set)
438 dlg_move (ch_dlg, 3, 54);
439 tty_printf (_("%6d of %d"), files_on_begin - (current_panel->marked) + 1, files_on_begin);
442 print_flags ();
445 /* --------------------------------------------------------------------------------------------- */
447 static void
448 chown_info_update (void)
450 /* display file info */
451 tty_setcolor (COLOR_NORMAL);
453 /* name && mode */
454 dlg_move (ch_dlg, 3, 8);
455 tty_print_string (str_fit_to_term (fname, 45, J_LEFT_FIT));
456 dlg_move (ch_dlg, BY + 2, 9);
457 tty_printf ("%12o", get_mode ());
459 /* permissions */
460 update_permissions ();
463 /* --------------------------------------------------------------------------------------------- */
465 static void
466 b_setpos (int f_pos)
468 b_att[0]->hotpos = -1;
469 b_att[1]->hotpos = -1;
470 b_att[2]->hotpos = -1;
471 b_att[f_pos]->hotpos = (flag_pos % 3);
474 /* --------------------------------------------------------------------------------------------- */
476 static cb_ret_t
477 advanced_chown_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
479 int i = 0, f_pos = BUTTONS - dlg_get_current_widget_id (h) - single_set - 1;
481 switch (msg)
483 case DLG_DRAW:
484 chown_refresh ();
485 chown_info_update ();
486 return MSG_HANDLED;
488 case DLG_POST_KEY:
489 if (f_pos < 3)
490 b_setpos (f_pos);
491 return MSG_HANDLED;
493 case DLG_FOCUS:
494 if (f_pos < 3)
496 if ((flag_pos / 3) != f_pos)
497 flag_pos = f_pos * 3;
498 b_setpos (f_pos);
500 else if (f_pos < 5)
501 flag_pos = f_pos + 6;
502 return MSG_HANDLED;
504 case DLG_KEY:
505 switch (parm)
508 case XCTRL ('b'):
509 case KEY_LEFT:
510 if (f_pos < 5)
511 return (dec_flag_pos (f_pos));
512 break;
514 case XCTRL ('f'):
515 case KEY_RIGHT:
516 if (f_pos < 5)
517 return (inc_flag_pos (f_pos));
518 break;
520 case ' ':
521 if (f_pos < 3)
522 return MSG_HANDLED;
523 break;
525 case '\n':
526 case KEY_ENTER:
527 if (f_pos <= 2 || f_pos >= 5)
528 break;
529 do_enter_key (h, f_pos);
530 return MSG_HANDLED;
532 case ALT ('x'):
533 i++;
535 case ALT ('w'):
536 i++;
538 case ALT ('r'):
539 parm = i + 3;
540 for (i = 0; i < 3; i++)
541 ch_flags[i * 3 + parm - 3] = (x_toggle & (1 << parm)) ? '-' : '+';
542 x_toggle ^= (1 << parm);
543 update_mode (h);
544 dlg_broadcast_msg (h, WIDGET_DRAW, FALSE);
545 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
546 break;
548 case XCTRL ('x'):
549 i++;
551 case XCTRL ('w'):
552 i++;
554 case XCTRL ('r'):
555 parm = i;
556 for (i = 0; i < 3; i++)
557 ch_flags[i * 3 + parm] = (x_toggle & (1 << parm)) ? '-' : '+';
558 x_toggle ^= (1 << parm);
559 update_mode (h);
560 dlg_broadcast_msg (h, WIDGET_DRAW, FALSE);
561 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
562 break;
564 case 'x':
565 i++;
567 case 'w':
568 i++;
570 case 'r':
571 if (f_pos > 2)
572 break;
573 flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */
574 if (((WButton *) h->current->data)->text.start[(flag_pos % 3)] == '-')
575 ch_flags[flag_pos] = '+';
576 else
577 ch_flags[flag_pos] = '-';
578 update_mode (h);
579 break;
581 case '4':
582 i++;
584 case '2':
585 i++;
587 case '1':
588 if (f_pos > 2)
589 break;
590 flag_pos = i + f_pos * 3;
591 ch_flags[flag_pos] = '=';
592 update_mode (h);
593 break;
595 case '-':
596 if (f_pos > 2)
597 break;
599 case '*':
600 if (parm == '*')
601 parm = '=';
603 case '=':
604 case '+':
605 if (f_pos <= 4)
607 ch_flags[flag_pos] = parm;
608 update_mode (h);
609 advanced_chown_callback (h, sender, DLG_KEY, KEY_RIGHT, NULL);
610 if (flag_pos > 8 || !(flag_pos % 3))
611 dlg_one_down (h);
613 break;
615 return MSG_NOT_HANDLED;
617 default:
618 return default_dlg_callback (h, sender, msg, parm, data);
622 /* --------------------------------------------------------------------------------------------- */
624 static void
625 init_chown_advanced (void)
627 int i;
628 enum
629 { dlg_h = 13, dlg_w = 74, n_elem = 4 };
630 #ifdef ENABLE_NLS
631 static int i18n_len = 0;
633 if (i18n_len == 0)
635 int dx, cx;
636 for (i = 0; i < n_elem; i++)
638 chown_advanced_but[i].text = _(chown_advanced_but[i].text);
639 i18n_len += str_term_width1 (chown_advanced_but[i].text) + 3;
640 if (DEFPUSH_BUTTON == chown_advanced_but[i].flags)
641 i18n_len += 2; /* "<>" */
643 cx = dx = (dlg_w - i18n_len - 2) / (n_elem + 1);
645 /* Reversed order */
646 for (i = n_elem - 1; i >= 0; i--)
648 chown_advanced_but[i].x = cx;
649 cx += str_term_width1 (chown_advanced_but[i].text) + 3 + dx;
652 #endif /* ENABLE_NLS */
654 sf_stat = g_new (struct stat, 1);
655 do_refresh ();
656 end_chown = need_update = current_file = 0;
657 single_set = (current_panel->marked < 2) ? 2 : 0;
658 memset (ch_flags, '=', 11);
659 flag_pos = 0;
660 x_toggle = 070;
662 ch_dlg =
663 create_dlg (TRUE, 0, 0, dlg_h, dlg_w, dialog_colors, advanced_chown_callback,
664 "[Advanced Chown]", _("Chown advanced command"), DLG_CENTER | DLG_REVERSE);
666 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
667 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
668 (chown_advanced_but[i].text), 0
670 for (i = 0; i < BUTTONS - 5; i++)
671 if (!single_set || i < 2)
672 add_widget (ch_dlg, button_new (XTRACT (i)));
674 b_att[0] = button_new (XTRACT (8));
675 b_att[1] = button_new (XTRACT (7));
676 b_att[2] = button_new (XTRACT (6));
677 b_user = button_new (XTRACT (5));
678 b_group = button_new (XTRACT (4));
680 add_widget (ch_dlg, b_group);
681 add_widget (ch_dlg, b_user);
682 add_widget (ch_dlg, b_att[2]);
683 add_widget (ch_dlg, b_att[1]);
684 add_widget (ch_dlg, b_att[0]);
687 /* --------------------------------------------------------------------------------------------- */
689 static void
690 chown_advanced_done (void)
692 g_free (sf_stat);
693 if (need_update)
694 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
695 repaint_screen ();
698 /* --------------------------------------------------------------------------------------------- */
700 #if 0
701 static void
702 do_chown (uid_t u, gid_t g)
704 chown (current_panel->dir.list[current_file].fname, u, g);
705 file_mark (current_panel, current_file, 0);
707 #endif
709 /* --------------------------------------------------------------------------------------------- */
711 static char *
712 next_file (void)
714 while (!current_panel->dir.list[current_file].f.marked)
715 current_file++;
717 return current_panel->dir.list[current_file].fname;
720 /* --------------------------------------------------------------------------------------------- */
722 static void
723 apply_advanced_chowns (struct stat *sf)
725 char *lc_fname;
726 gid_t a_gid = sf->st_gid;
727 uid_t a_uid = sf->st_uid;
729 lc_fname = current_panel->dir.list[current_file].fname;
730 need_update = end_chown = 1;
731 if (mc_chmod (lc_fname, get_mode ()) == -1)
732 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
733 lc_fname, unix_error_string (errno));
734 /* call mc_chown only, if mc_chmod didn't fail */
735 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? sf->st_uid : (uid_t) - 1,
736 (ch_flags[10] == '+') ? sf->st_gid : (gid_t) - 1) == -1)
737 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
738 lc_fname, unix_error_string (errno));
739 do_file_mark (current_panel, current_file, 0);
743 lc_fname = next_file ();
745 if (mc_stat (lc_fname, sf) != 0)
746 break;
747 ch_cmode = sf->st_mode;
748 if (mc_chmod (lc_fname, get_mode ()) == -1)
749 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
750 lc_fname, unix_error_string (errno));
751 /* call mc_chown only, if mc_chmod didn't fail */
752 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? a_uid : (uid_t) - 1,
753 (ch_flags[10] == '+') ? a_gid : (gid_t) - 1) == -1)
754 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
755 lc_fname, unix_error_string (errno));
757 do_file_mark (current_panel, current_file, 0);
759 while (current_panel->marked);
762 /* --------------------------------------------------------------------------------------------- */
763 /*** public functions ****************************************************************************/
764 /* --------------------------------------------------------------------------------------------- */
766 void
767 chown_advanced_cmd (void)
770 files_on_begin = current_panel->marked;
773 { /* do while any files remaining */
774 init_chown_advanced ();
776 if (current_panel->marked)
777 fname = next_file (); /* next marked file */
778 else
779 fname = selection (current_panel)->fname; /* single file */
781 if (mc_stat (fname, sf_stat) != 0)
782 { /* get status of file */
783 destroy_dlg (ch_dlg);
784 break;
786 ch_cmode = sf_stat->st_mode;
788 chown_refresh ();
790 update_ownership ();
792 /* game can begin */
793 run_dlg (ch_dlg);
795 switch (ch_dlg->ret_value)
797 case B_CANCEL:
798 end_chown = 1;
799 break;
801 case B_ENTER:
802 need_update = 1;
803 if (mc_chmod (fname, get_mode ()) == -1)
804 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
805 fname, unix_error_string (errno));
806 /* call mc_chown only, if mc_chmod didn't fail */
807 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf_stat->st_uid : (uid_t) - 1,
808 (ch_flags[10] == '+') ? sf_stat->st_gid : (gid_t) - 1) == -1)
809 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
810 fname, unix_error_string (errno));
811 break;
812 case B_SETALL:
813 apply_advanced_chowns (sf_stat);
814 break;
816 case B_SKIP:
817 break;
821 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
823 do_file_mark (current_panel, current_file, 0);
824 need_update = 1;
826 destroy_dlg (ch_dlg);
828 while (current_panel->marked && !end_chown);
830 chown_advanced_done ();
833 /* --------------------------------------------------------------------------------------------- */