Moved dir $(srcdir)/vfs into $(srcdir)/lib/vfs/mc-vfs
[midnight-commander.git] / src / achown.c
blobb94527c1423479156dcbe0dbf2bb63e4c6540a79
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"
41 #include "../vfs/vfs.h"
43 #include "dialog.h"
44 #include "widget.h"
45 #include "wtools.h" /* For init_box_colors() */
46 #include "strutil.h"
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 WLEntry *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, 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 if (fe)
299 listbox_select_entry (chl_list, fe);
301 b_pos = chl_list->pos;
302 add_widget (chl_dlg, chl_list);
304 run_dlg (chl_dlg);
306 if (b_pos != chl_list->pos) {
307 int ok = 0;
308 if (is_owner) {
309 chl_pass = getpwnam (chl_list->current->text);
310 if (chl_pass) {
311 ok = 1;
312 sf_stat->st_uid = chl_pass->pw_uid;
314 } else {
315 chl_grp = getgrnam (chl_list->current->text);
316 if (chl_grp) {
317 sf_stat->st_gid = chl_grp->gr_gid;
318 ok = 1;
321 if (ok) {
322 ch_flags[f_pos + 6] = '+';
323 update_ownership ();
325 dlg_focus (h);
326 if (ok)
327 print_flags ();
329 if (chl_dlg->ret_value == KEY_LEFT) {
330 if (!is_owner)
331 chl_end = 1;
332 dlg_one_up (ch_dlg);
333 f_pos--;
334 } else if (chl_dlg->ret_value == KEY_RIGHT) {
335 if (is_owner)
336 chl_end = 1;
337 dlg_one_down (ch_dlg);
338 f_pos++;
340 /* Here we used to redraw the window */
341 destroy_dlg (chl_dlg);
342 } while (chl_end);
345 static void chown_refresh (void)
347 common_dialog_repaint (ch_dlg);
349 tty_setcolor (COLOR_NORMAL);
351 dlg_move (ch_dlg, BY - 1, 8);
352 tty_print_string (_("owner"));
353 dlg_move (ch_dlg, BY - 1, 16);
354 tty_print_string (_("group"));
355 dlg_move (ch_dlg, BY - 1, 24);
356 tty_print_string (_("other"));
358 dlg_move (ch_dlg, BY - 1, 35);
359 tty_print_string (_("owner"));
360 dlg_move (ch_dlg, BY - 1, 53);
361 tty_print_string (_("group"));
363 dlg_move (ch_dlg, 3, 4);
364 tty_print_string (_("On"));
365 dlg_move (ch_dlg, BY + 1, 4);
366 tty_print_string (_("Flag"));
367 dlg_move (ch_dlg, BY + 2, 4);
368 tty_print_string (_("Mode"));
370 if (!single_set){
371 dlg_move (ch_dlg, 3, 54);
372 tty_printf (_("%6d of %d"),
373 files_on_begin - (current_panel->marked) + 1,
374 files_on_begin);
377 print_flags ();
380 static void chown_info_update (void)
382 /* display file info */
383 tty_setcolor (COLOR_NORMAL);
385 /* name && mode */
386 dlg_move (ch_dlg, 3, 8);
387 tty_print_string (str_fit_to_term (fname, 45, J_LEFT_FIT));
388 dlg_move (ch_dlg, BY + 2, 9);
389 tty_printf ("%12o", get_mode ());
391 /* permissions */
392 update_permissions ();
395 static void b_setpos (int f_pos) {
396 b_att[0]->hotpos=-1;
397 b_att[1]->hotpos=-1;
398 b_att[2]->hotpos=-1;
399 b_att[f_pos]->hotpos = (flag_pos % 3);
402 static cb_ret_t
403 advanced_chown_callback (Dlg_head *h, Widget *sender,
404 dlg_msg_t msg, int parm, void *data)
406 int i = 0, f_pos = BUTTONS - h->current->dlg_id - single_set - 1;
408 switch (msg) {
409 case DLG_DRAW:
410 chown_refresh ();
411 chown_info_update ();
412 return MSG_HANDLED;
414 case DLG_POST_KEY:
415 if (f_pos < 3)
416 b_setpos (f_pos);
417 return MSG_HANDLED;
419 case DLG_FOCUS:
420 if (f_pos < 3) {
421 if ((flag_pos / 3) != f_pos)
422 flag_pos = f_pos * 3;
423 b_setpos (f_pos);
424 } else if (f_pos < 5)
425 flag_pos = f_pos + 6;
426 return MSG_HANDLED;
428 case DLG_KEY:
429 switch (parm) {
431 case XCTRL ('b'):
432 case KEY_LEFT:
433 if (f_pos < 5)
434 return (dec_flag_pos (f_pos));
435 break;
437 case XCTRL ('f'):
438 case KEY_RIGHT:
439 if (f_pos < 5)
440 return (inc_flag_pos (f_pos));
441 break;
443 case ' ':
444 if (f_pos < 3)
445 return MSG_HANDLED;
446 break;
448 case '\n':
449 case KEY_ENTER:
450 if (f_pos <= 2 || f_pos >= 5)
451 break;
452 do_enter_key (h, f_pos);
453 return MSG_HANDLED;
455 case ALT ('x'):
456 i++;
458 case ALT ('w'):
459 i++;
461 case ALT ('r'):
462 parm = i + 3;
463 for (i = 0; i < 3; i++)
464 ch_flags[i * 3 + parm - 3] =
465 (x_toggle & (1 << parm)) ? '-' : '+';
466 x_toggle ^= (1 << parm);
467 update_mode (h);
468 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
469 send_message (h->current, WIDGET_FOCUS, 0);
470 break;
472 case XCTRL ('x'):
473 i++;
475 case XCTRL ('w'):
476 i++;
478 case XCTRL ('r'):
479 parm = i;
480 for (i = 0; i < 3; i++)
481 ch_flags[i * 3 + parm] =
482 (x_toggle & (1 << parm)) ? '-' : '+';
483 x_toggle ^= (1 << parm);
484 update_mode (h);
485 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
486 send_message (h->current, WIDGET_FOCUS, 0);
487 break;
489 case 'x':
490 i++;
492 case 'w':
493 i++;
495 case 'r':
496 if (f_pos > 2)
497 break;
498 flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */
499 if (((WButton *) h->current)->text.start[(flag_pos % 3)] ==
500 '-')
501 ch_flags[flag_pos] = '+';
502 else
503 ch_flags[flag_pos] = '-';
504 update_mode (h);
505 break;
507 case '4':
508 i++;
510 case '2':
511 i++;
513 case '1':
514 if (f_pos > 2)
515 break;
516 flag_pos = i + f_pos * 3;
517 ch_flags[flag_pos] = '=';
518 update_mode (h);
519 break;
521 case '-':
522 if (f_pos > 2)
523 break;
525 case '*':
526 if (parm == '*')
527 parm = '=';
529 case '=':
530 case '+':
531 if (f_pos <= 4) {
532 ch_flags[flag_pos] = parm;
533 update_mode (h);
534 advanced_chown_callback (h, sender, DLG_KEY, KEY_RIGHT, NULL);
535 if (flag_pos > 8 || !(flag_pos % 3))
536 dlg_one_down (h);
538 break;
540 return MSG_NOT_HANDLED;
542 default:
543 return default_dlg_callback (h, sender, msg, parm, data);
547 static void
548 init_chown_advanced (void)
550 int i;
551 enum { dlg_h = 13, dlg_w = 74, n_elem = 4 };
552 #ifdef ENABLE_NLS
553 static int i18n_len = 0;
555 if (i18n_len == 0) {
556 int dx, cx;
557 for (i = 0 ; i < n_elem ; i++) {
558 chown_advanced_but[i].text = _(chown_advanced_but[i].text);
559 i18n_len += str_term_width1 (chown_advanced_but[i].text) + 3;
560 if (DEFPUSH_BUTTON == chown_advanced_but[i].flags)
561 i18n_len += 2; /* "<>" */
563 cx = dx = (dlg_w - i18n_len - 2) / (n_elem + 1);
565 /* Reversed order */
566 for (i = n_elem - 1; i >= 0; i--) {
567 chown_advanced_but[i].x = cx;
568 cx += str_term_width1 (chown_advanced_but[i].text) + 3 + dx;
571 #endif /* ENABLE_NLS */
573 sf_stat = g_new (struct stat, 1);
574 do_refresh ();
575 end_chown = need_update = current_file = 0;
576 single_set = (current_panel->marked < 2) ? 2 : 0;
577 memset (ch_flags, '=', 11);
578 flag_pos = 0;
579 x_toggle = 070;
581 ch_dlg =
582 create_dlg (0, 0, dlg_h, dlg_w, dialog_colors, advanced_chown_callback,
583 "[Advanced Chown]", _(" Chown advanced command "),
584 DLG_CENTER | DLG_REVERSE);
586 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
587 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
588 (chown_advanced_but[i].text), 0
590 for (i = 0; i < BUTTONS - 5; i++)
591 if (!single_set || i < 2)
592 add_widget (ch_dlg, button_new (XTRACT (i)));
594 b_att[0] = button_new (XTRACT (8));
595 b_att[1] = button_new (XTRACT (7));
596 b_att[2] = button_new (XTRACT (6));
597 b_user = button_new (XTRACT (5));
598 b_group = button_new (XTRACT (4));
600 add_widget (ch_dlg, b_group);
601 add_widget (ch_dlg, b_user);
602 add_widget (ch_dlg, b_att[2]);
603 add_widget (ch_dlg, b_att[1]);
604 add_widget (ch_dlg, b_att[0]);
607 static void
608 chown_advanced_done (void)
610 g_free (sf_stat);
611 if (need_update)
612 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
613 repaint_screen ();
616 #if 0
617 static void do_chown (uid_t u, gid_t g)
619 chown (current_panel->dir.list[current_file].fname, u, g);
620 file_mark (current_panel, current_file, 0);
622 #endif
624 static char *next_file (void)
626 while (!current_panel->dir.list[current_file].f.marked)
627 current_file++;
629 return current_panel->dir.list[current_file].fname;
632 static void apply_advanced_chowns (struct stat *sf)
634 char *lc_fname;
635 gid_t a_gid = sf->st_gid;
636 uid_t a_uid = sf->st_uid;
638 lc_fname = current_panel->dir.list[current_file].fname;
639 need_update = end_chown = 1;
640 if (mc_chmod (lc_fname, get_mode ()) == -1)
641 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
642 lc_fname, unix_error_string (errno));
643 /* call mc_chown only, if mc_chmod didn't fail */
644 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? sf->st_uid : (uid_t) -1,
645 (ch_flags[10] == '+') ? sf->st_gid : (gid_t) -1) == -1)
646 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
647 lc_fname, unix_error_string (errno));
648 do_file_mark (current_panel, current_file, 0);
650 do {
651 lc_fname = next_file ();
653 if (mc_stat (lc_fname, sf) != 0)
654 break;
655 ch_cmode = sf->st_mode;
656 if (mc_chmod (lc_fname, get_mode ()) == -1)
657 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
658 lc_fname, unix_error_string (errno));
659 /* call mc_chown only, if mc_chmod didn't fail */
660 else if (mc_chown (lc_fname, (ch_flags[9] == '+') ? a_uid : (uid_t) -1,
661 (ch_flags[10] == '+') ? a_gid : (gid_t) -1) == -1)
662 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
663 lc_fname, unix_error_string (errno));
665 do_file_mark (current_panel, current_file, 0);
666 } while (current_panel->marked);
669 void
670 chown_advanced_cmd (void)
673 files_on_begin = current_panel->marked;
675 do { /* do while any files remaining */
676 init_chown_advanced ();
678 if (current_panel->marked)
679 fname = next_file (); /* next marked file */
680 else
681 fname = selection (current_panel)->fname; /* single file */
683 if (mc_stat (fname, sf_stat) != 0) { /* get status of file */
684 destroy_dlg (ch_dlg);
685 break;
687 ch_cmode = sf_stat->st_mode;
689 chown_refresh ();
691 update_ownership ();
693 /* game can begin */
694 run_dlg (ch_dlg);
696 switch (ch_dlg->ret_value) {
697 case B_CANCEL:
698 end_chown = 1;
699 break;
701 case B_ENTER:
702 need_update = 1;
703 if (mc_chmod (fname, get_mode ()) == -1)
704 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
705 fname, unix_error_string (errno));
706 /* call mc_chown only, if mc_chmod didn't fail */
707 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf_stat->st_uid : (uid_t) -1,
708 (ch_flags[10] == '+') ? sf_stat->st_gid : (gid_t) -1) == -1)
709 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
710 fname, unix_error_string (errno));
711 break;
712 case B_SETALL:
713 apply_advanced_chowns (sf_stat);
714 break;
716 case B_SKIP:
717 break;
721 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL) {
722 do_file_mark (current_panel, current_file, 0);
723 need_update = 1;
725 destroy_dlg (ch_dlg);
726 } while (current_panel->marked && !end_chown);
728 chown_advanced_done ();