fix: incorret draw files in 8-bit codeset after recode
[midnight-commander.git] / src / achown.c
blob444d7bc6821d7d7974decc6cf29b74bd99028025
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 */
38 #include "strutil.h"
40 #include "dir.h"
41 #include "panel.h" /* Needed for the externs */
42 #include "chmod.h"
43 #include "main.h"
44 #include "achown.h"
46 #define BX 5
47 #define BY 6
49 #define TX 50
50 #define TY 2
52 #define BUTTONS 9
54 #define B_SETALL B_USER
55 #define B_SKIP (B_USER + 1)
57 #define B_OWN (B_USER + 3)
58 #define B_GRP (B_USER + 4)
59 #define B_OTH (B_USER + 5)
60 #define B_OUSER (B_USER + 6)
61 #define B_OGROUP (B_USER + 7)
63 static struct Dlg_head *ch_dlg;
65 static struct {
66 int ret_cmd, flags, y, x;
67 const char *text;
68 } chown_advanced_but [BUTTONS] = {
69 { B_CANCEL, NORMAL_BUTTON, 4, 53, N_("&Cancel") },
70 { B_ENTER, DEFPUSH_BUTTON,4, 40, N_("&Set") },
71 { B_SKIP, NORMAL_BUTTON, 4, 23, N_("S&kip") },
72 { B_SETALL, NORMAL_BUTTON, 4, 0, N_("Set &all")},
73 { B_ENTER, NARROW_BUTTON, 0, 47, ""},
74 { B_ENTER, NARROW_BUTTON, 0, 29, ""},
75 { B_ENTER, NARROW_BUTTON, 0, 19, " "},
76 { B_ENTER, NARROW_BUTTON, 0, 11, " "},
77 { B_ENTER, NARROW_BUTTON, 0, 3, " "}
80 static WButton *b_att[3]; /* permission */
81 static WButton *b_user, *b_group; /* owner */
83 static int files_on_begin; /* Number of files at startup */
84 static int flag_pos;
85 static int x_toggle;
86 static char ch_flags[11];
87 static const char ch_perm[] = "rwx";
88 static mode_t ch_cmode;
89 static struct stat *sf_stat;
90 static int need_update;
91 static int end_chown;
92 static int current_file;
93 static int single_set;
94 static char *fname;
96 static void update_ownership (void)
98 button_set_text (b_user, get_owner (sf_stat->st_uid));
99 button_set_text (b_group, get_group (sf_stat->st_gid));
103 static cb_ret_t inc_flag_pos (int f_pos)
105 if (flag_pos == 10) {
106 flag_pos = 0;
107 return MSG_NOT_HANDLED;
109 flag_pos++;
110 if (!(flag_pos % 3) || f_pos > 2)
111 return MSG_NOT_HANDLED;
112 return MSG_HANDLED;
115 static cb_ret_t dec_flag_pos (int f_pos)
117 if (!flag_pos) {
118 flag_pos = 10;
119 return MSG_NOT_HANDLED;
121 flag_pos--;
122 if (!((flag_pos + 1) % 3) || f_pos > 2)
123 return MSG_NOT_HANDLED;
124 return MSG_HANDLED;
127 static void set_perm_by_flags (char *s, int f_p)
129 int i;
131 for (i = 0; i < 3; i++) {
132 if (ch_flags[f_p + i] == '+')
133 s[i] = ch_perm[i];
134 else if (ch_flags[f_p + i] == '-')
135 s[i] = '-';
136 else
137 s[i] = (ch_cmode & (1 << (8 - f_p - i))) ? ch_perm[i] : '-';
141 static void update_permissions (void)
143 set_perm_by_flags (b_att[0]->text.start, 0);
144 set_perm_by_flags (b_att[1]->text.start, 3);
145 set_perm_by_flags (b_att[2]->text.start, 6);
148 static mode_t get_perm (char *s, int base)
150 mode_t m;
152 m = 0;
153 m |= (s[0] == '-') ? 0 :
154 ((s[0] == '+') ? (1 << (base + 2)) : (1 << (base + 2)) & ch_cmode);
156 m |= (s[1] == '-') ? 0 :
157 ((s[1] == '+') ? (1 << (base + 1)) : (1 << (base + 1)) & ch_cmode);
159 m |= (s[2] == '-') ? 0 :
160 ((s[2] == '+') ? (1 << base) : (1 << base) & ch_cmode);
162 return m;
165 static mode_t get_mode (void)
167 mode_t m;
169 m = ch_cmode ^ (ch_cmode & 0777);
170 m |= get_perm (ch_flags, 6);
171 m |= get_perm (ch_flags + 3, 3);
172 m |= get_perm (ch_flags + 6, 0);
174 return m;
177 static void print_flags (void)
179 int i;
181 attrset (COLOR_NORMAL);
183 for (i = 0; i < 3; i++){
184 dlg_move (ch_dlg, BY+1, 9+i);
185 addch (ch_flags [i]);
188 for (i = 0; i < 3; i++){
189 dlg_move (ch_dlg, BY + 1, 17 + i);
190 addch (ch_flags [i+3]);
193 for (i = 0; i < 3; i++){
194 dlg_move (ch_dlg, BY + 1, 25 + i);
195 addch (ch_flags [i+6]);
198 update_permissions ();
200 for (i = 0; i < 15; i++){
201 dlg_move (ch_dlg, BY+1, 35+i);
202 addch (ch_flags[9]);
204 for (i = 0; i < 15; i++){
205 dlg_move (ch_dlg, BY + 1, 53 + i);
206 addch (ch_flags[10]);
210 static void update_mode (Dlg_head * h)
212 print_flags ();
213 attrset (COLOR_NORMAL);
214 dlg_move (h, BY + 2, 9);
215 tty_printf ("%12o", get_mode ());
216 send_message (h->current, WIDGET_FOCUS, 0);
219 static cb_ret_t
220 chl_callback (Dlg_head *h, dlg_msg_t msg, int parm)
222 switch (msg) {
223 case DLG_KEY:
224 switch (parm) {
225 case KEY_LEFT:
226 case KEY_RIGHT:
227 h->ret_value = parm;
228 dlg_stop (h);
231 default:
232 return default_dlg_callback (h, msg, parm);
236 static void
237 do_enter_key (Dlg_head * h, int f_pos)
239 Dlg_head *chl_dlg;
240 WListbox *chl_list;
241 struct passwd *chl_pass;
242 struct group *chl_grp;
243 WLEntry *fe;
244 int lxx, lyy, chl_end, b_pos;
245 int is_owner;
246 const char *title;
248 do {
249 is_owner = (f_pos == 3);
250 title = is_owner ? _("owner") : _("group");
252 lxx = (COLS - 74) / 2 + (is_owner ? 35 : 53);
253 lyy = (LINES - 13) / 2;
254 chl_end = 0;
256 chl_dlg =
257 create_dlg (lyy, lxx, 13, 17, dialog_colors, chl_callback,
258 "[Advanced Chown]", title, DLG_COMPACT | DLG_REVERSE);
260 /* get new listboxes */
261 chl_list = listbox_new (1, 1, 11, 15, NULL);
263 listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0,
264 "<Unknown>", NULL);
266 if (is_owner) {
267 /* get and put user names in the listbox */
268 setpwent ();
269 while ((chl_pass = getpwent ())) {
270 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0,
271 chl_pass->pw_name, NULL);
273 endpwent ();
274 fe = listbox_search_text (chl_list,
275 get_owner (sf_stat->st_uid));
276 } else {
277 /* get and put group names in the listbox */
278 setgrent ();
279 while ((chl_grp = getgrent ())) {
280 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0,
281 chl_grp->gr_name, NULL);
283 endgrent ();
284 fe = listbox_search_text (chl_list,
285 get_group (sf_stat->st_gid));
288 if (fe)
289 listbox_select_entry (chl_list, fe);
291 b_pos = chl_list->pos;
292 add_widget (chl_dlg, chl_list);
294 run_dlg (chl_dlg);
296 if (b_pos != chl_list->pos) {
297 int ok = 0;
298 if (is_owner) {
299 chl_pass = getpwnam (chl_list->current->text);
300 if (chl_pass) {
301 ok = 1;
302 sf_stat->st_uid = chl_pass->pw_uid;
304 } else {
305 chl_grp = getgrnam (chl_list->current->text);
306 if (chl_grp) {
307 sf_stat->st_gid = chl_grp->gr_gid;
308 ok = 1;
311 if (ok) {
312 ch_flags[f_pos + 6] = '+';
313 update_ownership ();
315 dlg_focus (h);
316 if (ok)
317 print_flags ();
319 if (chl_dlg->ret_value == KEY_LEFT) {
320 if (!is_owner)
321 chl_end = 1;
322 dlg_one_up (ch_dlg);
323 f_pos--;
324 } else if (chl_dlg->ret_value == KEY_RIGHT) {
325 if (is_owner)
326 chl_end = 1;
327 dlg_one_down (ch_dlg);
328 f_pos++;
330 /* Here we used to redraw the window */
331 destroy_dlg (chl_dlg);
332 } while (chl_end);
335 static void chown_refresh (void)
337 common_dialog_repaint (ch_dlg);
339 attrset (COLOR_NORMAL);
341 dlg_move (ch_dlg, BY - 1, 8);
342 addstr (_("owner"));
343 dlg_move (ch_dlg, BY - 1, 16);
344 addstr (_("group"));
345 dlg_move (ch_dlg, BY - 1, 24);
346 addstr (_("other"));
348 dlg_move (ch_dlg, BY - 1, 35);
349 addstr (_("owner"));
350 dlg_move (ch_dlg, BY - 1, 53);
351 addstr (_("group"));
353 dlg_move (ch_dlg, 3, 4);
354 addstr (_("On"));
355 dlg_move (ch_dlg, BY + 1, 4);
356 addstr (_("Flag"));
357 dlg_move (ch_dlg, BY + 2, 4);
358 addstr (_("Mode"));
360 if (!single_set){
361 dlg_move (ch_dlg, 3, 54);
362 tty_printf (_("%6d of %d"),
363 files_on_begin - (current_panel->marked) + 1,
364 files_on_begin);
367 print_flags ();
370 static void chown_info_update (void)
372 /* display file info */
373 attrset (COLOR_NORMAL);
375 /* name && mode */
376 dlg_move (ch_dlg, 3, 8);
377 addstr (str_fit_to_term (fname, 45, J_LEFT_FIT));
378 dlg_move (ch_dlg, BY + 2, 9);
379 tty_printf ("%12o", get_mode ());
381 /* permissions */
382 update_permissions ();
385 static void b_setpos (int f_pos) {
386 b_att[0]->hotpos=-1;
387 b_att[1]->hotpos=-1;
388 b_att[2]->hotpos=-1;
389 b_att[f_pos]->hotpos = (flag_pos % 3);
392 static cb_ret_t
393 advanced_chown_callback (Dlg_head *h, dlg_msg_t msg, int parm)
395 int i = 0, f_pos = BUTTONS - h->current->dlg_id - single_set - 1;
397 switch (msg) {
398 case DLG_DRAW:
399 chown_refresh ();
400 chown_info_update ();
401 return MSG_HANDLED;
403 case DLG_POST_KEY:
404 if (f_pos < 3)
405 b_setpos (f_pos);
406 return MSG_HANDLED;
408 case DLG_FOCUS:
409 if (f_pos < 3) {
410 if ((flag_pos / 3) != f_pos)
411 flag_pos = f_pos * 3;
412 b_setpos (f_pos);
413 } else if (f_pos < 5)
414 flag_pos = f_pos + 6;
415 return MSG_HANDLED;
417 case DLG_KEY:
418 switch (parm) {
420 case XCTRL ('b'):
421 case KEY_LEFT:
422 if (f_pos < 5)
423 return (dec_flag_pos (f_pos));
424 break;
426 case XCTRL ('f'):
427 case KEY_RIGHT:
428 if (f_pos < 5)
429 return (inc_flag_pos (f_pos));
430 break;
432 case ' ':
433 if (f_pos < 3)
434 return MSG_HANDLED;
435 break;
437 case '\n':
438 case KEY_ENTER:
439 if (f_pos <= 2 || f_pos >= 5)
440 break;
441 do_enter_key (h, f_pos);
442 return MSG_HANDLED;
444 case ALT ('x'):
445 i++;
447 case ALT ('w'):
448 i++;
450 case ALT ('r'):
451 parm = i + 3;
452 for (i = 0; i < 3; i++)
453 ch_flags[i * 3 + parm - 3] =
454 (x_toggle & (1 << parm)) ? '-' : '+';
455 x_toggle ^= (1 << parm);
456 update_mode (h);
457 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
458 send_message (h->current, WIDGET_FOCUS, 0);
459 break;
461 case XCTRL ('x'):
462 i++;
464 case XCTRL ('w'):
465 i++;
467 case XCTRL ('r'):
468 parm = i;
469 for (i = 0; i < 3; i++)
470 ch_flags[i * 3 + parm] =
471 (x_toggle & (1 << parm)) ? '-' : '+';
472 x_toggle ^= (1 << parm);
473 update_mode (h);
474 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
475 send_message (h->current, WIDGET_FOCUS, 0);
476 break;
478 case 'x':
479 i++;
481 case 'w':
482 i++;
484 case 'r':
485 if (f_pos > 2)
486 break;
487 flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */
488 if (((WButton *) h->current)->text.start[(flag_pos % 3)] ==
489 '-')
490 ch_flags[flag_pos] = '+';
491 else
492 ch_flags[flag_pos] = '-';
493 update_mode (h);
494 break;
496 case '4':
497 i++;
499 case '2':
500 i++;
502 case '1':
503 if (f_pos > 2)
504 break;
505 flag_pos = i + f_pos * 3;
506 ch_flags[flag_pos] = '=';
507 update_mode (h);
508 break;
510 case '-':
511 if (f_pos > 2)
512 break;
514 case '*':
515 if (parm == '*')
516 parm = '=';
518 case '=':
519 case '+':
520 if (f_pos > 4)
521 break;
522 ch_flags[flag_pos] = parm;
523 update_mode (h);
524 advanced_chown_callback (h, DLG_KEY, KEY_RIGHT);
525 if (flag_pos > 8 || !(flag_pos % 3))
526 dlg_one_down (h);
528 break;
530 return MSG_NOT_HANDLED;
532 default:
533 return default_dlg_callback (h, msg, parm);
537 static void
538 init_chown_advanced (void)
540 int i;
541 enum { dlg_h = 13, dlg_w = 74, n_elem = 4 };
542 #ifdef ENABLE_NLS
543 static int i18n_len = 0;
545 if (i18n_len == 0) {
546 int dx, cx;
547 for (i = 0 ; i < n_elem ; i++) {
548 chown_advanced_but[i].text = _(chown_advanced_but[i].text);
549 i18n_len += str_term_width1 (chown_advanced_but[i].text) + 3;
550 if (DEFPUSH_BUTTON == chown_advanced_but[i].flags)
551 i18n_len += 2; /* "<>" */
553 cx = dx = (dlg_w - i18n_len - 2) / (n_elem + 1);
555 /* Reversed order */
556 for (i = n_elem - 1; i >= 0; i--) {
557 chown_advanced_but[i].x = cx;
558 cx += str_term_width1 (chown_advanced_but[i].text) + 3 + dx;
561 #endif /* ENABLE_NLS */
563 sf_stat = g_new (struct stat, 1);
564 do_refresh ();
565 end_chown = need_update = current_file = 0;
566 single_set = (current_panel->marked < 2) ? 2 : 0;
567 memset (ch_flags, '=', 11);
568 flag_pos = 0;
569 x_toggle = 070;
571 ch_dlg =
572 create_dlg (0, 0, dlg_h, dlg_w, dialog_colors, advanced_chown_callback,
573 "[Advanced Chown]", _(" Chown advanced command "),
574 DLG_CENTER | DLG_REVERSE);
576 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
577 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, \
578 (chown_advanced_but[i].text), 0
580 for (i = 0; i < BUTTONS - 5; i++)
581 if (!single_set || i < 2)
582 add_widget (ch_dlg, button_new (XTRACT (i)));
584 b_att[0] = button_new (XTRACT (8));
585 b_att[1] = button_new (XTRACT (7));
586 b_att[2] = button_new (XTRACT (6));
587 b_user = button_new (XTRACT (5));
588 b_group = button_new (XTRACT (4));
590 add_widget (ch_dlg, b_group);
591 add_widget (ch_dlg, b_user);
592 add_widget (ch_dlg, b_att[2]);
593 add_widget (ch_dlg, b_att[1]);
594 add_widget (ch_dlg, b_att[0]);
597 static void
598 chown_advanced_done (void)
600 g_free (sf_stat);
601 if (need_update)
602 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
603 repaint_screen ();
606 #if 0
607 static inline void do_chown (uid_t u, gid_t g)
609 chown (current_panel->dir.list[current_file].fname, u, g);
610 file_mark (current_panel, current_file, 0);
612 #endif
614 static char *next_file (void)
616 while (!current_panel->dir.list[current_file].f.marked)
617 current_file++;
619 return current_panel->dir.list[current_file].fname;
622 static void apply_advanced_chowns (struct stat *sf)
624 char *fname;
625 gid_t a_gid = sf->st_gid;
626 uid_t a_uid = sf->st_uid;
628 fname = current_panel->dir.list[current_file].fname;
629 need_update = end_chown = 1;
630 if (mc_chmod (fname, get_mode ()) == -1)
631 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
632 fname, unix_error_string (errno));
633 /* call mc_chown only, if mc_chmod didn't fail */
634 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf->st_uid : (uid_t) -1,
635 (ch_flags[10] == '+') ? sf->st_gid : (gid_t) -1) == -1)
636 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
637 fname, unix_error_string (errno));
638 do_file_mark (current_panel, current_file, 0);
640 do {
641 fname = next_file ();
643 if (mc_stat (fname, sf) != 0)
644 break;
645 ch_cmode = sf->st_mode;
646 if (mc_chmod (fname, get_mode ()) == -1)
647 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
648 fname, unix_error_string (errno));
649 /* call mc_chown only, if mc_chmod didn't fail */
650 else if (mc_chown (fname, (ch_flags[9] == '+') ? a_uid : (uid_t) -1,
651 (ch_flags[10] == '+') ? a_gid : (gid_t) -1) == -1)
652 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
653 fname, unix_error_string (errno));
655 do_file_mark (current_panel, current_file, 0);
656 } while (current_panel->marked);
659 void
660 chown_advanced_cmd (void)
663 files_on_begin = current_panel->marked;
665 do { /* do while any files remaining */
666 init_chown_advanced ();
668 if (current_panel->marked)
669 fname = next_file (); /* next marked file */
670 else
671 fname = selection (current_panel)->fname; /* single file */
673 if (mc_stat (fname, sf_stat) != 0) { /* get status of file */
674 destroy_dlg (ch_dlg);
675 break;
677 ch_cmode = sf_stat->st_mode;
679 chown_refresh ();
681 update_ownership ();
683 /* game can begin */
684 run_dlg (ch_dlg);
686 switch (ch_dlg->ret_value) {
687 case B_CANCEL:
688 end_chown = 1;
689 break;
691 case B_ENTER:
692 need_update = 1;
693 if (mc_chmod (fname, get_mode ()) == -1)
694 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
695 fname, unix_error_string (errno));
696 /* call mc_chown only, if mc_chmod didn't fail */
697 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf_stat->st_uid : (uid_t) -1,
698 (ch_flags[10] == '+') ? sf_stat->st_gid : (gid_t) -1) == -1)
699 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
700 fname, unix_error_string (errno));
701 break;
702 case B_SETALL:
703 apply_advanced_chowns (sf_stat);
704 break;
706 case B_SKIP:
707 break;
711 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL) {
712 do_file_mark (current_panel, current_file, 0);
713 need_update = 1;
715 destroy_dlg (ch_dlg);
716 } while (current_panel->marked && !end_chown);
718 chown_advanced_done ();