Ticket #1443: show average copying velocity/ETA for whole operation.
[midnight-commander.git] / src / chown.c
blob0d922b0a285e7d446f44bfc6e0a9a09dffc8ea25
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.
20 /** \file chown.c
21 * \brief Source: chown command
24 #include <config.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <pwd.h>
30 #include <grp.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/skin.h"
39 #include "lib/vfs/mc-vfs/vfs.h"
40 #include "lib/strutil.h"
42 #include "dialog.h"
43 #include "widget.h"
45 /* Needed for the extern declarations of integer parameters */
46 #include "dir.h"
47 #include "panel.h" /* Needed for the externs */
48 #include "chmod.h"
49 #include "main.h" /* update_panels() */
50 #include "layout.h" /* repaint_screen() */
51 #include "chown.h"
52 #include "wtools.h" /* For init_box_colors */
54 #define UX 5
55 #define UY 2
57 #define GX 27
58 #define GY 2
60 #define BX 5
61 #define BY 15
63 #define TX 50
64 #define TY 2
66 #define BUTTONS 5
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;
77 static struct {
78 int ret_cmd, flags, y, x;
79 const char *text;
80 } chown_but[BUTTONS] = {
81 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
82 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
83 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
84 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
85 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
88 #define LABELS 5
89 static struct {
90 int y, x;
91 WLabel *l;
92 } chown_label [LABELS] = {
93 { TY+2, TX+2, NULL },
94 { TY+4, TX+2, NULL },
95 { TY+6, TX+2, NULL },
96 { TY+8, TX+2, NULL },
97 { TY+10,TX+2, NULL }
100 static void
101 chown_refresh (Dlg_head *h)
103 common_dialog_repaint (h);
105 tty_setcolor (COLOR_NORMAL);
107 draw_box (h, UY, UX, 12, 21);
108 draw_box (h, GY, GX, 12, 21);
109 draw_box (h, TY, TX, 12, 19);
111 dlg_move (h, TY + 1, TX + 1);
112 tty_print_string (_(" Name "));
113 dlg_move (h, TY + 3, TX + 1);
114 tty_print_string (_(" Owner name "));
115 dlg_move (h, TY + 5, TX + 1);
116 tty_print_string (_(" Group name "));
117 dlg_move (h, TY + 7, TX + 1);
118 tty_print_string (_(" Size "));
119 dlg_move (h, TY + 9, TX + 1);
120 tty_print_string (_(" Permission "));
122 tty_setcolor (COLOR_HOT_NORMAL);
123 dlg_move (h, UY, UX + 1);
124 tty_print_string (_(" User name "));
125 dlg_move (h, GY, GX + 1);
126 tty_print_string (_(" Group name "));
127 dlg_move (h, TY, TX + 1);
128 tty_print_string (_(" File "));
131 static char *
132 next_file (void)
134 while (!current_panel->dir.list[current_file].f.marked)
135 current_file++;
137 return current_panel->dir.list[current_file].fname;
140 static cb_ret_t
141 chown_callback (Dlg_head *h, Widget *sender,
142 dlg_msg_t msg, int parm, void *data)
144 switch (msg) {
145 case DLG_DRAW:
146 chown_refresh (h);
147 return MSG_HANDLED;
149 default:
150 return default_dlg_callback (h, sender, msg, parm, data);
154 static Dlg_head *
155 init_chown (void)
157 int i;
158 struct passwd *l_pass;
159 struct group *l_grp;
160 Dlg_head *ch_dlg;
162 do_refresh ();
163 end_chown = need_update = current_file = 0;
164 single_set = (current_panel->marked < 2) ? 3 : 0;
166 ch_dlg =
167 create_dlg (0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
168 _(" Chown command "), DLG_CENTER | DLG_REVERSE);
170 for (i = 0; i < BUTTONS - single_set; i++)
171 add_widget (ch_dlg,
172 button_new (BY + chown_but[i].y, BX + chown_but[i].x,
173 chown_but[i].ret_cmd, chown_but[i].flags,
174 _(chown_but[i].text), 0));
176 /* Add the widgets for the file information */
177 for (i = 0; i < LABELS; i++) {
178 chown_label[i].l =
179 label_new (chown_label[i].y, chown_label[i].x, "");
180 add_widget (ch_dlg, chown_label[i].l);
183 /* get new listboxes */
184 l_user = listbox_new (UY + 1, UX + 1, 10, 19, FALSE, NULL);
185 l_group = listbox_new (GY + 1, GX + 1, 10, 19, FALSE, NULL);
187 /* add fields for unknown names (numbers) */
188 listbox_add_item (l_user, LISTBOX_APPEND_AT_END, 0, _("<Unknown user>"), NULL);
189 listbox_add_item (l_group, LISTBOX_APPEND_AT_END, 0, _("<Unknown group>"), NULL);
191 /* get and put user names in the listbox */
192 setpwent ();
193 while ((l_pass = getpwent ())) {
194 listbox_add_item (l_user, LISTBOX_APPEND_SORTED, 0, l_pass->pw_name, NULL);
196 endpwent ();
198 /* get and put group names in the listbox */
199 setgrent ();
200 while ((l_grp = getgrent ())) {
201 listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL);
203 endgrent ();
205 /* add listboxes to the dialogs */
206 add_widget (ch_dlg, l_group);
207 add_widget (ch_dlg, l_user);
209 return ch_dlg;
212 static void
213 chown_done (void)
215 if (need_update)
216 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
217 repaint_screen ();
220 static void
221 do_chown (uid_t u, gid_t g)
223 if (mc_chown (current_panel->dir.list [current_file].fname, u, g) == -1)
224 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
225 current_panel->dir.list [current_file].fname, unix_error_string (errno));
227 do_file_mark (current_panel, current_file, 0);
230 static void
231 apply_chowns (uid_t u, gid_t g)
233 char *fname;
235 need_update = end_chown = 1;
236 do_chown (u,g);
238 do {
239 fname = next_file ();
241 do_chown (u,g);
242 } while (current_panel->marked);
245 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
247 void
248 chown_cmd (void)
250 char *fname;
251 struct stat sf_stat;
252 WLEntry *fe;
253 Dlg_head *ch_dlg;
254 uid_t new_user;
255 gid_t new_group;
256 char buffer [BUF_TINY];
258 do { /* do while any files remaining */
259 ch_dlg = init_chown ();
260 new_user = new_group = -1;
262 if (current_panel->marked)
263 fname = next_file (); /* next marked file */
264 else
265 fname = selection (current_panel)->fname; /* single file */
267 if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
268 destroy_dlg (ch_dlg);
269 break;
272 /* select in listboxes */
273 listbox_select_entry (l_user, listbox_search_text (l_user, get_owner(sf_stat.st_uid)));
274 listbox_select_entry (l_group, listbox_search_text (l_group, get_group(sf_stat.st_gid)));
276 chown_label (0, str_trunc (fname, 15));
277 chown_label (1, str_trunc (get_owner (sf_stat.st_uid), 15));
278 chown_label (2, str_trunc (get_group (sf_stat.st_gid), 15));
279 size_trunc_len (buffer, 15, sf_stat.st_size, 0);
280 chown_label (3, buffer);
281 chown_label (4, string_perm (sf_stat.st_mode));
283 switch (run_dlg (ch_dlg)) {
284 case B_CANCEL:
285 end_chown = 1;
286 break;
288 case B_SETUSR:
290 struct passwd *user;
291 char *text;
293 listbox_get_current (l_user, &text, NULL);
294 user = getpwnam (text);
295 if (user){
296 new_user = user->pw_uid;
297 apply_chowns (new_user, new_group);
299 break;
301 case B_SETGRP:
303 struct group *grp;
304 char *text;
306 listbox_get_current (l_group, &text, NULL);
307 grp = getgrnam (text);
308 if (grp){
309 new_group = grp->gr_gid;
310 apply_chowns (new_user, new_group);
312 break;
314 case B_SETALL:
315 case B_ENTER:
317 struct group *grp;
318 struct passwd *user;
319 char *text;
321 listbox_get_current (l_group, &text, NULL);
322 grp = getgrnam (text);
323 if (grp)
324 new_group = grp->gr_gid;
325 listbox_get_current (l_user, &text, NULL);
326 user = getpwnam (text);
327 if (user)
328 new_user = user->pw_uid;
329 if (ch_dlg->ret_value == B_ENTER) {
330 need_update = 1;
331 if (mc_chown (fname, new_user, new_group) == -1)
332 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
333 fname, unix_error_string (errno));
334 } else
335 apply_chowns (new_user, new_group);
336 break;
338 } /* switch */
340 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL){
341 do_file_mark (current_panel, current_file, 0);
342 need_update = 1;
344 destroy_dlg (ch_dlg);
345 } while (current_panel->marked && !end_chown);
347 chown_done ();