Ticket #2229: speed up of up/down moving in viewer in wrapped mode.
[midnight-commander.git] / src / achown.c
blobbaa02ad89178ffea76ec65f21bf9977872e24fb4
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/vfs/mc-vfs/vfs.h"
42 #include "lib/strutil.h"
44 #include "dialog.h"
45 #include "widget.h"
46 #include "wtools.h" /* For init_box_colors() */
48 #include "dir.h"
49 #include "panel.h" /* Needed for the externs */
50 #include "chmod.h"
51 #include "main.h" /* update_panels() */
52 #include "layout.h" /* repaint_screen() */
53 #include "achown.h"
55 #define BX 5
56 #define BY 6
58 #define TX 50
59 #define TY 2
61 #define BUTTONS 9
63 #define B_SETALL B_USER
64 #define B_SKIP (B_USER + 1)
66 #define B_OWN (B_USER + 3)
67 #define B_GRP (B_USER + 4)
68 #define B_OTH (B_USER + 5)
69 #define B_OUSER (B_USER + 6)
70 #define B_OGROUP (B_USER + 7)
72 static struct Dlg_head *ch_dlg;
74 static struct
76 int ret_cmd, flags, y, x;
77 const char *text;
78 } chown_advanced_but[BUTTONS] =
80 /* *INDENT-OFF* */
81 { B_CANCEL, NORMAL_BUTTON, 4, 53, N_("&Cancel") },
82 { B_ENTER, DEFPUSH_BUTTON,4, 40, N_("&Set") },
83 { B_SKIP, NORMAL_BUTTON, 4, 23, N_("S&kip") },
84 { B_SETALL, NORMAL_BUTTON, 4, 0, N_("Set &all")},
85 { B_ENTER, NARROW_BUTTON, 0, 47, ""},
86 { B_ENTER, NARROW_BUTTON, 0, 29, ""},
87 { B_ENTER, NARROW_BUTTON, 0, 19, " "},
88 { B_ENTER, NARROW_BUTTON, 0, 11, " "},
89 { B_ENTER, NARROW_BUTTON, 0, 3, " "}
90 /* *INDENT-ON* */
93 static WButton *b_att[3]; /* permission */
94 static WButton *b_user, *b_group; /* owner */
96 static int files_on_begin; /* Number of files at startup */
97 static int flag_pos;
98 static int x_toggle;
99 static char ch_flags[11];
100 static const char ch_perm[] = "rwx";
101 static mode_t ch_cmode;
102 static struct stat *sf_stat;
103 static int need_update;
104 static int end_chown;
105 static int current_file;
106 static int single_set;
107 static char *fname;
109 static void
110 update_ownership (void)
112 button_set_text (b_user, get_owner (sf_stat->st_uid));
113 button_set_text (b_group, get_group (sf_stat->st_gid));
117 static cb_ret_t
118 inc_flag_pos (int f_pos)
120 if (flag_pos == 10)
122 flag_pos = 0;
123 return MSG_NOT_HANDLED;
125 flag_pos++;
126 if (!(flag_pos % 3) || f_pos > 2)
127 return MSG_NOT_HANDLED;
128 return MSG_HANDLED;
131 static cb_ret_t
132 dec_flag_pos (int f_pos)
134 if (!flag_pos)
136 flag_pos = 10;
137 return MSG_NOT_HANDLED;
139 flag_pos--;
140 if (!((flag_pos + 1) % 3) || f_pos > 2)
141 return MSG_NOT_HANDLED;
142 return MSG_HANDLED;
145 static void
146 set_perm_by_flags (char *s, int f_p)
148 int i;
150 for (i = 0; i < 3; i++)
152 if (ch_flags[f_p + i] == '+')
153 s[i] = ch_perm[i];
154 else if (ch_flags[f_p + i] == '-')
155 s[i] = '-';
156 else
157 s[i] = (ch_cmode & (1 << (8 - f_p - i))) ? ch_perm[i] : '-';
161 static void
162 update_permissions (void)
164 set_perm_by_flags (b_att[0]->text.start, 0);
165 set_perm_by_flags (b_att[1]->text.start, 3);
166 set_perm_by_flags (b_att[2]->text.start, 6);
169 static mode_t
170 get_perm (char *s, int base)
172 mode_t m;
174 m = 0;
175 m |= (s[0] == '-') ? 0 :
176 ((s[0] == '+') ? (mode_t) (1 << (base + 2)) : (1 << (base + 2)) & ch_cmode);
178 m |= (s[1] == '-') ? 0 :
179 ((s[1] == '+') ? (mode_t) (1 << (base + 1)) : (1 << (base + 1)) & ch_cmode);
181 m |= (s[2] == '-') ? 0 : ((s[2] == '+') ? (mode_t) (1 << base) : (1 << base) & ch_cmode);
183 return m;
186 static mode_t
187 get_mode (void)
189 mode_t m;
191 m = ch_cmode ^ (ch_cmode & 0777);
192 m |= get_perm (ch_flags, 6);
193 m |= get_perm (ch_flags + 3, 3);
194 m |= get_perm (ch_flags + 6, 0);
196 return m;
199 static void
200 print_flags (void)
202 int i;
204 tty_setcolor (COLOR_NORMAL);
206 for (i = 0; i < 3; i++)
208 dlg_move (ch_dlg, BY + 1, 9 + i);
209 tty_print_char (ch_flags[i]);
212 for (i = 0; i < 3; i++)
214 dlg_move (ch_dlg, BY + 1, 17 + i);
215 tty_print_char (ch_flags[i + 3]);
218 for (i = 0; i < 3; i++)
220 dlg_move (ch_dlg, BY + 1, 25 + i);
221 tty_print_char (ch_flags[i + 6]);
224 update_permissions ();
226 for (i = 0; i < 15; i++)
228 dlg_move (ch_dlg, BY + 1, 35 + i);
229 tty_print_char (ch_flags[9]);
231 for (i = 0; i < 15; i++)
233 dlg_move (ch_dlg, BY + 1, 53 + i);
234 tty_print_char (ch_flags[10]);
238 static void
239 update_mode (Dlg_head * h)
241 print_flags ();
242 tty_setcolor (COLOR_NORMAL);
243 dlg_move (h, BY + 2, 9);
244 tty_printf ("%12o", get_mode ());
245 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
248 static cb_ret_t
249 chl_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
251 switch (msg)
253 case DLG_KEY:
254 switch (parm)
256 case KEY_LEFT:
257 case KEY_RIGHT:
258 h->ret_value = parm;
259 dlg_stop (h);
262 default:
263 return default_dlg_callback (h, sender, msg, parm, data);
267 static void
268 do_enter_key (Dlg_head * h, int f_pos)
270 Dlg_head *chl_dlg;
271 WListbox *chl_list;
272 struct passwd *chl_pass;
273 struct group *chl_grp;
274 int fe;
275 int lxx, lyy, chl_end, b_pos;
276 int is_owner;
277 const char *title;
281 is_owner = (f_pos == 3);
282 title = is_owner ? _("owner") : _("group");
284 lxx = (COLS - 74) / 2 + (is_owner ? 35 : 53);
285 lyy = (LINES - 13) / 2;
286 chl_end = 0;
288 chl_dlg =
289 create_dlg (TRUE, lyy, lxx, 13, 17, dialog_colors, chl_callback,
290 "[Advanced Chown]", title, DLG_COMPACT | DLG_REVERSE);
292 /* get new listboxes */
293 chl_list = listbox_new (1, 1, 11, 15, FALSE, NULL);
295 listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0, "<Unknown>", NULL);
297 if (is_owner)
299 /* get and put user names in the listbox */
300 setpwent ();
301 while ((chl_pass = getpwent ()))
303 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0, chl_pass->pw_name, NULL);
305 endpwent ();
306 fe = listbox_search_text (chl_list, get_owner (sf_stat->st_uid));
308 else
310 /* get and put group names in the listbox */
311 setgrent ();
312 while ((chl_grp = getgrent ()))
314 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0, chl_grp->gr_name, NULL);
316 endgrent ();
317 fe = listbox_search_text (chl_list, get_group (sf_stat->st_gid));
320 listbox_select_entry (chl_list, fe);
322 b_pos = chl_list->pos;
323 add_widget (chl_dlg, chl_list);
325 run_dlg (chl_dlg);
327 if (b_pos != chl_list->pos)
329 int ok = 0;
330 char *text;
332 listbox_get_current (chl_list, &text, NULL);
333 if (is_owner)
335 chl_pass = getpwnam (text);
336 if (chl_pass)
338 ok = 1;
339 sf_stat->st_uid = chl_pass->pw_uid;
342 else
344 chl_grp = getgrnam (text);
345 if (chl_grp)
347 sf_stat->st_gid = chl_grp->gr_gid;
348 ok = 1;
351 if (ok)
353 ch_flags[f_pos + 6] = '+';
354 update_ownership ();
356 dlg_focus (h);
357 if (ok)
358 print_flags ();
360 if (chl_dlg->ret_value == KEY_LEFT)
362 if (!is_owner)
363 chl_end = 1;
364 dlg_one_up (ch_dlg);
365 f_pos--;
367 else if (chl_dlg->ret_value == KEY_RIGHT)
369 if (is_owner)
370 chl_end = 1;
371 dlg_one_down (ch_dlg);
372 f_pos++;
374 /* Here we used to redraw the window */
375 destroy_dlg (chl_dlg);
377 while (chl_end);
380 static void
381 chown_refresh (void)
383 common_dialog_repaint (ch_dlg);
385 tty_setcolor (COLOR_NORMAL);
387 dlg_move (ch_dlg, BY - 1, 8);
388 tty_print_string (_("owner"));
389 dlg_move (ch_dlg, BY - 1, 16);
390 tty_print_string (_("group"));
391 dlg_move (ch_dlg, BY - 1, 24);
392 tty_print_string (_("other"));
394 dlg_move (ch_dlg, BY - 1, 35);
395 tty_print_string (_("owner"));
396 dlg_move (ch_dlg, BY - 1, 53);
397 tty_print_string (_("group"));
399 dlg_move (ch_dlg, 3, 4);
400 tty_print_string (_("On"));
401 dlg_move (ch_dlg, BY + 1, 4);
402 tty_print_string (_("Flag"));
403 dlg_move (ch_dlg, BY + 2, 4);
404 tty_print_string (_("Mode"));
406 if (!single_set)
408 dlg_move (ch_dlg, 3, 54);
409 tty_printf (_("%6d of %d"), files_on_begin - (current_panel->marked) + 1, files_on_begin);
412 print_flags ();
415 static void
416 chown_info_update (void)
418 /* display file info */
419 tty_setcolor (COLOR_NORMAL);
421 /* name && mode */
422 dlg_move (ch_dlg, 3, 8);
423 tty_print_string (str_fit_to_term (fname, 45, J_LEFT_FIT));
424 dlg_move (ch_dlg, BY + 2, 9);
425 tty_printf ("%12o", get_mode ());
427 /* permissions */
428 update_permissions ();
431 static void
432 b_setpos (int f_pos)
434 b_att[0]->hotpos = -1;
435 b_att[1]->hotpos = -1;
436 b_att[2]->hotpos = -1;
437 b_att[f_pos]->hotpos = (flag_pos % 3);
440 static cb_ret_t
441 advanced_chown_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
443 int i = 0, f_pos = BUTTONS - dlg_get_current_widget_id (h) - single_set - 1;
445 switch (msg)
447 case DLG_DRAW:
448 chown_refresh ();
449 chown_info_update ();
450 return MSG_HANDLED;
452 case DLG_POST_KEY:
453 if (f_pos < 3)
454 b_setpos (f_pos);
455 return MSG_HANDLED;
457 case DLG_FOCUS:
458 if (f_pos < 3)
460 if ((flag_pos / 3) != f_pos)
461 flag_pos = f_pos * 3;
462 b_setpos (f_pos);
464 else if (f_pos < 5)
465 flag_pos = f_pos + 6;
466 return MSG_HANDLED;
468 case DLG_KEY:
469 switch (parm)
472 case XCTRL ('b'):
473 case KEY_LEFT:
474 if (f_pos < 5)
475 return (dec_flag_pos (f_pos));
476 break;
478 case XCTRL ('f'):
479 case KEY_RIGHT:
480 if (f_pos < 5)
481 return (inc_flag_pos (f_pos));
482 break;
484 case ' ':
485 if (f_pos < 3)
486 return MSG_HANDLED;
487 break;
489 case '\n':
490 case KEY_ENTER:
491 if (f_pos <= 2 || f_pos >= 5)
492 break;
493 do_enter_key (h, f_pos);
494 return MSG_HANDLED;
496 case ALT ('x'):
497 i++;
499 case ALT ('w'):
500 i++;
502 case ALT ('r'):
503 parm = i + 3;
504 for (i = 0; i < 3; i++)
505 ch_flags[i * 3 + parm - 3] = (x_toggle & (1 << parm)) ? '-' : '+';
506 x_toggle ^= (1 << parm);
507 update_mode (h);
508 dlg_broadcast_msg (h, WIDGET_DRAW, FALSE);
509 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
510 break;
512 case XCTRL ('x'):
513 i++;
515 case XCTRL ('w'):
516 i++;
518 case XCTRL ('r'):
519 parm = i;
520 for (i = 0; i < 3; i++)
521 ch_flags[i * 3 + parm] = (x_toggle & (1 << parm)) ? '-' : '+';
522 x_toggle ^= (1 << parm);
523 update_mode (h);
524 dlg_broadcast_msg (h, WIDGET_DRAW, FALSE);
525 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
526 break;
528 case 'x':
529 i++;
531 case 'w':
532 i++;
534 case 'r':
535 if (f_pos > 2)
536 break;
537 flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */
538 if (((WButton *) h->current->data)->text.start[(flag_pos % 3)] == '-')
539 ch_flags[flag_pos] = '+';
540 else
541 ch_flags[flag_pos] = '-';
542 update_mode (h);
543 break;
545 case '4':
546 i++;
548 case '2':
549 i++;
551 case '1':
552 if (f_pos > 2)
553 break;
554 flag_pos = i + f_pos * 3;
555 ch_flags[flag_pos] = '=';
556 update_mode (h);
557 break;
559 case '-':
560 if (f_pos > 2)
561 break;
563 case '*':
564 if (parm == '*')
565 parm = '=';
567 case '=':
568 case '+':
569 if (f_pos <= 4)
571 ch_flags[flag_pos] = parm;
572 update_mode (h);
573 advanced_chown_callback (h, sender, DLG_KEY, KEY_RIGHT, NULL);
574 if (flag_pos > 8 || !(flag_pos % 3))
575 dlg_one_down (h);
577 break;
579 return MSG_NOT_HANDLED;
581 default:
582 return default_dlg_callback (h, sender, msg, parm, data);
586 static void
587 init_chown_advanced (void)
589 int i;
590 enum
591 { dlg_h = 13, dlg_w = 74, n_elem = 4 };
592 #ifdef ENABLE_NLS
593 static int i18n_len = 0;
595 if (i18n_len == 0)
597 int dx, cx;
598 for (i = 0; i < n_elem; i++)
600 chown_advanced_but[i].text = _(chown_advanced_but[i].text);
601 i18n_len += str_term_width1 (chown_advanced_but[i].text) + 3;
602 if (DEFPUSH_BUTTON == chown_advanced_but[i].flags)
603 i18n_len += 2; /* "<>" */
605 cx = dx = (dlg_w - i18n_len - 2) / (n_elem + 1);
607 /* Reversed order */
608 for (i = n_elem - 1; i >= 0; i--)
610 chown_advanced_but[i].x = cx;
611 cx += str_term_width1 (chown_advanced_but[i].text) + 3 + dx;
614 #endif /* ENABLE_NLS */
616 sf_stat = g_new (struct stat, 1);
617 do_refresh ();
618 end_chown = need_update = current_file = 0;
619 single_set = (current_panel->marked < 2) ? 2 : 0;
620 memset (ch_flags, '=', 11);
621 flag_pos = 0;
622 x_toggle = 070;
624 ch_dlg =
625 create_dlg (TRUE, 0, 0, dlg_h, dlg_w, dialog_colors, advanced_chown_callback,
626 "[Advanced Chown]", _("Chown advanced command"), DLG_CENTER | DLG_REVERSE);
628 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
629 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
630 (chown_advanced_but[i].text), 0
632 for (i = 0; i < BUTTONS - 5; i++)
633 if (!single_set || i < 2)
634 add_widget (ch_dlg, button_new (XTRACT (i)));
636 b_att[0] = button_new (XTRACT (8));
637 b_att[1] = button_new (XTRACT (7));
638 b_att[2] = button_new (XTRACT (6));
639 b_user = button_new (XTRACT (5));
640 b_group = button_new (XTRACT (4));
642 add_widget (ch_dlg, b_group);
643 add_widget (ch_dlg, b_user);
644 add_widget (ch_dlg, b_att[2]);
645 add_widget (ch_dlg, b_att[1]);
646 add_widget (ch_dlg, b_att[0]);
649 static void
650 chown_advanced_done (void)
652 g_free (sf_stat);
653 if (need_update)
654 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
655 repaint_screen ();
658 #if 0
659 static void
660 do_chown (uid_t u, gid_t g)
662 chown (current_panel->dir.list[current_file].fname, u, g);
663 file_mark (current_panel, current_file, 0);
665 #endif
667 static char *
668 next_file (void)
670 while (!current_panel->dir.list[current_file].f.marked)
671 current_file++;
673 return current_panel->dir.list[current_file].fname;
676 static void
677 apply_advanced_chowns (struct stat *sf)
679 char *lc_fname;
680 gid_t a_gid = sf->st_gid;
681 uid_t a_uid = sf->st_uid;
683 lc_fname = current_panel->dir.list[current_file].fname;
684 need_update = end_chown = 1;
685 if (mc_chmod (lc_fname, get_mode ()) == -1)
686 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
687 lc_fname, unix_error_string (errno));
688 /* call mc_chown only, if mc_chmod didn't fail */
689 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? sf->st_uid : (uid_t) - 1,
690 (ch_flags[10] == '+') ? sf->st_gid : (gid_t) - 1) == -1)
691 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
692 lc_fname, unix_error_string (errno));
693 do_file_mark (current_panel, current_file, 0);
697 lc_fname = next_file ();
699 if (mc_stat (lc_fname, sf) != 0)
700 break;
701 ch_cmode = sf->st_mode;
702 if (mc_chmod (lc_fname, get_mode ()) == -1)
703 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
704 lc_fname, unix_error_string (errno));
705 /* call mc_chown only, if mc_chmod didn't fail */
706 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? a_uid : (uid_t) - 1,
707 (ch_flags[10] == '+') ? a_gid : (gid_t) - 1) == -1)
708 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
709 lc_fname, unix_error_string (errno));
711 do_file_mark (current_panel, current_file, 0);
713 while (current_panel->marked);
716 void
717 chown_advanced_cmd (void)
720 files_on_begin = current_panel->marked;
723 { /* do while any files remaining */
724 init_chown_advanced ();
726 if (current_panel->marked)
727 fname = next_file (); /* next marked file */
728 else
729 fname = selection (current_panel)->fname; /* single file */
731 if (mc_stat (fname, sf_stat) != 0)
732 { /* get status of file */
733 destroy_dlg (ch_dlg);
734 break;
736 ch_cmode = sf_stat->st_mode;
738 chown_refresh ();
740 update_ownership ();
742 /* game can begin */
743 run_dlg (ch_dlg);
745 switch (ch_dlg->ret_value)
747 case B_CANCEL:
748 end_chown = 1;
749 break;
751 case B_ENTER:
752 need_update = 1;
753 if (mc_chmod (fname, get_mode ()) == -1)
754 message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
755 fname, unix_error_string (errno));
756 /* call mc_chown only, if mc_chmod didn't fail */
757 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf_stat->st_uid : (uid_t) - 1,
758 (ch_flags[10] == '+') ? sf_stat->st_gid : (gid_t) - 1) == -1)
759 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
760 fname, unix_error_string (errno));
761 break;
762 case B_SETALL:
763 apply_advanced_chowns (sf_stat);
764 break;
766 case B_SKIP:
767 break;
771 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
773 do_file_mark (current_panel, current_file, 0);
774 need_update = 1;
776 destroy_dlg (ch_dlg);
778 while (current_panel->marked && !end_chown);
780 chown_advanced_done ();