Editor: sync with new global config location (user menu and syntax files).
[midnight-commander.git] / src / chown.c
blobc565ad39cd82aedcf33678485ecc1ea7d2fbbcae
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>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
34 #include "global.h"
35 #include "tty.h"
36 #include "win.h"
37 #include "color.h"
38 #include "dialog.h"
39 #include "widget.h"
40 #include "strutil.h"
42 /* Needed for the extern declarations of integer parameters */
43 #include "dir.h"
44 #include "panel.h" /* Needed for the externs */
45 #include "chmod.h"
46 #include "main.h"
47 #include "chown.h"
48 #include "wtools.h" /* For init_box_colors */
50 #define UX 5
51 #define UY 2
53 #define GX 27
54 #define GY 2
56 #define BX 5
57 #define BY 15
59 #define TX 50
60 #define TY 2
62 #define BUTTONS 5
64 #define B_SETALL B_USER
65 #define B_SETUSR (B_USER + 1)
66 #define B_SETGRP (B_USER + 2)
68 static int need_update, end_chown;
69 static int current_file;
70 static int single_set;
71 static WListbox *l_user, *l_group;
73 static struct {
74 int ret_cmd, flags, y, x;
75 const char *text;
76 } chown_but[BUTTONS] = {
77 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
78 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
79 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
80 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
81 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
84 #define LABELS 5
85 static struct {
86 int y, x;
87 WLabel *l;
88 } chown_label [LABELS] = {
89 { TY+2, TX+2, NULL },
90 { TY+4, TX+2, NULL },
91 { TY+6, TX+2, NULL },
92 { TY+8, TX+2, NULL },
93 { TY+10,TX+2, NULL }
96 static void
97 chown_refresh (Dlg_head *h)
99 common_dialog_repaint (h);
101 attrset (COLOR_NORMAL);
103 draw_box (h, UY, UX, 12, 21);
104 draw_box (h, GY, GX, 12, 21);
105 draw_box (h, TY, TX, 12, 19);
107 dlg_move (h, TY + 1, TX + 1);
108 addstr (_(" Name "));
109 dlg_move (h, TY + 3, TX + 1);
110 addstr (_(" Owner name "));
111 dlg_move (h, TY + 5, TX + 1);
112 addstr (_(" Group name "));
113 dlg_move (h, TY + 7, TX + 1);
114 addstr (_(" Size "));
115 dlg_move (h, TY + 9, TX + 1);
116 addstr (_(" Permission "));
118 attrset (COLOR_HOT_NORMAL);
119 dlg_move (h, UY, UX + 1);
120 addstr (_(" User name "));
121 dlg_move (h, GY, GX + 1);
122 addstr (_(" Group name "));
123 dlg_move (h, TY, TX + 1);
124 addstr (_(" File "));
127 static char *
128 next_file (void)
130 while (!current_panel->dir.list[current_file].f.marked)
131 current_file++;
133 return current_panel->dir.list[current_file].fname;
136 static cb_ret_t
137 chown_callback (Dlg_head *h, dlg_msg_t msg, int parm)
139 switch (msg) {
140 case DLG_DRAW:
141 chown_refresh (h);
142 return MSG_HANDLED;
144 default:
145 return default_dlg_callback (h, msg, parm);
149 static Dlg_head *
150 init_chown (void)
152 int i;
153 struct passwd *l_pass;
154 struct group *l_grp;
155 Dlg_head *ch_dlg;
157 do_refresh ();
158 end_chown = need_update = current_file = 0;
159 single_set = (current_panel->marked < 2) ? 3 : 0;
161 ch_dlg =
162 create_dlg (0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
163 _(" Chown command "), DLG_CENTER | DLG_REVERSE);
165 for (i = 0; i < BUTTONS - single_set; i++)
166 add_widget (ch_dlg,
167 button_new (BY + chown_but[i].y, BX + chown_but[i].x,
168 chown_but[i].ret_cmd, chown_but[i].flags,
169 _(chown_but[i].text), 0));
171 /* Add the widgets for the file information */
172 for (i = 0; i < LABELS; i++) {
173 chown_label[i].l =
174 label_new (chown_label[i].y, chown_label[i].x, "");
175 add_widget (ch_dlg, chown_label[i].l);
178 /* get new listboxes */
179 l_user = listbox_new (UY + 1, UX + 1, 10, 19, NULL);
180 l_group = listbox_new (GY + 1, GX + 1, 10, 19, NULL);
182 /* add fields for unknown names (numbers) */
183 listbox_add_item (l_user, 0, 0, _("<Unknown user>"), NULL);
184 listbox_add_item (l_group, 0, 0, _("<Unknown group>"), NULL);
186 /* get and put user names in the listbox */
187 setpwent ();
188 while ((l_pass = getpwent ())) {
189 listbox_add_item (l_user, LISTBOX_APPEND_SORTED, 0, l_pass->pw_name, NULL);
191 endpwent ();
193 /* get and put group names in the listbox */
194 setgrent ();
195 while ((l_grp = getgrent ())) {
196 listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL);
198 endgrent ();
200 /* add listboxes to the dialogs */
201 add_widget (ch_dlg, l_group);
202 add_widget (ch_dlg, l_user);
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 inline 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);
233 do {
234 fname = next_file ();
236 do_chown (u,g);
237 } while (current_panel->marked);
240 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
242 void
243 chown_cmd (void)
245 char *fname;
246 struct stat sf_stat;
247 WLEntry *fe;
248 Dlg_head *ch_dlg;
249 uid_t new_user;
250 gid_t new_group;
251 char buffer [BUF_TINY];
253 do { /* do while any files remaining */
254 ch_dlg = init_chown ();
255 new_user = new_group = -1;
257 if (current_panel->marked)
258 fname = next_file (); /* next marked file */
259 else
260 fname = selection (current_panel)->fname; /* single file */
262 if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
263 destroy_dlg (ch_dlg);
264 break;
267 /* select in listboxes */
268 fe = listbox_search_text (l_user, get_owner(sf_stat.st_uid));
269 if (fe)
270 listbox_select_entry (l_user, fe);
272 fe = listbox_search_text (l_group, get_group(sf_stat.st_gid));
273 if (fe)
274 listbox_select_entry (l_group, fe);
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 run_dlg (ch_dlg);
285 switch (ch_dlg->ret_value) {
286 case B_CANCEL:
287 end_chown = 1;
288 break;
290 case B_SETUSR:
292 struct passwd *user;
294 user = getpwnam (l_user->current->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;
305 grp = getgrnam (l_group->current->text);
306 if (grp){
307 new_group = grp->gr_gid;
308 apply_chowns (new_user, new_group);
310 break;
312 case B_SETALL:
313 case B_ENTER:
315 struct group *grp;
316 struct passwd *user;
318 grp = getgrnam (l_group->current->text);
319 if (grp)
320 new_group = grp->gr_gid;
321 user = getpwnam (l_user->current->text);
322 if (user)
323 new_user = user->pw_uid;
324 if (ch_dlg->ret_value==B_ENTER) {
325 need_update = 1;
326 if (mc_chown (fname, new_user, new_group) == -1)
327 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
328 fname, unix_error_string (errno));
329 } else
330 apply_chowns (new_user, new_group);
331 break;
335 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL){
336 do_file_mark (current_panel, current_file, 0);
337 need_update = 1;
339 destroy_dlg (ch_dlg);
340 } while (current_panel->marked && !end_chown);
342 chown_done ();