Merge branch '44_more_functionally_u7z'
[pantumic.git] / src / chown.c
blob713f7267d84e9d19d62c36f10f622fe3efa9b698
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 "global.h"
37 #include "../src/tty/tty.h"
38 #include "../src/skin/skin.h"
40 #include "dialog.h"
41 #include "widget.h"
42 #include "strutil.h"
44 /* Needed for the extern declarations of integer parameters */
45 #include "dir.h"
46 #include "panel.h" /* Needed for the externs */
47 #include "chmod.h"
48 #include "main.h" /* update_panels() */
49 #include "layout.h" /* repaint_screen() */
50 #include "chown.h"
51 #include "wtools.h" /* For init_box_colors */
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 static struct {
77 int ret_cmd, flags, y, x;
78 const char *text;
79 } chown_but[BUTTONS] = {
80 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
81 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
82 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
83 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
84 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
87 #define LABELS 5
88 static struct {
89 int y, x;
90 WLabel *l;
91 } chown_label [LABELS] = {
92 { TY+2, TX+2, NULL },
93 { TY+4, TX+2, NULL },
94 { TY+6, TX+2, NULL },
95 { TY+8, TX+2, NULL },
96 { TY+10,TX+2, NULL }
99 static void
100 chown_refresh (Dlg_head *h)
102 common_dialog_repaint (h);
104 tty_setcolor (COLOR_NORMAL);
106 draw_box (h, UY, UX, 12, 21);
107 draw_box (h, GY, GX, 12, 21);
108 draw_box (h, TY, TX, 12, 19);
110 dlg_move (h, TY + 1, TX + 1);
111 tty_print_string (_(" Name "));
112 dlg_move (h, TY + 3, TX + 1);
113 tty_print_string (_(" Owner name "));
114 dlg_move (h, TY + 5, TX + 1);
115 tty_print_string (_(" Group name "));
116 dlg_move (h, TY + 7, TX + 1);
117 tty_print_string (_(" Size "));
118 dlg_move (h, TY + 9, TX + 1);
119 tty_print_string (_(" Permission "));
121 tty_setcolor (COLOR_HOT_NORMAL);
122 dlg_move (h, UY, UX + 1);
123 tty_print_string (_(" User name "));
124 dlg_move (h, GY, GX + 1);
125 tty_print_string (_(" Group name "));
126 dlg_move (h, TY, TX + 1);
127 tty_print_string (_(" File "));
130 static char *
131 next_file (void)
133 while (!current_panel->dir.list[current_file].f.marked)
134 current_file++;
136 return current_panel->dir.list[current_file].fname;
139 static cb_ret_t
140 chown_callback (Dlg_head *h, dlg_msg_t msg, int parm)
142 switch (msg) {
143 case DLG_DRAW:
144 chown_refresh (h);
145 return MSG_HANDLED;
147 default:
148 return default_dlg_callback (h, msg, parm);
152 static Dlg_head *
153 init_chown (void)
155 int i;
156 struct passwd *l_pass;
157 struct group *l_grp;
158 Dlg_head *ch_dlg;
160 do_refresh ();
161 end_chown = need_update = current_file = 0;
162 single_set = (current_panel->marked < 2) ? 3 : 0;
164 ch_dlg =
165 create_dlg (0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
166 _(" Chown command "), DLG_CENTER | DLG_REVERSE);
168 for (i = 0; i < BUTTONS - single_set; i++)
169 add_widget (ch_dlg,
170 button_new (BY + chown_but[i].y, BX + chown_but[i].x,
171 chown_but[i].ret_cmd, chown_but[i].flags,
172 _(chown_but[i].text), 0));
174 /* Add the widgets for the file information */
175 for (i = 0; i < LABELS; i++) {
176 chown_label[i].l =
177 label_new (chown_label[i].y, chown_label[i].x, "");
178 add_widget (ch_dlg, chown_label[i].l);
181 /* get new listboxes */
182 l_user = listbox_new (UY + 1, UX + 1, 10, 19, NULL);
183 l_group = listbox_new (GY + 1, GX + 1, 10, 19, NULL);
185 /* add fields for unknown names (numbers) */
186 listbox_add_item (l_user, 0, 0, _("<Unknown user>"), NULL);
187 listbox_add_item (l_group, 0, 0, _("<Unknown group>"), NULL);
189 /* get and put user names in the listbox */
190 setpwent ();
191 while ((l_pass = getpwent ())) {
192 listbox_add_item (l_user, LISTBOX_APPEND_SORTED, 0, l_pass->pw_name, NULL);
194 endpwent ();
196 /* get and put group names in the listbox */
197 setgrent ();
198 while ((l_grp = getgrent ())) {
199 listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL);
201 endgrent ();
203 /* add listboxes to the dialogs */
204 add_widget (ch_dlg, l_group);
205 add_widget (ch_dlg, l_user);
207 return ch_dlg;
210 static void
211 chown_done (void)
213 if (need_update)
214 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
215 repaint_screen ();
218 static void
219 do_chown (uid_t u, gid_t g)
221 if (mc_chown (current_panel->dir.list [current_file].fname, u, g) == -1)
222 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
223 current_panel->dir.list [current_file].fname, unix_error_string (errno));
225 do_file_mark (current_panel, current_file, 0);
228 static void
229 apply_chowns (uid_t u, gid_t g)
231 char *fname;
233 need_update = end_chown = 1;
234 do_chown (u,g);
236 do {
237 fname = next_file ();
239 do_chown (u,g);
240 } while (current_panel->marked);
243 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
245 void
246 chown_cmd (void)
248 char *fname;
249 struct stat sf_stat;
250 WLEntry *fe;
251 Dlg_head *ch_dlg;
252 uid_t new_user;
253 gid_t new_group;
254 char buffer [BUF_TINY];
256 do { /* do while any files remaining */
257 ch_dlg = init_chown ();
258 new_user = new_group = -1;
260 if (current_panel->marked)
261 fname = next_file (); /* next marked file */
262 else
263 fname = selection (current_panel)->fname; /* single file */
265 if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
266 destroy_dlg (ch_dlg);
267 break;
270 /* select in listboxes */
271 fe = listbox_search_text (l_user, get_owner(sf_stat.st_uid));
272 if (fe)
273 listbox_select_entry (l_user, fe);
275 fe = listbox_search_text (l_group, get_group(sf_stat.st_gid));
276 if (fe)
277 listbox_select_entry (l_group, fe);
279 chown_label (0, str_trunc (fname, 15));
280 chown_label (1, str_trunc (get_owner (sf_stat.st_uid), 15));
281 chown_label (2, str_trunc (get_group (sf_stat.st_gid), 15));
282 size_trunc_len (buffer, 15, sf_stat.st_size, 0);
283 chown_label (3, buffer);
284 chown_label (4, string_perm (sf_stat.st_mode));
286 run_dlg (ch_dlg);
288 switch (ch_dlg->ret_value) {
289 case B_CANCEL:
290 end_chown = 1;
291 break;
293 case B_SETUSR:
295 struct passwd *user;
297 user = getpwnam (l_user->current->text);
298 if (user){
299 new_user = user->pw_uid;
300 apply_chowns (new_user, new_group);
302 break;
304 case B_SETGRP:
306 struct group *grp;
308 grp = getgrnam (l_group->current->text);
309 if (grp){
310 new_group = grp->gr_gid;
311 apply_chowns (new_user, new_group);
313 break;
315 case B_SETALL:
316 case B_ENTER:
318 struct group *grp;
319 struct passwd *user;
321 grp = getgrnam (l_group->current->text);
322 if (grp)
323 new_group = grp->gr_gid;
324 user = getpwnam (l_user->current->text);
325 if (user)
326 new_user = user->pw_uid;
327 if (ch_dlg->ret_value==B_ENTER) {
328 need_update = 1;
329 if (mc_chown (fname, new_user, new_group) == -1)
330 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
331 fname, unix_error_string (errno));
332 } else
333 apply_chowns (new_user, new_group);
334 break;
338 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL){
339 do_file_mark (current_panel, current_file, 0);
340 need_update = 1;
342 destroy_dlg (ch_dlg);
343 } while (current_panel->marked && !end_chown);
345 chown_done ();