Merge branch '2499_mcedit_select_cur_word'
[midnight-commander/borarpet.git] / src / filemanager / chown.c
blob5ea070d8d4714c999e5b95b06f44d7acbe71eb63
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"
41 #include "lib/util.h"
42 #include "lib/widget.h"
44 #include "src/setup.h" /* panels_options */
46 /* Needed for the extern declarations of integer parameters */
47 #include "chmod.h"
48 #include "midnight.h" /* current_panel */
49 #include "layout.h" /* repaint_screen() */
51 #include "chown.h"
53 /*** global variables ****************************************************************************/
55 /*** file scope macro definitions ****************************************************************/
57 #define UX 5
58 #define UY 2
60 #define GX 27
61 #define GY 2
63 #define BX 5
64 #define BY 15
66 #define TX 50
67 #define TY 2
69 #define BUTTONS 5
71 #define B_SETALL B_USER
72 #define B_SETUSR (B_USER + 1)
73 #define B_SETGRP (B_USER + 2)
75 #define LABELS 5
77 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
79 /*** file scope type declarations ****************************************************************/
81 /*** file scope variables ************************************************************************/
83 static int need_update, end_chown;
84 static int current_file;
85 static int single_set;
86 static WListbox *l_user, *l_group;
88 /* *INDENT-OFF* */
89 static struct
91 int ret_cmd, flags, y, x;
92 const char *text;
93 } chown_but[BUTTONS] = {
94 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
95 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
96 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
97 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
98 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
101 static struct {
102 int y, x;
103 WLabel *l;
104 } chown_label [LABELS] = {
105 { TY + 2, TX + 2, NULL },
106 { TY + 4, TX + 2, NULL },
107 { TY + 6, TX + 2, NULL },
108 { TY + 8, TX + 2, NULL },
109 { TY + 10, TX + 2, NULL }
111 /* *INDENT-ON* */
113 /*** file scope functions ************************************************************************/
114 /* --------------------------------------------------------------------------------------------- */
116 static void
117 chown_refresh (Dlg_head * h)
119 common_dialog_repaint (h);
121 tty_setcolor (COLOR_NORMAL);
123 dlg_move (h, TY + 1, TX + 2);
124 tty_print_string (_("Name"));
125 dlg_move (h, TY + 3, TX + 2);
126 tty_print_string (_("Owner name"));
127 dlg_move (h, TY + 5, TX + 2);
128 tty_print_string (_("Group name"));
129 dlg_move (h, TY + 7, TX + 2);
130 tty_print_string (_("Size"));
131 dlg_move (h, TY + 9, TX + 2);
132 tty_print_string (_("Permission"));
135 /* --------------------------------------------------------------------------------------------- */
137 static char *
138 next_file (void)
140 while (!current_panel->dir.list[current_file].f.marked)
141 current_file++;
143 return current_panel->dir.list[current_file].fname;
146 /* --------------------------------------------------------------------------------------------- */
148 static cb_ret_t
149 chown_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
151 switch (msg)
153 case DLG_DRAW:
154 chown_refresh (h);
155 return MSG_HANDLED;
157 default:
158 return default_dlg_callback (h, sender, msg, parm, data);
162 /* --------------------------------------------------------------------------------------------- */
164 static Dlg_head *
165 init_chown (void)
167 int i;
168 struct passwd *l_pass;
169 struct group *l_grp;
170 Dlg_head *ch_dlg;
172 do_refresh ();
173 end_chown = need_update = current_file = 0;
174 single_set = (current_panel->marked < 2) ? 3 : 0;
176 ch_dlg =
177 create_dlg (TRUE, 0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
178 _("Chown command"), DLG_CENTER | DLG_REVERSE);
180 for (i = 0; i < BUTTONS - single_set; i++)
181 add_widget (ch_dlg,
182 button_new (BY + chown_but[i].y, BX + chown_but[i].x,
183 chown_but[i].ret_cmd, chown_but[i].flags, _(chown_but[i].text), 0));
185 /* Add the widgets for the file information */
186 for (i = 0; i < LABELS; i++)
188 chown_label[i].l = label_new (chown_label[i].y, chown_label[i].x, "");
189 add_widget (ch_dlg, chown_label[i].l);
192 /* get new listboxes */
193 l_user = listbox_new (UY + 1, UX + 1, 10, 19, FALSE, NULL);
194 l_group = listbox_new (GY + 1, GX + 1, 10, 19, FALSE, NULL);
196 /* add fields for unknown names (numbers) */
197 listbox_add_item (l_user, LISTBOX_APPEND_AT_END, 0, _("<Unknown user>"), NULL);
198 listbox_add_item (l_group, LISTBOX_APPEND_AT_END, 0, _("<Unknown group>"), NULL);
200 /* get and put user names in the listbox */
201 setpwent ();
202 while ((l_pass = getpwent ()) != NULL)
204 listbox_add_item (l_user, LISTBOX_APPEND_SORTED, 0, l_pass->pw_name, NULL);
206 endpwent ();
208 /* get and put group names in the listbox */
209 setgrent ();
210 while ((l_grp = getgrent ()) != NULL)
212 listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL);
214 endgrent ();
216 add_widget (ch_dlg, groupbox_new (TY, TX, 12, 19, _("File")));
218 /* add listboxes to the dialogs */
219 add_widget (ch_dlg, l_group);
220 add_widget (ch_dlg, groupbox_new (GY, GX, 12, 21, _("Group name")));
221 add_widget (ch_dlg, l_user);
222 add_widget (ch_dlg, groupbox_new (UY, UX, 12, 21, _("User name")));
224 return ch_dlg;
227 /* --------------------------------------------------------------------------------------------- */
229 static void
230 chown_done (void)
232 if (need_update)
233 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
234 repaint_screen ();
237 /* --------------------------------------------------------------------------------------------- */
239 static void
240 do_chown (uid_t u, gid_t g)
242 if (mc_chown (current_panel->dir.list[current_file].fname, u, g) == -1)
243 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
244 current_panel->dir.list[current_file].fname, unix_error_string (errno));
246 do_file_mark (current_panel, current_file, 0);
249 /* --------------------------------------------------------------------------------------------- */
251 static void
252 apply_chowns (uid_t u, gid_t g)
254 char *fname;
256 need_update = end_chown = 1;
257 do_chown (u, g);
261 fname = next_file ();
263 do_chown (u, g);
265 while (current_panel->marked);
268 /* --------------------------------------------------------------------------------------------- */
269 /*** public functions ****************************************************************************/
270 /* --------------------------------------------------------------------------------------------- */
272 void
273 chown_cmd (void)
275 char *fname;
276 struct stat sf_stat;
277 Dlg_head *ch_dlg;
278 uid_t new_user;
279 gid_t new_group;
280 char buffer[BUF_TINY];
283 { /* do while any files remaining */
284 ch_dlg = init_chown ();
285 new_user = new_group = -1;
287 if (current_panel->marked)
288 fname = next_file (); /* next marked file */
289 else
290 fname = selection (current_panel)->fname; /* single file */
292 if (mc_stat (fname, &sf_stat) != 0)
293 { /* get status of file */
294 destroy_dlg (ch_dlg);
295 break;
298 /* select in listboxes */
299 listbox_select_entry (l_user, listbox_search_text (l_user, get_owner (sf_stat.st_uid)));
300 listbox_select_entry (l_group, listbox_search_text (l_group, get_group (sf_stat.st_gid)));
302 chown_label (0, str_trunc (fname, 15));
303 chown_label (1, str_trunc (get_owner (sf_stat.st_uid), 15));
304 chown_label (2, str_trunc (get_group (sf_stat.st_gid), 15));
305 size_trunc_len (buffer, 15, sf_stat.st_size, 0, panels_options.kilobyte_si);
306 chown_label (3, buffer);
307 chown_label (4, string_perm (sf_stat.st_mode));
309 switch (run_dlg (ch_dlg))
311 case B_CANCEL:
312 end_chown = 1;
313 break;
315 case B_SETUSR:
317 struct passwd *user;
318 char *text;
320 listbox_get_current (l_user, &text, NULL);
321 user = getpwnam (text);
322 if (user)
324 new_user = user->pw_uid;
325 apply_chowns (new_user, new_group);
327 break;
330 case B_SETGRP:
332 struct group *grp;
333 char *text;
335 listbox_get_current (l_group, &text, NULL);
336 grp = getgrnam (text);
337 if (grp)
339 new_group = grp->gr_gid;
340 apply_chowns (new_user, new_group);
342 break;
345 case B_SETALL:
346 case B_ENTER:
348 struct group *grp;
349 struct passwd *user;
350 char *text;
352 listbox_get_current (l_group, &text, NULL);
353 grp = getgrnam (text);
354 if (grp)
355 new_group = grp->gr_gid;
356 listbox_get_current (l_user, &text, NULL);
357 user = getpwnam (text);
358 if (user)
359 new_user = user->pw_uid;
360 if (ch_dlg->ret_value == B_ENTER)
362 need_update = 1;
363 if (mc_chown (fname, new_user, new_group) == -1)
364 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
365 fname, unix_error_string (errno));
367 else
368 apply_chowns (new_user, new_group);
369 break;
371 } /* switch */
373 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
375 do_file_mark (current_panel, current_file, 0);
376 need_update = 1;
379 destroy_dlg (ch_dlg);
381 while (current_panel->marked && !end_chown);
383 chown_done ();
386 /* --------------------------------------------------------------------------------------------- */