Ticket #2784: segfault at chown.
[midnight-commander.git] / src / filemanager / chown.c
blob5953023b0b08eefc87b274b56ff668c4e042be04
1 /*
2 Chown command -- for the Midnight Commander
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file chown.c
25 * \brief Source: chown command
28 #include <config.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <grp.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/skin.h"
43 #include "lib/vfs/vfs.h"
44 #include "lib/strutil.h"
45 #include "lib/util.h"
46 #include "lib/widget.h"
48 #include "src/setup.h" /* panels_options */
50 /* Needed for the extern declarations of integer parameters */
51 #include "chmod.h"
52 #include "midnight.h" /* current_panel */
54 #include "chown.h"
56 /*** global variables ****************************************************************************/
58 /*** file scope macro definitions ****************************************************************/
60 #define UX 5
61 #define UY 2
63 #define GX 27
64 #define GY 2
66 #define BX 5
67 #define BY 15
69 #define TX 50
70 #define TY 2
72 #define BUTTONS 5
74 #define B_SETALL B_USER
75 #define B_SETUSR (B_USER + 1)
76 #define B_SETGRP (B_USER + 2)
78 #define LABELS 5
80 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
82 /*** file scope type declarations ****************************************************************/
84 /*** file scope variables ************************************************************************/
86 static int need_update, end_chown;
87 static int current_file;
88 static int single_set;
89 static WListbox *l_user, *l_group;
91 /* *INDENT-OFF* */
92 static struct
94 int ret_cmd, flags, y, x;
95 const char *text;
96 } chown_but[BUTTONS] = {
97 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
98 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
99 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
100 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
101 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
104 static struct {
105 int y, x;
106 WLabel *l;
107 } chown_label [LABELS] = {
108 { TY + 2, TX + 2, NULL },
109 { TY + 4, TX + 2, NULL },
110 { TY + 6, TX + 2, NULL },
111 { TY + 8, TX + 2, NULL },
112 { TY + 10, TX + 2, NULL }
114 /* *INDENT-ON* */
116 /*** file scope functions ************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
119 static void
120 chown_refresh (Dlg_head * h)
122 common_dialog_repaint (h);
124 tty_setcolor (COLOR_NORMAL);
126 dlg_move (h, TY + 1, TX + 2);
127 tty_print_string (_("Name"));
128 dlg_move (h, TY + 3, TX + 2);
129 tty_print_string (_("Owner name"));
130 dlg_move (h, TY + 5, TX + 2);
131 tty_print_string (_("Group name"));
132 dlg_move (h, TY + 7, TX + 2);
133 tty_print_string (_("Size"));
134 dlg_move (h, TY + 9, TX + 2);
135 tty_print_string (_("Permission"));
138 /* --------------------------------------------------------------------------------------------- */
140 static char *
141 next_file (void)
143 while (!current_panel->dir.list[current_file].f.marked)
144 current_file++;
146 return current_panel->dir.list[current_file].fname;
149 /* --------------------------------------------------------------------------------------------- */
151 static cb_ret_t
152 chown_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
154 switch (msg)
156 case DLG_DRAW:
157 chown_refresh (h);
158 return MSG_HANDLED;
160 default:
161 return default_dlg_callback (h, sender, msg, parm, data);
165 /* --------------------------------------------------------------------------------------------- */
167 static Dlg_head *
168 init_chown (void)
170 int i;
171 struct passwd *l_pass;
172 struct group *l_grp;
173 Dlg_head *ch_dlg;
175 do_refresh ();
176 end_chown = need_update = current_file = 0;
177 single_set = (current_panel->marked < 2) ? 3 : 0;
179 ch_dlg =
180 create_dlg (TRUE, 0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
181 _("Chown command"), DLG_CENTER | DLG_REVERSE);
183 for (i = 0; i < BUTTONS - single_set; i++)
184 add_widget (ch_dlg,
185 button_new (BY + chown_but[i].y, BX + chown_but[i].x,
186 chown_but[i].ret_cmd, chown_but[i].flags, _(chown_but[i].text), 0));
188 /* Add the widgets for the file information */
189 for (i = 0; i < LABELS; i++)
191 chown_label[i].l = label_new (chown_label[i].y, chown_label[i].x, "");
192 add_widget (ch_dlg, chown_label[i].l);
195 /* get new listboxes */
196 l_user = listbox_new (UY + 1, UX + 1, 10, 19, FALSE, NULL);
197 l_group = listbox_new (GY + 1, GX + 1, 10, 19, FALSE, NULL);
199 /* add fields for unknown names (numbers) */
200 listbox_add_item (l_user, LISTBOX_APPEND_AT_END, 0, _("<Unknown user>"), NULL);
201 listbox_add_item (l_group, LISTBOX_APPEND_AT_END, 0, _("<Unknown group>"), NULL);
203 /* get and put user names in the listbox */
204 setpwent ();
205 while ((l_pass = getpwent ()) != NULL)
207 listbox_add_item (l_user, LISTBOX_APPEND_SORTED, 0, l_pass->pw_name, NULL);
209 endpwent ();
211 /* get and put group names in the listbox */
212 setgrent ();
213 while ((l_grp = getgrent ()) != NULL)
215 listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL);
217 endgrent ();
219 add_widget (ch_dlg, groupbox_new (TY, TX, 12, 19, _("File")));
221 /* add listboxes to the dialogs */
222 add_widget (ch_dlg, l_group);
223 add_widget (ch_dlg, groupbox_new (GY, GX, 12, 21, _("Group name")));
224 add_widget (ch_dlg, l_user);
225 add_widget (ch_dlg, groupbox_new (UY, UX, 12, 21, _("User name")));
227 return ch_dlg;
230 /* --------------------------------------------------------------------------------------------- */
232 static void
233 chown_done (void)
235 if (need_update)
236 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
237 repaint_screen ();
240 /* --------------------------------------------------------------------------------------------- */
242 static void
243 do_chown (uid_t u, gid_t g)
245 vfs_path_t *vpath;
247 vpath = vfs_path_from_str (current_panel->dir.list[current_file].fname);
248 if (mc_chown (vpath, u, g) == -1)
249 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
250 current_panel->dir.list[current_file].fname, unix_error_string (errno));
252 vfs_path_free (vpath);
253 do_file_mark (current_panel, current_file, 0);
256 /* --------------------------------------------------------------------------------------------- */
258 static void
259 apply_chowns (uid_t u, gid_t g)
261 char *fname;
263 need_update = end_chown = 1;
264 do_chown (u, g);
268 fname = next_file ();
270 do_chown (u, g);
272 while (current_panel->marked);
275 /* --------------------------------------------------------------------------------------------- */
276 /*** public functions ****************************************************************************/
277 /* --------------------------------------------------------------------------------------------- */
279 void
280 chown_cmd (void)
282 char *fname;
283 struct stat sf_stat;
284 Dlg_head *ch_dlg;
285 uid_t new_user;
286 gid_t new_group;
287 char buffer[BUF_TINY];
290 { /* do while any files remaining */
291 vfs_path_t *vpath;
293 ch_dlg = init_chown ();
294 new_user = new_group = -1;
296 if (current_panel->marked)
297 fname = next_file (); /* next marked file */
298 else
299 fname = selection (current_panel)->fname; /* single file */
301 vpath = vfs_path_from_str (fname);
302 if (mc_stat (vpath, &sf_stat) != 0)
303 { /* get status of file */
304 destroy_dlg (ch_dlg);
305 vfs_path_free (vpath);
306 break;
308 vfs_path_free (vpath);
310 /* select in listboxes */
311 listbox_select_entry (l_user, listbox_search_text (l_user, get_owner (sf_stat.st_uid)));
312 listbox_select_entry (l_group, listbox_search_text (l_group, get_group (sf_stat.st_gid)));
314 chown_label (0, str_trunc (fname, 15));
315 chown_label (1, str_trunc (get_owner (sf_stat.st_uid), 15));
316 chown_label (2, str_trunc (get_group (sf_stat.st_gid), 15));
317 size_trunc_len (buffer, 15, sf_stat.st_size, 0, panels_options.kilobyte_si);
318 chown_label (3, buffer);
319 chown_label (4, string_perm (sf_stat.st_mode));
321 switch (run_dlg (ch_dlg))
323 case B_CANCEL:
324 end_chown = 1;
325 break;
327 case B_SETUSR:
329 struct passwd *user;
330 char *text;
332 listbox_get_current (l_user, &text, NULL);
333 user = getpwnam (text);
334 if (user)
336 new_user = user->pw_uid;
337 apply_chowns (new_user, new_group);
339 break;
342 case B_SETGRP:
344 struct group *grp;
345 char *text;
347 listbox_get_current (l_group, &text, NULL);
348 grp = getgrnam (text);
349 if (grp)
351 new_group = grp->gr_gid;
352 apply_chowns (new_user, new_group);
354 break;
357 case B_SETALL:
358 case B_ENTER:
360 struct group *grp;
361 struct passwd *user;
362 char *text;
364 listbox_get_current (l_group, &text, NULL);
365 grp = getgrnam (text);
366 if (grp)
367 new_group = grp->gr_gid;
368 listbox_get_current (l_user, &text, NULL);
369 user = getpwnam (text);
370 if (user)
371 new_user = user->pw_uid;
372 if (ch_dlg->ret_value == B_ENTER)
374 vfs_path_t *fname_vpath;
376 fname_vpath = vfs_path_from_str (fname);
377 need_update = 1;
378 if (mc_chown (fname_vpath, new_user, new_group) == -1)
379 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
380 fname, unix_error_string (errno));
381 vfs_path_free (fname_vpath);
383 else
384 apply_chowns (new_user, new_group);
385 break;
387 } /* switch */
389 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
391 do_file_mark (current_panel, current_file, 0);
392 need_update = 1;
395 destroy_dlg (ch_dlg);
397 while (current_panel->marked && !end_chown);
399 chown_done ();
402 /* --------------------------------------------------------------------------------------------- */