1 /* Chown command -- for the Midnight Commander
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 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.
21 * \brief Source: chown command
31 #include <sys/types.h>
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
39 #include "lib/vfs/mc-vfs/vfs.h"
40 #include "lib/strutil.h"
45 /* Needed for the extern declarations of integer parameters */
47 #include "panel.h" /* Needed for the externs */
49 #include "main.h" /* update_panels() */
50 #include "layout.h" /* repaint_screen() */
52 #include "wtools.h" /* For init_box_colors */
68 #define B_SETALL B_USER
69 #define B_SETUSR (B_USER + 1)
70 #define B_SETGRP (B_USER + 2)
72 static int need_update
, end_chown
;
73 static int current_file
;
74 static int single_set
;
75 static WListbox
*l_user
, *l_group
;
79 int ret_cmd
, flags
, y
, x
;
81 } chown_but
[BUTTONS
] = {
82 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel") },
83 { B_ENTER
, DEFPUSH_BUTTON
, 0, 40, N_("&Set") },
84 { B_SETUSR
, NORMAL_BUTTON
, 0, 25, N_("Set &users") },
85 { B_SETGRP
, NORMAL_BUTTON
, 0, 11, N_("Set &groups") },
86 { B_SETALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
93 } chown_label
[LABELS
] = {
103 chown_refresh (Dlg_head
* h
)
105 common_dialog_repaint (h
);
107 tty_setcolor (COLOR_NORMAL
);
109 draw_box (h
, UY
, UX
, 12, 21);
110 draw_box (h
, GY
, GX
, 12, 21);
111 draw_box (h
, TY
, TX
, 12, 19);
113 dlg_move (h
, TY
+ 1, TX
+ 1);
114 tty_print_string (_(" Name "));
115 dlg_move (h
, TY
+ 3, TX
+ 1);
116 tty_print_string (_(" Owner name "));
117 dlg_move (h
, TY
+ 5, TX
+ 1);
118 tty_print_string (_(" Group name "));
119 dlg_move (h
, TY
+ 7, TX
+ 1);
120 tty_print_string (_(" Size "));
121 dlg_move (h
, TY
+ 9, TX
+ 1);
122 tty_print_string (_(" Permission "));
124 tty_setcolor (COLOR_HOT_NORMAL
);
125 dlg_move (h
, UY
, UX
+ 1);
126 tty_print_string (_(" User name "));
127 dlg_move (h
, GY
, GX
+ 1);
128 tty_print_string (_(" Group name "));
129 dlg_move (h
, TY
, TX
+ 1);
130 tty_print_string (_(" File "));
136 while (!current_panel
->dir
.list
[current_file
].f
.marked
)
139 return current_panel
->dir
.list
[current_file
].fname
;
143 chown_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
152 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
160 struct passwd
*l_pass
;
165 end_chown
= need_update
= current_file
= 0;
166 single_set
= (current_panel
->marked
< 2) ? 3 : 0;
169 create_dlg (0, 0, 18, 74, dialog_colors
, chown_callback
, "[Chown]",
170 _(" Chown command "), DLG_CENTER
| DLG_REVERSE
);
172 for (i
= 0; i
< BUTTONS
- single_set
; i
++)
174 button_new (BY
+ chown_but
[i
].y
, BX
+ chown_but
[i
].x
,
175 chown_but
[i
].ret_cmd
, chown_but
[i
].flags
, _(chown_but
[i
].text
), 0));
177 /* Add the widgets for the file information */
178 for (i
= 0; i
< LABELS
; i
++)
180 chown_label
[i
].l
= label_new (chown_label
[i
].y
, chown_label
[i
].x
, "");
181 add_widget (ch_dlg
, chown_label
[i
].l
);
184 /* get new listboxes */
185 l_user
= listbox_new (UY
+ 1, UX
+ 1, 10, 19, FALSE
, NULL
);
186 l_group
= listbox_new (GY
+ 1, GX
+ 1, 10, 19, FALSE
, NULL
);
188 /* add fields for unknown names (numbers) */
189 listbox_add_item (l_user
, LISTBOX_APPEND_AT_END
, 0, _("<Unknown user>"), NULL
);
190 listbox_add_item (l_group
, LISTBOX_APPEND_AT_END
, 0, _("<Unknown group>"), NULL
);
192 /* get and put user names in the listbox */
194 while ((l_pass
= getpwent ()))
196 listbox_add_item (l_user
, LISTBOX_APPEND_SORTED
, 0, l_pass
->pw_name
, NULL
);
200 /* get and put group names in the listbox */
202 while ((l_grp
= getgrent ()))
204 listbox_add_item (l_group
, LISTBOX_APPEND_SORTED
, 0, l_grp
->gr_name
, NULL
);
208 /* add listboxes to the dialogs */
209 add_widget (ch_dlg
, l_group
);
210 add_widget (ch_dlg
, l_user
);
219 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
224 do_chown (uid_t u
, gid_t g
)
226 if (mc_chown (current_panel
->dir
.list
[current_file
].fname
, u
, g
) == -1)
227 message (D_ERROR
, MSG_ERROR
, _(" Cannot chown \"%s\" \n %s "),
228 current_panel
->dir
.list
[current_file
].fname
, unix_error_string (errno
));
230 do_file_mark (current_panel
, current_file
, 0);
234 apply_chowns (uid_t u
, gid_t g
)
238 need_update
= end_chown
= 1;
243 fname
= next_file ();
247 while (current_panel
->marked
);
250 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
260 char buffer
[BUF_TINY
];
263 { /* do while any files remaining */
264 ch_dlg
= init_chown ();
265 new_user
= new_group
= -1;
267 if (current_panel
->marked
)
268 fname
= next_file (); /* next marked file */
270 fname
= selection (current_panel
)->fname
; /* single file */
272 if (mc_stat (fname
, &sf_stat
) != 0)
273 { /* get status of file */
274 destroy_dlg (ch_dlg
);
278 /* select in listboxes */
279 listbox_select_entry (l_user
, listbox_search_text (l_user
, get_owner (sf_stat
.st_uid
)));
280 listbox_select_entry (l_group
, listbox_search_text (l_group
, get_group (sf_stat
.st_gid
)));
282 chown_label (0, str_trunc (fname
, 15));
283 chown_label (1, str_trunc (get_owner (sf_stat
.st_uid
), 15));
284 chown_label (2, str_trunc (get_group (sf_stat
.st_gid
), 15));
285 size_trunc_len (buffer
, 15, sf_stat
.st_size
, 0);
286 chown_label (3, buffer
);
287 chown_label (4, string_perm (sf_stat
.st_mode
));
289 switch (run_dlg (ch_dlg
))
300 listbox_get_current (l_user
, &text
, NULL
);
301 user
= getpwnam (text
);
304 new_user
= user
->pw_uid
;
305 apply_chowns (new_user
, new_group
);
314 listbox_get_current (l_group
, &text
, NULL
);
315 grp
= getgrnam (text
);
318 new_group
= grp
->gr_gid
;
319 apply_chowns (new_user
, new_group
);
330 listbox_get_current (l_group
, &text
, NULL
);
331 grp
= getgrnam (text
);
333 new_group
= grp
->gr_gid
;
334 listbox_get_current (l_user
, &text
, NULL
);
335 user
= getpwnam (text
);
337 new_user
= user
->pw_uid
;
338 if (ch_dlg
->ret_value
== B_ENTER
)
341 if (mc_chown (fname
, new_user
, new_group
) == -1)
342 message (D_ERROR
, MSG_ERROR
, _(" Cannot chown \"%s\" \n %s "),
343 fname
, unix_error_string (errno
));
346 apply_chowns (new_user
, new_group
);
351 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
)
353 do_file_mark (current_panel
, current_file
, 0);
356 destroy_dlg (ch_dlg
);
358 while (current_panel
->marked
&& !end_chown
);