Merge branch '2121_dir_symlink'
[kaloumi3.git] / src / achown.c
blobef6bfefebff621a38afcbae1720e414223389c6f
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 {
75 int ret_cmd, flags, y, x;
76 const char *text;
77 } chown_advanced_but [BUTTONS] = {
78 { B_CANCEL, NORMAL_BUTTON, 4, 53, N_("&Cancel") },
79 { B_ENTER, DEFPUSH_BUTTON,4, 40, N_("&Set") },
80 { B_SKIP, NORMAL_BUTTON, 4, 23, N_("S&kip") },
81 { B_SETALL, NORMAL_BUTTON, 4, 0, N_("Set &all")},
82 { B_ENTER, NARROW_BUTTON, 0, 47, ""},
83 { B_ENTER, NARROW_BUTTON, 0, 29, ""},
84 { B_ENTER, NARROW_BUTTON, 0, 19, " "},
85 { B_ENTER, NARROW_BUTTON, 0, 11, " "},
86 { B_ENTER, NARROW_BUTTON, 0, 3, " "}
89 static WButton *b_att[3]; /* permission */
90 static WButton *b_user, *b_group; /* owner */
92 static int files_on_begin; /* Number of files at startup */
93 static int flag_pos;
94 static int x_toggle;
95 static char ch_flags[11];
96 static const char ch_perm[] = "rwx";
97 static mode_t ch_cmode;
98 static struct stat *sf_stat;
99 static int need_update;
100 static int end_chown;
101 static int current_file;
102 static int single_set;
103 static char *fname;
105 static void update_ownership (void)
107 button_set_text (b_user, get_owner (sf_stat->st_uid));
108 button_set_text (b_group, get_group (sf_stat->st_gid));
112 static cb_ret_t inc_flag_pos (int f_pos)
114 if (flag_pos == 10) {
115 flag_pos = 0;
116 return MSG_NOT_HANDLED;
118 flag_pos++;
119 if (!(flag_pos % 3) || f_pos > 2)
120 return MSG_NOT_HANDLED;
121 return MSG_HANDLED;
124 static cb_ret_t dec_flag_pos (int f_pos)
126 if (!flag_pos) {
127 flag_pos = 10;
128 return MSG_NOT_HANDLED;
130 flag_pos--;
131 if (!((flag_pos + 1) % 3) || f_pos > 2)
132 return MSG_NOT_HANDLED;
133 return MSG_HANDLED;
136 static void set_perm_by_flags (char *s, int f_p)
138 int i;
140 for (i = 0; i < 3; i++) {
141 if (ch_flags[f_p + i] == '+')
142 s[i] = ch_perm[i];
143 else if (ch_flags[f_p + i] == '-')
144 s[i] = '-';
145 else
146 s[i] = (ch_cmode & (1 << (8 - f_p - i))) ? ch_perm[i] : '-';
150 static void update_permissions (void)
152 set_perm_by_flags (b_att[0]->text.start, 0);
153 set_perm_by_flags (b_att[1]->text.start, 3);
154 set_perm_by_flags (b_att[2]->text.start, 6);
157 static mode_t get_perm (char *s, int base)
159 mode_t m;
161 m = 0;
162 m |= (s[0] == '-') ? 0 :
163 ((s[0] == '+') ? (mode_t)(1 << (base + 2)) : (1 << (base + 2)) & ch_cmode);
165 m |= (s[1] == '-') ? 0 :
166 ((s[1] == '+') ? (mode_t)(1 << (base + 1)) : (1 << (base + 1)) & ch_cmode);
168 m |= (s[2] == '-') ? 0 :
169 ((s[2] == '+') ? (mode_t)(1 << base) : (1 << base) & ch_cmode);
171 return m;
174 static mode_t get_mode (void)
176 mode_t m;
178 m = ch_cmode ^ (ch_cmode & 0777);
179 m |= get_perm (ch_flags, 6);
180 m |= get_perm (ch_flags + 3, 3);
181 m |= get_perm (ch_flags + 6, 0);
183 return m;
186 static void print_flags (void)
188 int i;
190 tty_setcolor (COLOR_NORMAL);
192 for (i = 0; i < 3; i++){
193 dlg_move (ch_dlg, BY+1, 9+i);
194 tty_print_char (ch_flags [i]);
197 for (i = 0; i < 3; i++){
198 dlg_move (ch_dlg, BY + 1, 17 + i);
199 tty_print_char (ch_flags [i+3]);
202 for (i = 0; i < 3; i++){
203 dlg_move (ch_dlg, BY + 1, 25 + i);
204 tty_print_char (ch_flags [i+6]);
207 update_permissions ();
209 for (i = 0; i < 15; i++){
210 dlg_move (ch_dlg, BY+1, 35+i);
211 tty_print_char (ch_flags[9]);
213 for (i = 0; i < 15; i++){
214 dlg_move (ch_dlg, BY + 1, 53 + i);
215 tty_print_char (ch_flags[10]);
219 static void update_mode (Dlg_head * h)
221 print_flags ();
222 tty_setcolor (COLOR_NORMAL);
223 dlg_move (h, BY + 2, 9);
224 tty_printf ("%12o", get_mode ());
225 send_message (h->current, WIDGET_FOCUS, 0);
228 static cb_ret_t
229 chl_callback (Dlg_head *h, Widget *sender,
230 dlg_msg_t msg, int parm, void *data)
232 switch (msg) {
233 case DLG_KEY:
234 switch (parm) {
235 case KEY_LEFT:
236 case KEY_RIGHT:
237 h->ret_value = parm;
238 dlg_stop (h);
241 default:
242 return default_dlg_callback (h, sender, msg, parm, data);
246 static void
247 do_enter_key (Dlg_head * h, int f_pos)
249 Dlg_head *chl_dlg;
250 WListbox *chl_list;
251 struct passwd *chl_pass;
252 struct group *chl_grp;
253 int fe;
254 int lxx, lyy, chl_end, b_pos;
255 int is_owner;
256 const char *title;
258 do {
259 is_owner = (f_pos == 3);
260 title = is_owner ? _("owner") : _("group");
262 lxx = (COLS - 74) / 2 + (is_owner ? 35 : 53);
263 lyy = (LINES - 13) / 2;
264 chl_end = 0;
266 chl_dlg =
267 create_dlg (lyy, lxx, 13, 17, dialog_colors, chl_callback,
268 "[Advanced Chown]", title, DLG_COMPACT | DLG_REVERSE);
270 /* get new listboxes */
271 chl_list = listbox_new (1, 1, 11, 15, FALSE, NULL);
273 listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0,
274 "<Unknown>", NULL);
276 if (is_owner) {
277 /* get and put user names in the listbox */
278 setpwent ();
279 while ((chl_pass = getpwent ())) {
280 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0,
281 chl_pass->pw_name, NULL);
283 endpwent ();
284 fe = listbox_search_text (chl_list,
285 get_owner (sf_stat->st_uid));
286 } else {
287 /* get and put group names in the listbox */
288 setgrent ();
289 while ((chl_grp = getgrent ())) {
290 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0,
291 chl_grp->gr_name, NULL);
293 endgrent ();
294 fe = listbox_search_text (chl_list,
295 get_group (sf_stat->st_gid));
298 listbox_select_entry (chl_list, fe);
300 b_pos = chl_list->pos;
301 add_widget (chl_dlg, chl_list);
303 run_dlg (chl_dlg);
305 if (b_pos != chl_list->pos) {
306 int ok = 0;
307 char *text;
309 listbox_get_current (chl_list, &text, NULL);
310 if (is_owner) {
311 chl_pass = getpwnam (text);
312 if (chl_pass) {
313 ok = 1;
314 sf_stat->st_uid = chl_pass->pw_uid;
316 } else {
317 chl_grp = getgrnam (text);
318 if (chl_grp) {
319 sf_stat->st_gid = chl_grp->gr_gid;
320 ok = 1;
323 if (ok) {
324 ch_flags[f_pos + 6] = '+';
325 update_ownership ();
327 dlg_focus (h);
328 if (ok)
329 print_flags ();
331 if (chl_dlg->ret_value == KEY_LEFT) {
332 if (!is_owner)
333 chl_end = 1;
334 dlg_one_up (ch_dlg);
335 f_pos--;
336 } else if (chl_dlg->ret_value == KEY_RIGHT) {
337 if (is_owner)
338 chl_end = 1;
339 dlg_one_down (ch_dlg);
340 f_pos++;
342 /* Here we used to redraw the window */
343 destroy_dlg (chl_dlg);
344 } while (chl_end);
347 static void chown_refresh (void)
349 common_dialog_repaint (ch_dlg);
351 tty_setcolor (COLOR_NORMAL);
353 dlg_move (ch_dlg, BY - 1, 8);
354 tty_print_string (_("owner"));
355 dlg_move (ch_dlg, BY - 1, 16);
356 tty_print_string (_("group"));
357 dlg_move (ch_dlg, BY - 1, 24);
358 tty_print_string (_("other"));
360 dlg_move (ch_dlg, BY - 1, 35);
361 tty_print_string (_("owner"));
362 dlg_move (ch_dlg, BY - 1, 53);
363 tty_print_string (_("group"));
365 dlg_move (ch_dlg, 3, 4);
366 tty_print_string (_("On"));
367 dlg_move (ch_dlg, BY + 1, 4);
368 tty_print_string (_("Flag"));
369 dlg_move (ch_dlg, BY + 2, 4);
370 tty_print_string (_("Mode"));
372 if (!single_set){
373 dlg_move (ch_dlg, 3, 54);
374 tty_printf (_("%6d of %d"),
375 files_on_begin - (current_panel->marked) + 1,
376 files_on_begin);
379 print_flags ();
382 static void chown_info_update (void)
384 /* display file info */
385 tty_setcolor (COLOR_NORMAL);
387 /* name && mode */
388 dlg_move (ch_dlg, 3, 8);
389 tty_print_string (str_fit_to_term (fname, 45, J_LEFT_FIT));
390 dlg_move (ch_dlg, BY + 2, 9);
391 tty_printf ("%12o", get_mode ());
393 /* permissions */
394 update_permissions ();
397 static void b_setpos (int f_pos) {
398 b_att[0]->hotpos=-1;
399 b_att[1]->hotpos=-1;
400 b_att[2]->hotpos=-1;
401 b_att[f_pos]->hotpos = (flag_pos % 3);
404 static cb_ret_t
405 advanced_chown_callback (Dlg_head *h, Widget *sender,
406 dlg_msg_t msg, int parm, void *data)
408 int i = 0, f_pos = BUTTONS - h->current->dlg_id - single_set - 1;
410 switch (msg) {
411 case DLG_DRAW:
412 chown_refresh ();
413 chown_info_update ();
414 return MSG_HANDLED;
416 case DLG_POST_KEY:
417 if (f_pos < 3)
418 b_setpos (f_pos);
419 return MSG_HANDLED;
421 case DLG_FOCUS:
422 if (f_pos < 3) {
423 if ((flag_pos / 3) != f_pos)
424 flag_pos = f_pos * 3;
425 b_setpos (f_pos);
426 } else if (f_pos < 5)
427 flag_pos = f_pos + 6;
428 return MSG_HANDLED;
430 case DLG_KEY:
431 switch (parm) {
433 case XCTRL ('b'):
434 case KEY_LEFT:
435 if (f_pos < 5)
436 return (dec_flag_pos (f_pos));
437 break;
439 case XCTRL ('f'):
440 case KEY_RIGHT:
441 if (f_pos < 5)
442 return (inc_flag_pos (f_pos));
443 break;
445 case ' ':
446 if (f_pos < 3)
447 return MSG_HANDLED;
448 break;
450 case '\n':
451 case KEY_ENTER:
452 if (f_pos <= 2 || f_pos >= 5)
453 break;
454 do_enter_key (h, f_pos);
455 return MSG_HANDLED;
457 case ALT ('x'):
458 i++;
460 case ALT ('w'):
461 i++;
463 case ALT ('r'):
464 parm = i + 3;
465 for (i = 0; i < 3; i++)
466 ch_flags[i * 3 + parm - 3] =
467 (x_toggle & (1 << parm)) ? '-' : '+';
468 x_toggle ^= (1 << parm);
469 update_mode (h);
470 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
471 send_message (h->current, WIDGET_FOCUS, 0);
472 break;
474 case XCTRL ('x'):
475 i++;
477 case XCTRL ('w'):
478 i++;
480 case XCTRL ('r'):
481 parm = i;
482 for (i = 0; i < 3; i++)
483 ch_flags[i * 3 + parm] =
484 (x_toggle & (1 << parm)) ? '-' : '+';
485 x_toggle ^= (1 << parm);
486 update_mode (h);
487 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
488 send_message (h->current, WIDGET_FOCUS, 0);
489 break;
491 case 'x':
492 i++;
494 case 'w':
495 i++;
497 case 'r':
498 if (f_pos > 2)
499 break;
500 flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */
501 if (((WButton *) h->current)->text.start[(flag_pos % 3)] ==
502 '-')
503 ch_flags[flag_pos] = '+';
504 else
505 ch_flags[flag_pos] = '-';
506 update_mode (h);
507 break;
509 case '4':
510 i++;
512 case '2':
513 i++;
515 case '1':
516 if (f_pos > 2)
517 break;
518 flag_pos = i + f_pos * 3;
519 ch_flags[flag_pos] = '=';
520 update_mode (h);
521 break;
523 case '-':
524 if (f_pos > 2)
525 break;
527 case '*':
528 if (parm == '*')
529 parm = '=';
531 case '=':
532 case '+':
533 if (f_pos <= 4) {
534 ch_flags[flag_pos] = parm;
535 update_mode (h);
536 advanced_chown_callback (h, sender, DLG_KEY, KEY_RIGHT, NULL);
537 if (flag_pos > 8 || !(flag_pos % 3))
538 dlg_one_down (h);
540 break;
542 return MSG_NOT_HANDLED;
544 default:
545 return default_dlg_callback (h, sender, msg, parm, data);
549 static void
550 init_chown_advanced (void)
552 int i;
553 enum { dlg_h = 13, dlg_w = 74, n_elem = 4 };
554 #ifdef ENABLE_NLS
555 static int i18n_len = 0;
557 if (i18n_len == 0) {
558 int dx, cx;
559 for (i = 0 ; i < n_elem ; i++) {
560 chown_advanced_but[i].text = _(chown_advanced_but[i].text);
561 i18n_len += str_term_width1 (chown_advanced_but[i].text) + 3;
562 if (DEFPUSH_BUTTON == chown_advanced_but[i].flags)
563 i18n_len += 2; /* "<>" */
565 cx = dx = (dlg_w - i18n_len - 2) / (n_elem + 1);
567 /* Reversed order */
568 for (i = n_elem - 1; i >= 0; i--) {
569 chown_advanced_but[i].x = cx;
570 cx += str_term_width1 (chown_advanced_but[i].text) + 3 + dx;
573 #endif /* ENABLE_NLS */
575 sf_stat = g_new (struct stat, 1);
576 do_refresh ();
577 end_chown = need_update = current_file = 0;
578 single_set = (current_panel->marked < 2) ? 2 : 0;
579 memset (ch_flags, '=', 11);
580 flag_pos = 0;
581 x_toggle = 070;
583 ch_dlg =
584 create_dlg (0, 0, dlg_h, dlg_w, dialog_colors, advanced_chown_callback,
585 "[Advanced Chown]", _(" Chown advanced command "),
586 DLG_CENTER | DLG_REVERSE);
588 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
589 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
590 (chown_advanced_but[i].text), 0
592 for (i = 0; i < BUTTONS - 5; i++)
593 if (!single_set || i < 2)
594 add_widget (ch_dlg, button_new (XTRACT (i)));
596 b_att[0] = button_new (XTRACT (8));
597 b_att[1] = button_new (XTRACT (7));
598 b_att[2] = button_new (XTRACT (6));
599 b_user = button_new (XTRACT (5));
600 b_group = button_new (XTRACT (4));
602 add_widget (ch_dlg, b_group);
603 add_widget (ch_dlg, b_user);
604 add_widget (ch_dlg, b_att[2]);
605 add_widget (ch_dlg, b_att[1]);
606 add_widget (ch_dlg, b_att[0]);
609 static void
610 chown_advanced_done (void)
612 g_free (sf_stat);
613 if (need_update)
614 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
615 repaint_screen ();
618 #if 0
619 static void do_chown (uid_t u, gid_t g)
621 chown (current_panel->dir.list[current_file].fname, u, g);
622 file_mark (current_panel, current_file, 0);
624 #endif
626 static char *next_file (void)
628 while (!current_panel->dir.list[current_file].f.marked)
629 current_file++;
631 return current_panel->dir.list[current_file].fname;
634 static void apply_advanced_chowns (struct stat *sf)
636 char *lc_fname;
637 gid_t a_gid = sf->st_gid;
638 uid_t a_uid = sf->st_uid;
640 lc_fname = current_panel->dir.list[current_file].fname;
641 need_update = end_chown = 1;
642 if (mc_chmod (lc_fname, get_mode ()) == -1)
643 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
644 lc_fname, unix_error_string (errno));
645 /* call mc_chown only, if mc_chmod didn't fail */
646 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? sf->st_uid : (uid_t) -1,
647 (ch_flags[10] == '+') ? sf->st_gid : (gid_t) -1) == -1)
648 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
649 lc_fname, unix_error_string (errno));
650 do_file_mark (current_panel, current_file, 0);
652 do {
653 lc_fname = next_file ();
655 if (mc_stat (lc_fname, sf) != 0)
656 break;
657 ch_cmode = sf->st_mode;
658 if (mc_chmod (lc_fname, get_mode ()) == -1)
659 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
660 lc_fname, unix_error_string (errno));
661 /* call mc_chown only, if mc_chmod didn't fail */
662 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? a_uid : (uid_t) -1,
663 (ch_flags[10] == '+') ? a_gid : (gid_t) -1) == -1)
664 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
665 lc_fname, unix_error_string (errno));
667 do_file_mark (current_panel, current_file, 0);
668 } while (current_panel->marked);
671 void
672 chown_advanced_cmd (void)
675 files_on_begin = current_panel->marked;
677 do { /* do while any files remaining */
678 init_chown_advanced ();
680 if (current_panel->marked)
681 fname = next_file (); /* next marked file */
682 else
683 fname = selection (current_panel)->fname; /* single file */
685 if (mc_stat (fname, sf_stat) != 0) { /* get status of file */
686 destroy_dlg (ch_dlg);
687 break;
689 ch_cmode = sf_stat->st_mode;
691 chown_refresh ();
693 update_ownership ();
695 /* game can begin */
696 run_dlg (ch_dlg);
698 switch (ch_dlg->ret_value) {
699 case B_CANCEL:
700 end_chown = 1;
701 break;
703 case B_ENTER:
704 need_update = 1;
705 if (mc_chmod (fname, get_mode ()) == -1)
706 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
707 fname, unix_error_string (errno));
708 /* call mc_chown only, if mc_chmod didn't fail */
709 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf_stat->st_uid : (uid_t) -1,
710 (ch_flags[10] == '+') ? sf_stat->st_gid : (gid_t) -1) == -1)
711 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
712 fname, unix_error_string (errno));
713 break;
714 case B_SETALL:
715 apply_advanced_chowns (sf_stat);
716 break;
718 case B_SKIP:
719 break;
723 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL) {
724 do_file_mark (current_panel, current_file, 0);
725 need_update = 1;
727 destroy_dlg (ch_dlg);
728 } while (current_panel->marked && !end_chown);
730 chown_advanced_done ();