Merge branch '207_minor_regex_fixes' into mc-4.6
[midnight-commander.git] / src / achown.c
blob72ddcada2bfcd06fec4b081a9129816afb81606d
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 #include <config.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
30 #include "global.h"
31 #include "tty.h"
32 #include "win.h"
33 #include "color.h"
34 #include "dialog.h"
35 #include "widget.h"
36 #include "wtools.h" /* For init_box_colors() */
37 #include "key.h" /* XCTRL and ALT macros */
39 #include "dir.h"
40 #include "panel.h" /* Needed for the externs */
41 #include "chmod.h"
42 #include "main.h"
43 #include "achown.h"
45 #define BX 5
46 #define BY 6
48 #define TX 50
49 #define TY 2
51 #define BUTTONS 9
53 #define B_SETALL B_USER
54 #define B_SKIP (B_USER + 1)
56 #define B_OWN (B_USER + 3)
57 #define B_GRP (B_USER + 4)
58 #define B_OTH (B_USER + 5)
59 #define B_OUSER (B_USER + 6)
60 #define B_OGROUP (B_USER + 7)
62 static struct Dlg_head *ch_dlg;
64 static struct {
65 int ret_cmd, flags, y, x;
66 const char *text;
67 } chown_advanced_but [BUTTONS] = {
68 { B_CANCEL, NORMAL_BUTTON, 4, 53, N_("&Cancel") },
69 { B_ENTER, DEFPUSH_BUTTON,4, 40, N_("&Set") },
70 { B_SKIP, NORMAL_BUTTON, 4, 23, N_("S&kip") },
71 { B_SETALL, NORMAL_BUTTON, 4, 0, N_("Set &all")},
72 { B_ENTER, NARROW_BUTTON, 0, 47, ""},
73 { B_ENTER, NARROW_BUTTON, 0, 29, ""},
74 { B_ENTER, NARROW_BUTTON, 0, 19, " "},
75 { B_ENTER, NARROW_BUTTON, 0, 11, " "},
76 { B_ENTER, NARROW_BUTTON, 0, 3, " "}
79 static WButton *b_att[3]; /* permission */
80 static WButton *b_user, *b_group; /* owner */
82 static int files_on_begin; /* Number of files at startup */
83 static int flag_pos;
84 static int x_toggle;
85 static char ch_flags[11];
86 static const char ch_perm[] = "rwx";
87 static mode_t ch_cmode;
88 static struct stat *sf_stat;
89 static int need_update;
90 static int end_chown;
91 static int current_file;
92 static int single_set;
93 static char *fname;
95 static void update_ownership (void)
97 button_set_text (b_user, get_owner (sf_stat->st_uid));
98 button_set_text (b_group, get_group (sf_stat->st_gid));
102 static cb_ret_t inc_flag_pos (int f_pos)
104 if (flag_pos == 10) {
105 flag_pos = 0;
106 return MSG_NOT_HANDLED;
108 flag_pos++;
109 if (!(flag_pos % 3) || f_pos > 2)
110 return MSG_NOT_HANDLED;
111 return MSG_HANDLED;
114 static cb_ret_t dec_flag_pos (int f_pos)
116 if (!flag_pos) {
117 flag_pos = 10;
118 return MSG_NOT_HANDLED;
120 flag_pos--;
121 if (!((flag_pos + 1) % 3) || f_pos > 2)
122 return MSG_NOT_HANDLED;
123 return MSG_HANDLED;
126 static void set_perm_by_flags (char *s, int f_p)
128 int i;
130 for (i = 0; i < 3; i++) {
131 if (ch_flags[f_p + i] == '+')
132 s[i] = ch_perm[i];
133 else if (ch_flags[f_p + i] == '-')
134 s[i] = '-';
135 else
136 s[i] = (ch_cmode & (1 << (8 - f_p - i))) ? ch_perm[i] : '-';
140 static void update_permissions (void)
142 set_perm_by_flags (b_att[0]->text, 0);
143 set_perm_by_flags (b_att[1]->text, 3);
144 set_perm_by_flags (b_att[2]->text, 6);
147 static mode_t get_perm (char *s, int base)
149 mode_t m;
151 m = 0;
152 m |= (s[0] == '-') ? 0 :
153 ((s[0] == '+') ? (1 << (base + 2)) : (1 << (base + 2)) & ch_cmode);
155 m |= (s[1] == '-') ? 0 :
156 ((s[1] == '+') ? (1 << (base + 1)) : (1 << (base + 1)) & ch_cmode);
158 m |= (s[2] == '-') ? 0 :
159 ((s[2] == '+') ? (1 << base) : (1 << base) & ch_cmode);
161 return m;
164 static mode_t get_mode (void)
166 mode_t m;
168 m = ch_cmode ^ (ch_cmode & 0777);
169 m |= get_perm (ch_flags, 6);
170 m |= get_perm (ch_flags + 3, 3);
171 m |= get_perm (ch_flags + 6, 0);
173 return m;
176 static void print_flags (void)
178 int i;
180 attrset (COLOR_NORMAL);
182 for (i = 0; i < 3; i++){
183 dlg_move (ch_dlg, BY+1, 9+i);
184 addch (ch_flags [i]);
187 for (i = 0; i < 3; i++){
188 dlg_move (ch_dlg, BY + 1, 17 + i);
189 addch (ch_flags [i+3]);
192 for (i = 0; i < 3; i++){
193 dlg_move (ch_dlg, BY + 1, 25 + i);
194 addch (ch_flags [i+6]);
197 update_permissions ();
199 for (i = 0; i < 15; i++){
200 dlg_move (ch_dlg, BY+1, 35+i);
201 addch (ch_flags[9]);
203 for (i = 0; i < 15; i++){
204 dlg_move (ch_dlg, BY + 1, 53 + i);
205 addch (ch_flags[10]);
209 static void update_mode (Dlg_head * h)
211 print_flags ();
212 attrset (COLOR_NORMAL);
213 dlg_move (h, BY + 2, 9);
214 tty_printf ("%12o", get_mode ());
215 send_message (h->current, WIDGET_FOCUS, 0);
218 static cb_ret_t
219 chl_callback (Dlg_head *h, dlg_msg_t msg, int parm)
221 switch (msg) {
222 case DLG_KEY:
223 switch (parm) {
224 case KEY_LEFT:
225 case KEY_RIGHT:
226 h->ret_value = parm;
227 dlg_stop (h);
230 default:
231 return default_dlg_callback (h, msg, parm);
235 static void
236 do_enter_key (Dlg_head * h, int f_pos)
238 Dlg_head *chl_dlg;
239 WListbox *chl_list;
240 struct passwd *chl_pass;
241 struct group *chl_grp;
242 WLEntry *fe;
243 int lxx, lyy, chl_end, b_pos;
244 int is_owner;
245 const char *title;
247 do {
248 is_owner = (f_pos == 3);
249 title = is_owner ? _("owner") : _("group");
251 lxx = (COLS - 74) / 2 + (is_owner ? 35 : 53);
252 lyy = (LINES - 13) / 2;
253 chl_end = 0;
255 chl_dlg =
256 create_dlg (lyy, lxx, 13, 17, dialog_colors, chl_callback,
257 "[Advanced Chown]", title, DLG_COMPACT | DLG_REVERSE);
259 /* get new listboxes */
260 chl_list = listbox_new (1, 1, 15, 11, NULL);
262 listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0,
263 "<Unknown>", NULL);
265 if (is_owner) {
266 /* get and put user names in the listbox */
267 setpwent ();
268 while ((chl_pass = getpwent ())) {
269 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0,
270 chl_pass->pw_name, NULL);
272 endpwent ();
273 fe = listbox_search_text (chl_list,
274 get_owner (sf_stat->st_uid));
275 } else {
276 /* get and put group names in the listbox */
277 setgrent ();
278 while ((chl_grp = getgrent ())) {
279 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0,
280 chl_grp->gr_name, NULL);
282 endgrent ();
283 fe = listbox_search_text (chl_list,
284 get_group (sf_stat->st_gid));
287 if (fe)
288 listbox_select_entry (chl_list, fe);
290 b_pos = chl_list->pos;
291 add_widget (chl_dlg, chl_list);
293 run_dlg (chl_dlg);
295 if (b_pos != chl_list->pos) {
296 int ok = 0;
297 if (is_owner) {
298 chl_pass = getpwnam (chl_list->current->text);
299 if (chl_pass) {
300 ok = 1;
301 sf_stat->st_uid = chl_pass->pw_uid;
303 } else {
304 chl_grp = getgrnam (chl_list->current->text);
305 if (chl_grp) {
306 sf_stat->st_gid = chl_grp->gr_gid;
307 ok = 1;
310 if (ok) {
311 ch_flags[f_pos + 6] = '+';
312 update_ownership ();
314 dlg_focus (h);
315 if (ok)
316 print_flags ();
318 if (chl_dlg->ret_value == KEY_LEFT) {
319 if (!is_owner)
320 chl_end = 1;
321 dlg_one_up (ch_dlg);
322 f_pos--;
323 } else if (chl_dlg->ret_value == KEY_RIGHT) {
324 if (is_owner)
325 chl_end = 1;
326 dlg_one_down (ch_dlg);
327 f_pos++;
329 /* Here we used to redraw the window */
330 destroy_dlg (chl_dlg);
331 } while (chl_end);
334 static void chown_refresh (void)
336 common_dialog_repaint (ch_dlg);
338 attrset (COLOR_NORMAL);
340 dlg_move (ch_dlg, BY - 1, 8);
341 addstr (_("owner"));
342 dlg_move (ch_dlg, BY - 1, 16);
343 addstr (_("group"));
344 dlg_move (ch_dlg, BY - 1, 24);
345 addstr (_("other"));
347 dlg_move (ch_dlg, BY - 1, 35);
348 addstr (_("owner"));
349 dlg_move (ch_dlg, BY - 1, 53);
350 addstr (_("group"));
352 dlg_move (ch_dlg, 3, 4);
353 addstr (_("On"));
354 dlg_move (ch_dlg, BY + 1, 4);
355 addstr (_("Flag"));
356 dlg_move (ch_dlg, BY + 2, 4);
357 addstr (_("Mode"));
359 if (!single_set){
360 dlg_move (ch_dlg, 3, 54);
361 tty_printf (_("%6d of %d"),
362 files_on_begin - (current_panel->marked) + 1,
363 files_on_begin);
366 print_flags ();
369 static void chown_info_update (void)
371 /* display file info */
372 attrset (COLOR_NORMAL);
374 /* name && mode */
375 dlg_move (ch_dlg, 3, 8);
376 tty_printf ("%s", name_trunc (fname, 45));
377 dlg_move (ch_dlg, BY + 2, 9);
378 tty_printf ("%12o", get_mode ());
380 /* permissions */
381 update_permissions ();
384 static void b_setpos (int f_pos) {
385 b_att[0]->hotpos=-1;
386 b_att[1]->hotpos=-1;
387 b_att[2]->hotpos=-1;
388 b_att[f_pos]->hotpos = (flag_pos % 3);
391 static cb_ret_t
392 advanced_chown_callback (Dlg_head *h, dlg_msg_t msg, int parm)
394 int i = 0, f_pos = BUTTONS - h->current->dlg_id - single_set - 1;
396 switch (msg) {
397 case DLG_DRAW:
398 chown_refresh ();
399 chown_info_update ();
400 return MSG_HANDLED;
402 case DLG_POST_KEY:
403 if (f_pos < 3)
404 b_setpos (f_pos);
405 return MSG_HANDLED;
407 case DLG_FOCUS:
408 if (f_pos < 3) {
409 if ((flag_pos / 3) != f_pos)
410 flag_pos = f_pos * 3;
411 b_setpos (f_pos);
412 } else if (f_pos < 5)
413 flag_pos = f_pos + 6;
414 return MSG_HANDLED;
416 case DLG_KEY:
417 switch (parm) {
419 case XCTRL ('b'):
420 case KEY_LEFT:
421 if (f_pos < 5)
422 return (dec_flag_pos (f_pos));
423 break;
425 case XCTRL ('f'):
426 case KEY_RIGHT:
427 if (f_pos < 5)
428 return (inc_flag_pos (f_pos));
429 break;
431 case ' ':
432 if (f_pos < 3)
433 return MSG_HANDLED;
434 break;
436 case '\n':
437 case KEY_ENTER:
438 if (f_pos <= 2 || f_pos >= 5)
439 break;
440 do_enter_key (h, f_pos);
441 return MSG_HANDLED;
443 case ALT ('x'):
444 i++;
446 case ALT ('w'):
447 i++;
449 case ALT ('r'):
450 parm = i + 3;
451 for (i = 0; i < 3; i++)
452 ch_flags[i * 3 + parm - 3] =
453 (x_toggle & (1 << parm)) ? '-' : '+';
454 x_toggle ^= (1 << parm);
455 update_mode (h);
456 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
457 send_message (h->current, WIDGET_FOCUS, 0);
458 break;
460 case XCTRL ('x'):
461 i++;
463 case XCTRL ('w'):
464 i++;
466 case XCTRL ('r'):
467 parm = i;
468 for (i = 0; i < 3; i++)
469 ch_flags[i * 3 + parm] =
470 (x_toggle & (1 << parm)) ? '-' : '+';
471 x_toggle ^= (1 << parm);
472 update_mode (h);
473 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
474 send_message (h->current, WIDGET_FOCUS, 0);
475 break;
477 case 'x':
478 i++;
480 case 'w':
481 i++;
483 case 'r':
484 if (f_pos > 2)
485 break;
486 flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */
487 if (((WButton *) h->current)->text[(flag_pos % 3)] ==
488 '-')
489 ch_flags[flag_pos] = '+';
490 else
491 ch_flags[flag_pos] = '-';
492 update_mode (h);
493 break;
495 case '4':
496 i++;
498 case '2':
499 i++;
501 case '1':
502 if (f_pos > 2)
503 break;
504 flag_pos = i + f_pos * 3;
505 ch_flags[flag_pos] = '=';
506 update_mode (h);
507 break;
509 case '-':
510 if (f_pos > 2)
511 break;
513 case '*':
514 if (parm == '*')
515 parm = '=';
517 case '=':
518 case '+':
519 if (f_pos > 4)
520 break;
521 ch_flags[flag_pos] = parm;
522 update_mode (h);
523 advanced_chown_callback (h, DLG_KEY, KEY_RIGHT);
524 if (flag_pos > 8 || !(flag_pos % 3))
525 dlg_one_down (h);
527 break;
529 return MSG_NOT_HANDLED;
531 default:
532 return default_dlg_callback (h, msg, parm);
536 static void
537 init_chown_advanced (void)
539 int i;
540 enum { dlg_h = 13, dlg_w = 74, n_elem = 4 };
541 #ifdef ENABLE_NLS
542 static int i18n_len;
544 if (!i18n_len) {
545 int dx, cx;
546 for (i = 0 ; i < n_elem ; i++) {
547 chown_advanced_but[i].text = _(chown_advanced_but[i].text);
548 i18n_len += strlen (chown_advanced_but[i].text) + 3;
549 if (DEFPUSH_BUTTON == chown_advanced_but[i].flags)
550 i18n_len += 2; /* "<>" */
552 cx = dx = (dlg_w - i18n_len - 2) / (n_elem + 1);
554 /* Reversed order */
555 for (i = n_elem - 1; i >= 0; i--) {
556 chown_advanced_but[i].x = cx;
557 cx += strlen (chown_advanced_but[i].text) + 3 + dx;
560 #endif /* ENABLE_NLS */
562 sf_stat = g_new (struct stat, 1);
563 do_refresh ();
564 end_chown = need_update = current_file = 0;
565 single_set = (current_panel->marked < 2) ? 2 : 0;
566 memset (ch_flags, '=', 11);
567 flag_pos = 0;
568 x_toggle = 070;
570 ch_dlg =
571 create_dlg (0, 0, dlg_h, dlg_w, dialog_colors, advanced_chown_callback,
572 "[Advanced Chown]", _(" Chown advanced command "),
573 DLG_CENTER | DLG_REVERSE);
575 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
576 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
577 (chown_advanced_but[i].text), 0
579 for (i = 0; i < BUTTONS - 5; i++)
580 if (!single_set || i < 2)
581 add_widget (ch_dlg, button_new (XTRACT (i)));
583 b_att[0] = button_new (XTRACT (8));
584 b_att[1] = button_new (XTRACT (7));
585 b_att[2] = button_new (XTRACT (6));
586 b_user = button_new (XTRACT (5));
587 b_group = button_new (XTRACT (4));
589 add_widget (ch_dlg, b_group);
590 add_widget (ch_dlg, b_user);
591 add_widget (ch_dlg, b_att[2]);
592 add_widget (ch_dlg, b_att[1]);
593 add_widget (ch_dlg, b_att[0]);
596 static void
597 chown_advanced_done (void)
599 g_free (sf_stat);
600 if (need_update)
601 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
602 repaint_screen ();
605 #if 0
606 static inline void do_chown (uid_t u, gid_t g)
608 chown (current_panel->dir.list[current_file].fname, u, g);
609 file_mark (current_panel, current_file, 0);
611 #endif
613 static char *next_file (void)
615 while (!current_panel->dir.list[current_file].f.marked)
616 current_file++;
618 return current_panel->dir.list[current_file].fname;
621 static void apply_advanced_chowns (struct stat *sf)
623 char *fname;
624 gid_t a_gid = sf->st_gid;
625 uid_t a_uid = sf->st_uid;
627 fname = current_panel->dir.list[current_file].fname;
628 need_update = end_chown = 1;
629 if (mc_chmod (fname, get_mode ()) == -1)
630 message (1, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
631 fname, unix_error_string (errno));
632 /* call mc_chown only, if mc_chmod didn't fail */
633 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf->st_uid : (uid_t) -1,
634 (ch_flags[10] == '+') ? sf->st_gid : (gid_t) -1) == -1)
635 message (1, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
636 fname, unix_error_string (errno));
637 do_file_mark (current_panel, current_file, 0);
639 do {
640 fname = next_file ();
642 if (mc_stat (fname, sf) != 0)
643 break;
644 ch_cmode = sf->st_mode;
645 if (mc_chmod (fname, get_mode ()) == -1)
646 message (1, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
647 fname, unix_error_string (errno));
648 /* call mc_chown only, if mc_chmod didn't fail */
649 else if (mc_chown (fname, (ch_flags[9] == '+') ? a_uid : (uid_t) -1,
650 (ch_flags[10] == '+') ? a_gid : (gid_t) -1) == -1)
651 message (1, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
652 fname, unix_error_string (errno));
654 do_file_mark (current_panel, current_file, 0);
655 } while (current_panel->marked);
658 void
659 chown_advanced_cmd (void)
662 files_on_begin = current_panel->marked;
664 do { /* do while any files remaining */
665 init_chown_advanced ();
667 if (current_panel->marked)
668 fname = next_file (); /* next marked file */
669 else
670 fname = selection (current_panel)->fname; /* single file */
672 if (mc_stat (fname, sf_stat) != 0) { /* get status of file */
673 destroy_dlg (ch_dlg);
674 break;
676 ch_cmode = sf_stat->st_mode;
678 chown_refresh ();
680 update_ownership ();
682 /* game can begin */
683 run_dlg (ch_dlg);
685 switch (ch_dlg->ret_value) {
686 case B_CANCEL:
687 end_chown = 1;
688 break;
690 case B_ENTER:
691 need_update = 1;
692 if (mc_chmod (fname, get_mode ()) == -1)
693 message (1, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
694 fname, unix_error_string (errno));
695 /* call mc_chown only, if mc_chmod didn't fail */
696 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf_stat->st_uid : (uid_t) -1,
697 (ch_flags[10] == '+') ? sf_stat->st_gid : (gid_t) -1) == -1)
698 message (1, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
699 fname, unix_error_string (errno));
700 break;
701 case B_SETALL:
702 apply_advanced_chowns (sf_stat);
703 break;
705 case B_SKIP:
706 break;
710 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL) {
711 do_file_mark (current_panel, current_file, 0);
712 need_update = 1;
714 destroy_dlg (ch_dlg);
715 } while (current_panel->marked && !end_chown);
717 chown_advanced_done ();