* global.h: Move fcntl.h inclusion here. Define O_BINARY.
[midnight-commander.git] / src / achown.c
blob280b0d3d1f68e88d1fc9a13c3293b11c1cf92229
1 /* Chown-advanced command -- for the Midnight Commander
2 Copyright (C) 1994, 1995 Radek Doulik
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include <config.h>
20 /* Needed for the extern declarations of integer parameters */
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h> /* For errno on SunOS systems */
30 #include "global.h"
31 #include "tty.h"
32 #include "win.h"
33 #include "color.h"
34 #include "dlg.h"
35 #include "widget.h"
36 #include "dialog.h" /* For do_refresh() */
37 #include "wtools.h" /* For init_box_colors() */
38 #include "key.h" /* XCTRL and ALT macros */
40 #include "dir.h"
41 #include "panel.h" /* Needed for the externs */
42 #include "file.h"
43 #include "chmod.h"
44 #include "main.h"
45 #include "achown.h"
46 #include "../vfs/vfs.h"
48 #define BX 5
49 #define BY 6
51 #define TX 50
52 #define TY 2
54 #define BUTTONS 9
56 #define B_SETALL B_USER
57 #define B_SKIP B_USER + 1
59 #define B_OWN B_USER + 3
60 #define B_GRP B_USER + 4
61 #define B_OTH B_USER + 5
62 #define B_OUSER B_USER + 6
63 #define B_OGROUP B_USER + 7
65 static struct Dlg_head *ch_dlg;
67 static struct {
68 int ret_cmd, flags, y, x;
69 char *text;
70 } chown_advanced_but [BUTTONS] = {
71 { B_CANCEL, NORMAL_BUTTON, 4, 53, N_("&Cancel") },
72 { B_ENTER, DEFPUSH_BUTTON,4, 40, N_("&Set") },
73 { B_SKIP, NORMAL_BUTTON, 4, 23, N_("S&kip") },
74 { B_SETALL, NORMAL_BUTTON, 4, 0, N_("Set &all")},
75 { B_ENTER, NARROW_BUTTON, 0, 47, " "},
76 { B_ENTER, NARROW_BUTTON, 0, 29, " "},
77 { B_ENTER, NARROW_BUTTON, 0, 19, " "},
78 { B_ENTER, NARROW_BUTTON, 0, 11, " "},
79 { B_ENTER, NARROW_BUTTON, 0, 3, " "},
82 static WButton *b_att[3]; /* permission */
83 static WButton *b_user, *b_group; /* owner */
85 static int files_on_begin; /* Number of files at startup */
86 static int flag_pos;
87 static int x_toggle;
88 static char ch_flags[11];
89 static const char ch_perm[] = "rwx";
90 static umode_t ch_cmode;
91 static struct stat *sf_stat;
92 static int need_update;
93 static int end_chown;
94 static int current_file;
95 static int single_set;
96 static char *fname;
98 static void get_ownership (void)
99 { /* set buttons - ownership */
100 char *name_t;
102 name_t = name_trunc (get_owner (sf_stat->st_uid), 15);
103 memset (b_user->text, ' ', 15);
104 strncpy (b_user->text, name_t, strlen (name_t));
105 name_t = name_trunc (get_group (sf_stat->st_gid), 15);
106 memset (b_group->text, ' ', 15);
107 strncpy (b_group->text, name_t, strlen (name_t));
111 static int inc_flag_pos (int f_pos)
113 if (flag_pos == 10) {
114 flag_pos = 0;
115 return 0;
117 flag_pos++;
118 if (!(flag_pos % 3) || f_pos > 2)
119 return 0;
120 return 1;
123 static int dec_flag_pos (int f_pos)
125 if (!flag_pos) {
126 flag_pos = 10;
127 return 0;
129 flag_pos--;
130 if (!((flag_pos + 1) % 3) || f_pos > 2)
131 return 0;
132 return 1;
135 static void set_perm_by_flags (char *s, int f_p)
137 int i;
139 for (i = 0; i < 3; i++) {
140 if (ch_flags[f_p + i] == '+')
141 s[i] = ch_perm[i];
142 else if (ch_flags[f_p + i] == '-')
143 s[i] = '-';
144 else
145 s[i] = (ch_cmode & (1 << (8 - f_p - i))) ? ch_perm[i] : '-';
149 static umode_t get_perm (char *s, int base)
151 umode_t m;
153 m = 0;
154 m |= (s [0] == '-') ? 0 :
155 ((s[0] == '+') ? (1 << (base + 2)) : (1 << (base + 2)) & ch_cmode);
157 m |= (s [1] == '-') ? 0 :
158 ((s[1] == '+') ? (1 << (base + 1)) : (1 << (base + 1)) & ch_cmode);
160 m |= (s [2] == '-') ? 0 :
161 ((s[2] == '+') ? (1 << base) : (1 << base) & ch_cmode);
163 return m;
166 static umode_t get_mode (void)
168 umode_t m;
170 m = ch_cmode ^ (ch_cmode & 0777);
171 m |= get_perm (ch_flags, 6);
172 m |= get_perm (ch_flags + 3, 3);
173 m |= get_perm (ch_flags + 6, 0);
175 return m;
178 static void print_flags (void)
180 int i;
182 attrset (COLOR_NORMAL);
184 for (i = 0; i < 3; i++){
185 dlg_move (ch_dlg, BY+1, 9+i);
186 addch (ch_flags [i]);
189 for (i = 0; i < 3; i++){
190 dlg_move (ch_dlg, BY + 1, 17 + i);
191 addch (ch_flags [i+3]);
194 for (i = 0; i < 3; i++){
195 dlg_move (ch_dlg, BY + 1, 25 + i);
196 addch (ch_flags [i+6]);
199 set_perm_by_flags (b_att[0]->text, 0);
200 set_perm_by_flags (b_att[1]->text, 3);
201 set_perm_by_flags (b_att[2]->text, 6);
203 for (i = 0; i < 15; i++){
204 dlg_move (ch_dlg, BY+1, 35+i);
205 addch (ch_flags[9]);
207 for (i = 0; i < 15; i++){
208 dlg_move (ch_dlg, BY + 1, 53 + i);
209 addch (ch_flags[10]);
213 static void update_mode (Dlg_head * h)
215 print_flags ();
216 attrset (COLOR_NORMAL);
217 dlg_move (h, BY + 2, 9);
218 printw ("%12o", get_mode ());
219 send_message (h, h->current->widget, WIDGET_FOCUS, 0);
222 static int l_call (void *data)
224 return 1;
227 static int chl_callback (Dlg_head * h, int Par, int Msg)
229 switch (Msg) {
230 case DLG_DRAW:
231 common_dialog_repaint (h);
232 break;
234 case DLG_KEY:
235 switch (Par) {
236 case KEY_LEFT:
237 case KEY_RIGHT:
238 h->ret_value = Par;
239 dlg_stop (h);
242 return 0;
245 static void
246 do_enter_key (Dlg_head * h, int f_pos)
248 Dlg_head *chl_dlg;
249 WListbox *chl_list;
250 struct passwd *chl_pass;
251 struct group *chl_grp;
252 WLEntry *fe;
253 int lxx, lyy, chl_end, b_pos;
254 int is_owner;
255 char *title;
257 do {
258 is_owner = (f_pos == 3);
259 title = is_owner ? _("owner") : _("group");
261 lxx = (COLS - 74) / 2 + (is_owner ? 35 : 53);
262 lyy = (LINES - 13) / 2;
263 chl_end = 0;
265 chl_dlg =
266 create_dlg (lyy, lxx, 13, 17, dialog_colors, chl_callback,
267 "[Advanced Chown]", title, DLG_COMPACT);
269 /* get new listboxes */
270 chl_list = listbox_new (1, 1, 15, 11, 0, l_call, NULL);
272 listbox_add_item (chl_list, 0, 0, "<Unknown>", NULL);
274 if (is_owner) {
275 /* get and put user names in the listbox */
276 setpwent ();
277 while ((chl_pass = getpwent ()))
278 listbox_add_item (chl_list, 0, 0, chl_pass->pw_name, NULL);
279 endpwent ();
280 fe = listbox_search_text (chl_list,
281 get_owner (sf_stat->st_uid));
282 } else {
283 /* get and put group names in the listbox */
284 setgrent ();
285 while ((chl_grp = getgrent ())) {
286 listbox_add_item (chl_list, 0, 0, chl_grp->gr_name, NULL);
288 endgrent ();
289 fe = listbox_search_text (chl_list,
290 get_group (sf_stat->st_gid));
293 if (fe)
294 listbox_select_entry (chl_list, fe);
296 b_pos = chl_list->pos;
297 add_widget (chl_dlg, chl_list);
299 run_dlg (chl_dlg);
301 if (b_pos != chl_list->pos) {
302 int ok = 0;
303 if (is_owner) {
304 chl_pass = getpwnam (chl_list->current->text);
305 if (chl_pass) {
306 ok = 1;
307 sf_stat->st_uid = chl_pass->pw_uid;
309 } else {
310 chl_grp = getgrnam (chl_list->current->text);
311 if (chl_grp) {
312 sf_stat->st_gid = chl_grp->gr_gid;
313 ok = 1;
316 if (ok) {
317 ch_flags[f_pos + 6] = '+';
318 get_ownership ();
320 dlg_focus (h);
321 if (ok)
322 print_flags ();
324 if (chl_dlg->ret_value == KEY_LEFT) {
325 if (!is_owner)
326 chl_end = 1;
327 dlg_one_up (ch_dlg);
328 f_pos--;
329 } else if (chl_dlg->ret_value == KEY_RIGHT) {
330 if (is_owner)
331 chl_end = 1;
332 dlg_one_down (ch_dlg);
333 f_pos++;
335 /* Here we used to redraw the window */
336 destroy_dlg (chl_dlg);
337 } while (chl_end);
340 static void chown_refresh (void)
342 common_dialog_repaint (ch_dlg);
344 attrset (COLOR_NORMAL);
346 dlg_move (ch_dlg, BY - 1, 8);
347 addstr (_("owner"));
348 dlg_move (ch_dlg, BY - 1, 16);
349 addstr (_("group"));
350 dlg_move (ch_dlg, BY - 1, 24);
351 addstr (_("other"));
353 dlg_move (ch_dlg, BY - 1, 35);
354 addstr (_("owner"));
355 dlg_move (ch_dlg, BY - 1, 53);
356 addstr (_("group"));
358 dlg_move (ch_dlg, 3, 4);
359 addstr (_("On"));
360 dlg_move (ch_dlg, BY + 1, 4);
361 addstr (_("Flag"));
362 dlg_move (ch_dlg, BY + 2, 4);
363 addstr (_("Mode"));
365 if (!single_set){
366 dlg_move (ch_dlg, 3, 54);
367 printw (_("%6d of %d"), files_on_begin - (cpanel->marked) + 1,
368 files_on_begin);
371 print_flags ();
374 static void chown_info_update (void)
376 /* display file info */
377 attrset (COLOR_NORMAL);
379 /* name && mode */
380 dlg_move (ch_dlg, 3, 8);
381 printw ("%s", name_trunc (fname, 45));
382 dlg_move (ch_dlg, BY + 2, 9);
383 printw ("%12o", get_mode ());
385 /* permissions */
386 set_perm_by_flags (b_att[0]->text, 0);
387 set_perm_by_flags (b_att[1]->text, 3);
388 set_perm_by_flags (b_att[2]->text, 6);
391 static void b_setpos (int f_pos) {
392 b_att[0]->hotpos=-1;
393 b_att[1]->hotpos=-1;
394 b_att[2]->hotpos=-1;
395 b_att[f_pos]->hotpos = (flag_pos % 3);
398 static int advanced_chown_callback (Dlg_head * h, int Par, int Msg)
400 int i = 0, f_pos = BUTTONS - h->current->dlg_id - single_set - 1;
402 switch (Msg) {
403 case DLG_DRAW:
404 chown_refresh ();
405 chown_info_update ();
406 return 1;
408 case DLG_POST_KEY:
409 if (f_pos < 3)
410 b_setpos (f_pos);
411 break;
413 case DLG_FOCUS:
414 if (f_pos < 3) {
415 if ((flag_pos / 3) != f_pos)
416 flag_pos = f_pos * 3;
417 b_setpos (f_pos);
418 } else if (f_pos < 5)
419 flag_pos = f_pos + 6;
420 break;
422 case DLG_KEY:
423 switch (Par) {
425 case XCTRL('b'):
426 case KEY_LEFT:
427 if (f_pos < 5)
428 return (dec_flag_pos (f_pos));
429 break;
431 case XCTRL('f'):
432 case KEY_RIGHT:
433 if (f_pos < 5)
434 return (inc_flag_pos (f_pos));
435 break;
437 case ' ':
438 if (f_pos < 3)
439 return 1;
440 break;
442 case '\n':
443 case KEY_ENTER:
444 if (f_pos <= 2 || f_pos >= 5)
445 break;
446 do_enter_key (h, f_pos);
447 return 1;
449 case ALT ('x'):
450 i++;
452 case ALT ('w'):
453 i++;
455 case ALT ('r'):
456 Par = i + 3;
457 for (i = 0; i < 3; i++)
458 ch_flags[i * 3 + Par - 3] = (x_toggle & (1 << Par)) ? '-' : '+';
459 x_toggle ^= (1 << Par);
460 update_mode (h);
461 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
462 send_message (h, h->current->widget, WIDGET_FOCUS, 0);
463 break;
465 case XCTRL ('x'):
466 i++;
468 case XCTRL ('w'):
469 i++;
471 case XCTRL ('r'):
472 Par = i;
473 for (i = 0; i < 3; i++)
474 ch_flags[i * 3 + Par] = (x_toggle & (1 << Par)) ? '-' : '+';
475 x_toggle ^= (1 << Par);
476 update_mode (h);
477 dlg_broadcast_msg (h, WIDGET_DRAW, 0);
478 send_message (h, h->current->widget, WIDGET_FOCUS, 0);
479 break;
481 case 'x':
482 i++;
484 case 'w':
485 i++;
487 case 'r':
488 if (f_pos > 2)
489 break;
490 flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,Par)-ch_perm); */
491 if (((WButton *) h->current->widget)->text[(flag_pos % 3)] == '-')
492 ch_flags[flag_pos] = '+';
493 else
494 ch_flags[flag_pos] = '-';
495 update_mode (h);
496 break;
498 case '4':
499 i++;
501 case '2':
502 i++;
504 case '1':
505 if (f_pos > 2)
506 break;
507 flag_pos = i + f_pos * 3;
508 ch_flags[flag_pos] = '=';
509 update_mode (h);
510 break;
512 case '-':
513 if (f_pos > 2)
514 break;
516 case '*':
517 if (Par == '*')
518 Par = '=';
520 case '=':
521 case '+':
522 if (f_pos > 4)
523 break;
524 ch_flags[flag_pos] = Par;
525 update_mode (h);
526 advanced_chown_callback (h, KEY_RIGHT, DLG_KEY);
527 if (flag_pos>8 || !(flag_pos%3)) dlg_one_down (h);
529 break;
531 return 0;
533 return 0;
536 static void
537 init_chown_advanced (void)
539 int i;
541 sf_stat = g_new (struct stat, 1);
542 do_refresh ();
543 end_chown = need_update = current_file = 0;
544 single_set = (cpanel->marked < 2) ? 2 : 0;
545 memset (ch_flags, '=', 11);
546 flag_pos = 0;
547 x_toggle = 070;
549 ch_dlg =
550 create_dlg (0, 0, 13, 74, dialog_colors, advanced_chown_callback,
551 "[Advanced Chown]", _(" Chown advanced command "),
552 DLG_CENTER);
554 #define XTRACT(i) BY+chown_advanced_but[i].y, BX+chown_advanced_but[i].x, \
555 chown_advanced_but[i].ret_cmd, chown_advanced_but[i].flags, _(chown_advanced_but[i].text), \
556 0, 0, NULL
558 for (i = 0; i < BUTTONS - 5; i++)
559 if (!single_set || i < 2)
560 add_widget (ch_dlg, button_new (XTRACT (i)));
562 b_att[0] = button_new (XTRACT (8));
563 b_att[1] = button_new (XTRACT (7));
564 b_att[2] = button_new (XTRACT (6));
565 b_user = button_new (XTRACT (5));
566 b_group = button_new (XTRACT (4));
568 add_widget (ch_dlg, b_group);
569 add_widget (ch_dlg, b_user);
570 add_widget (ch_dlg, b_att[2]);
571 add_widget (ch_dlg, b_att[1]);
572 add_widget (ch_dlg, b_att[0]);
575 static void
576 chown_advanced_done (void)
578 g_free (sf_stat);
579 if (need_update)
580 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
581 repaint_screen ();
584 #if 0
585 static inline void do_chown (uid_t u, gid_t g)
587 chown (cpanel->dir.list[current_file].fname, u, g);
588 file_mark (cpanel, current_file, 0);
590 #endif
592 static char *next_file (void)
594 while (!cpanel->dir.list[current_file].f.marked)
595 current_file++;
597 return cpanel->dir.list[current_file].fname;
600 static void apply_advanced_chowns (struct stat *sf)
602 char *fname;
603 gid_t a_gid = sf->st_gid;
604 uid_t a_uid = sf->st_uid;
606 fname = cpanel->dir.list[current_file].fname;
607 need_update = end_chown = 1;
608 if (mc_chmod (fname, get_mode ()) == -1)
609 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
610 fname, unix_error_string (errno));
611 /* call mc_chown only, if mc_chmod didn't fail */
612 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf->st_uid : -1,
613 (ch_flags[10] == '+') ? sf->st_gid : -1) == -1)
614 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
615 fname, unix_error_string (errno));
616 do_file_mark (cpanel, current_file, 0);
618 do {
619 fname = next_file ();
621 if (!stat_file (fname, sf))
622 break;
623 ch_cmode = sf->st_mode;
624 if (mc_chmod (fname, get_mode ()) == -1)
625 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
626 fname, unix_error_string (errno));
627 /* call mc_chown only, if mc_chmod didn't fail */
628 else if (mc_chown (fname, (ch_flags[9] == '+') ? a_uid : -1, (ch_flags[10] == '+') ? a_gid : -1) == -1)
629 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
630 fname, unix_error_string (errno));
632 do_file_mark (cpanel, current_file, 0);
633 } while (cpanel->marked);
636 void
637 chown_advanced_cmd (void)
640 files_on_begin = cpanel->marked;
642 do { /* do while any files remaining */
643 init_chown_advanced ();
645 if (cpanel->marked)
646 fname = next_file (); /* next marked file */
647 else
648 fname = selection (cpanel)->fname; /* single file */
650 if (!stat_file (fname, sf_stat)){ /* get status of file */
651 destroy_dlg (ch_dlg);
652 break;
654 ch_cmode = sf_stat->st_mode;
656 chown_refresh ();
658 get_ownership ();
660 /* game can begin */
661 run_dlg (ch_dlg);
663 switch (ch_dlg->ret_value) {
664 case B_CANCEL:
665 end_chown = 1;
666 break;
668 case B_ENTER:
669 need_update = 1;
670 if (mc_chmod (fname, get_mode ()) == -1)
671 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
672 fname, unix_error_string (errno));
673 /* call mc_chown only, if mc_chmod didn't fail */
674 else if (mc_chown (fname, (ch_flags[9] == '+') ? sf_stat->st_uid : -1, (ch_flags[10] == '+') ? sf_stat->st_gid : -1) == -1)
675 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
676 fname, unix_error_string (errno));
677 break;
678 case B_SETALL:
679 apply_advanced_chowns (sf_stat);
680 break;
682 case B_SKIP:
683 break;
687 if (cpanel->marked && ch_dlg->ret_value != B_CANCEL) {
688 do_file_mark (cpanel, current_file, 0);
689 need_update = 1;
691 destroy_dlg (ch_dlg);
692 } while (cpanel->marked && !end_chown);
694 chown_advanced_done ();