Updated italian translation.
[midnight-commander.git] / src / chown.c
blob1c62e690056088aacb64e8741efb279d28f6f276
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 "chmod.h"
47 #include "main.h" /* update_panels() */
48 #include "layout.h" /* repaint_screen() */
49 #include "chown.h"
50 #include "wtools.h" /* init_box_colors() */
51 #include "setup.h" /* panels_options */
53 #define UX 5
54 #define UY 2
56 #define GX 27
57 #define GY 2
59 #define BX 5
60 #define BY 15
62 #define TX 50
63 #define TY 2
65 #define BUTTONS 5
67 #define B_SETALL B_USER
68 #define B_SETUSR (B_USER + 1)
69 #define B_SETGRP (B_USER + 2)
71 static int need_update, end_chown;
72 static int current_file;
73 static int single_set;
74 static WListbox *l_user, *l_group;
76 /* *INDENT-OFF* */
77 static struct
79 int ret_cmd, flags, y, x;
80 const char *text;
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") },
89 #define LABELS 5
90 static struct {
91 int y, x;
92 WLabel *l;
93 } chown_label [LABELS] = {
94 { TY + 2, TX + 2, NULL },
95 { TY + 4, TX + 2, NULL },
96 { TY + 6, TX + 2, NULL },
97 { TY + 8, TX + 2, NULL },
98 { TY + 10, TX + 2, NULL }
100 /* *INDENT-ON* */
102 static void
103 chown_refresh (Dlg_head * h)
105 common_dialog_repaint (h);
107 tty_setcolor (COLOR_NORMAL);
109 dlg_move (h, TY + 1, TX + 2);
110 tty_print_string (_("Name"));
111 dlg_move (h, TY + 3, TX + 2);
112 tty_print_string (_("Owner name"));
113 dlg_move (h, TY + 5, TX + 2);
114 tty_print_string (_("Group name"));
115 dlg_move (h, TY + 7, TX + 2);
116 tty_print_string (_("Size"));
117 dlg_move (h, TY + 9, TX + 2);
118 tty_print_string (_("Permission"));
121 static char *
122 next_file (void)
124 while (!current_panel->dir.list[current_file].f.marked)
125 current_file++;
127 return current_panel->dir.list[current_file].fname;
130 static cb_ret_t
131 chown_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
133 switch (msg)
135 case DLG_DRAW:
136 chown_refresh (h);
137 return MSG_HANDLED;
139 default:
140 return default_dlg_callback (h, sender, msg, parm, data);
144 static Dlg_head *
145 init_chown (void)
147 int i;
148 struct passwd *l_pass;
149 struct group *l_grp;
150 Dlg_head *ch_dlg;
152 do_refresh ();
153 end_chown = need_update = current_file = 0;
154 single_set = (current_panel->marked < 2) ? 3 : 0;
156 ch_dlg =
157 create_dlg (TRUE, 0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
158 _("Chown command"), DLG_CENTER | DLG_REVERSE);
160 for (i = 0; i < BUTTONS - single_set; i++)
161 add_widget (ch_dlg,
162 button_new (BY + chown_but[i].y, BX + chown_but[i].x,
163 chown_but[i].ret_cmd, chown_but[i].flags, _(chown_but[i].text), 0));
165 /* Add the widgets for the file information */
166 for (i = 0; i < LABELS; i++)
168 chown_label[i].l = label_new (chown_label[i].y, chown_label[i].x, "");
169 add_widget (ch_dlg, chown_label[i].l);
172 /* get new listboxes */
173 l_user = listbox_new (UY + 1, UX + 1, 10, 19, FALSE, NULL);
174 l_group = listbox_new (GY + 1, GX + 1, 10, 19, FALSE, NULL);
176 /* add fields for unknown names (numbers) */
177 listbox_add_item (l_user, LISTBOX_APPEND_AT_END, 0, _("<Unknown user>"), NULL);
178 listbox_add_item (l_group, LISTBOX_APPEND_AT_END, 0, _("<Unknown group>"), NULL);
180 /* get and put user names in the listbox */
181 setpwent ();
182 while ((l_pass = getpwent ()) != NULL)
184 listbox_add_item (l_user, LISTBOX_APPEND_SORTED, 0, l_pass->pw_name, NULL);
186 endpwent ();
188 /* get and put group names in the listbox */
189 setgrent ();
190 while ((l_grp = getgrent ()) != NULL)
192 listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL);
194 endgrent ();
196 add_widget (ch_dlg, groupbox_new (TY, TX, 12, 19, _("File")));
198 /* add listboxes to the dialogs */
199 add_widget (ch_dlg, l_group);
200 add_widget (ch_dlg, groupbox_new (GY, GX, 12, 21, _("Group name")));
201 add_widget (ch_dlg, l_user);
202 add_widget (ch_dlg, groupbox_new (UY, UX, 12, 21, _("User name")));
204 return ch_dlg;
207 static void
208 chown_done (void)
210 if (need_update)
211 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
212 repaint_screen ();
215 static void
216 do_chown (uid_t u, gid_t g)
218 if (mc_chown (current_panel->dir.list[current_file].fname, u, g) == -1)
219 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
220 current_panel->dir.list[current_file].fname, unix_error_string (errno));
222 do_file_mark (current_panel, current_file, 0);
225 static void
226 apply_chowns (uid_t u, gid_t g)
228 char *fname;
230 need_update = end_chown = 1;
231 do_chown (u, g);
235 fname = next_file ();
237 do_chown (u, g);
239 while (current_panel->marked);
242 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
244 void
245 chown_cmd (void)
247 char *fname;
248 struct stat sf_stat;
249 Dlg_head *ch_dlg;
250 uid_t new_user;
251 gid_t new_group;
252 char buffer[BUF_TINY];
255 { /* do while any files remaining */
256 ch_dlg = init_chown ();
257 new_user = new_group = -1;
259 if (current_panel->marked)
260 fname = next_file (); /* next marked file */
261 else
262 fname = selection (current_panel)->fname; /* single file */
264 if (mc_stat (fname, &sf_stat) != 0)
265 { /* get status of file */
266 destroy_dlg (ch_dlg);
267 break;
270 /* select in listboxes */
271 listbox_select_entry (l_user, listbox_search_text (l_user, get_owner (sf_stat.st_uid)));
272 listbox_select_entry (l_group, listbox_search_text (l_group, get_group (sf_stat.st_gid)));
274 chown_label (0, str_trunc (fname, 15));
275 chown_label (1, str_trunc (get_owner (sf_stat.st_uid), 15));
276 chown_label (2, str_trunc (get_group (sf_stat.st_gid), 15));
277 size_trunc_len (buffer, 15, sf_stat.st_size, 0, panels_options.kilobyte_si);
278 chown_label (3, buffer);
279 chown_label (4, string_perm (sf_stat.st_mode));
281 switch (run_dlg (ch_dlg))
283 case B_CANCEL:
284 end_chown = 1;
285 break;
287 case B_SETUSR:
289 struct passwd *user;
290 char *text;
292 listbox_get_current (l_user, &text, NULL);
293 user = getpwnam (text);
294 if (user)
296 new_user = user->pw_uid;
297 apply_chowns (new_user, new_group);
299 break;
302 case B_SETGRP:
304 struct group *grp;
305 char *text;
307 listbox_get_current (l_group, &text, NULL);
308 grp = getgrnam (text);
309 if (grp)
311 new_group = grp->gr_gid;
312 apply_chowns (new_user, new_group);
314 break;
317 case B_SETALL:
318 case B_ENTER:
320 struct group *grp;
321 struct passwd *user;
322 char *text;
324 listbox_get_current (l_group, &text, NULL);
325 grp = getgrnam (text);
326 if (grp)
327 new_group = grp->gr_gid;
328 listbox_get_current (l_user, &text, NULL);
329 user = getpwnam (text);
330 if (user)
331 new_user = user->pw_uid;
332 if (ch_dlg->ret_value == B_ENTER)
334 need_update = 1;
335 if (mc_chown (fname, new_user, new_group) == -1)
336 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
337 fname, unix_error_string (errno));
339 else
340 apply_chowns (new_user, new_group);
341 break;
343 } /* switch */
345 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
347 do_file_mark (current_panel, current_file, 0);
348 need_update = 1;
351 destroy_dlg (ch_dlg);
353 while (current_panel->marked && !end_chown);
355 chown_done ();