Removed type SHELL_ESCAPED_STR in favour of plain char*
[midnight-commander.git] / src / chown.c
blob6598b43dde3656b81aa613a5483ca023382153f2
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 #include <config.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
30 #include "global.h"
31 #include "tty.h"
32 #include "win.h"
33 #include "color.h"
34 #include "dialog.h"
35 #include "widget.h"
37 /* Needed for the extern declarations of integer parameters */
38 #include "dir.h"
39 #include "panel.h" /* Needed for the externs */
40 #include "chmod.h"
41 #include "main.h"
42 #include "chown.h"
43 #include "wtools.h" /* For init_box_colors */
45 #define UX 5
46 #define UY 2
48 #define GX 27
49 #define GY 2
51 #define BX 5
52 #define BY 15
54 #define TX 50
55 #define TY 2
57 #define BUTTONS 5
59 #define B_SETALL B_USER
60 #define B_SETUSR (B_USER + 1)
61 #define B_SETGRP (B_USER + 2)
63 static int need_update, end_chown;
64 static int current_file;
65 static int single_set;
66 static WListbox *l_user, *l_group;
68 static struct {
69 int ret_cmd, flags, y, x;
70 const char *text;
71 } chown_but[BUTTONS] = {
72 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
73 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
74 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
75 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
76 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
79 #define LABELS 5
80 static struct {
81 int y, x;
82 WLabel *l;
83 } chown_label [LABELS] = {
84 { TY+2, TX+2, NULL },
85 { TY+4, TX+2, NULL },
86 { TY+6, TX+2, NULL },
87 { TY+8, TX+2, NULL },
88 { TY+10,TX+2, NULL }
91 static void
92 chown_refresh (Dlg_head *h)
94 common_dialog_repaint (h);
96 attrset (COLOR_NORMAL);
98 draw_box (h, UY, UX, 12, 21);
99 draw_box (h, GY, GX, 12, 21);
100 draw_box (h, TY, TX, 12, 19);
102 dlg_move (h, TY + 1, TX + 1);
103 addstr (_(" Name "));
104 dlg_move (h, TY + 3, TX + 1);
105 addstr (_(" Owner name "));
106 dlg_move (h, TY + 5, TX + 1);
107 addstr (_(" Group name "));
108 dlg_move (h, TY + 7, TX + 1);
109 addstr (_(" Size "));
110 dlg_move (h, TY + 9, TX + 1);
111 addstr (_(" Permission "));
113 attrset (COLOR_HOT_NORMAL);
114 dlg_move (h, UY, UX + 1);
115 addstr (_(" User name "));
116 dlg_move (h, GY, GX + 1);
117 addstr (_(" Group name "));
118 dlg_move (h, TY, TX + 1);
119 addstr (_(" File "));
122 static char *
123 next_file (void)
125 while (!current_panel->dir.list[current_file].f.marked)
126 current_file++;
128 return current_panel->dir.list[current_file].fname;
131 static cb_ret_t
132 chown_callback (Dlg_head *h, dlg_msg_t msg, int parm)
134 switch (msg) {
135 case DLG_DRAW:
136 chown_refresh (h);
137 return MSG_HANDLED;
139 default:
140 return default_dlg_callback (h, msg, parm);
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 (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,
164 _(chown_but[i].text), 0));
166 /* Add the widgets for the file information */
167 for (i = 0; i < LABELS; i++) {
168 chown_label[i].l =
169 label_new (chown_label[i].y, chown_label[i].x, "");
170 add_widget (ch_dlg, chown_label[i].l);
173 /* get new listboxes */
174 l_user = listbox_new (UY + 1, UX + 1, 19, 10, NULL);
175 l_group = listbox_new (GY + 1, GX + 1, 19, 10, NULL);
177 /* add fields for unknown names (numbers) */
178 listbox_add_item (l_user, 0, 0, _("<Unknown user>"), NULL);
179 listbox_add_item (l_group, 0, 0, _("<Unknown group>"), NULL);
181 /* get and put user names in the listbox */
182 setpwent ();
183 while ((l_pass = getpwent ())) {
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 ())) {
191 listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL);
193 endgrent ();
195 /* add listboxes to the dialogs */
196 add_widget (ch_dlg, l_group);
197 add_widget (ch_dlg, l_user);
199 return ch_dlg;
202 static void
203 chown_done (void)
205 if (need_update)
206 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
207 repaint_screen ();
210 static inline void
211 do_chown (uid_t u, gid_t g)
213 if (mc_chown (current_panel->dir.list [current_file].fname, u, g) == -1)
214 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
215 current_panel->dir.list [current_file].fname, unix_error_string (errno));
217 do_file_mark (current_panel, current_file, 0);
220 static void
221 apply_chowns (uid_t u, gid_t g)
223 char *fname;
225 need_update = end_chown = 1;
226 do_chown (u,g);
228 do {
229 fname = next_file ();
231 do_chown (u,g);
232 } while (current_panel->marked);
235 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
237 void
238 chown_cmd (void)
240 char *fname;
241 struct stat sf_stat;
242 WLEntry *fe;
243 Dlg_head *ch_dlg;
244 uid_t new_user;
245 gid_t new_group;
246 char buffer [BUF_TINY];
248 do { /* do while any files remaining */
249 ch_dlg = init_chown ();
250 new_user = new_group = -1;
252 if (current_panel->marked)
253 fname = next_file (); /* next marked file */
254 else
255 fname = selection (current_panel)->fname; /* single file */
257 if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
258 destroy_dlg (ch_dlg);
259 break;
262 /* select in listboxes */
263 fe = listbox_search_text (l_user, get_owner(sf_stat.st_uid));
264 if (fe)
265 listbox_select_entry (l_user, fe);
267 fe = listbox_search_text (l_group, get_group(sf_stat.st_gid));
268 if (fe)
269 listbox_select_entry (l_group, fe);
271 chown_label (0, name_trunc (fname, 15));
272 chown_label (1, name_trunc (get_owner (sf_stat.st_uid), 15));
273 chown_label (2, name_trunc (get_group (sf_stat.st_gid), 15));
274 size_trunc_len (buffer, 15, sf_stat.st_size, 0);
275 chown_label (3, buffer);
276 chown_label (4, string_perm (sf_stat.st_mode));
278 run_dlg (ch_dlg);
280 switch (ch_dlg->ret_value) {
281 case B_CANCEL:
282 end_chown = 1;
283 break;
285 case B_SETUSR:
287 struct passwd *user;
289 user = getpwnam (l_user->current->text);
290 if (user){
291 new_user = user->pw_uid;
292 apply_chowns (new_user, new_group);
294 break;
296 case B_SETGRP:
298 struct group *grp;
300 grp = getgrnam (l_group->current->text);
301 if (grp){
302 new_group = grp->gr_gid;
303 apply_chowns (new_user, new_group);
305 break;
307 case B_SETALL:
308 case B_ENTER:
310 struct group *grp;
311 struct passwd *user;
313 grp = getgrnam (l_group->current->text);
314 if (grp)
315 new_group = grp->gr_gid;
316 user = getpwnam (l_user->current->text);
317 if (user)
318 new_user = user->pw_uid;
319 if (ch_dlg->ret_value==B_ENTER) {
320 need_update = 1;
321 if (mc_chown (fname, new_user, new_group) == -1)
322 message (D_ERROR, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
323 fname, unix_error_string (errno));
324 } else
325 apply_chowns (new_user, new_group);
326 break;
330 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL){
331 do_file_mark (current_panel, current_file, 0);
332 need_update = 1;
334 destroy_dlg (ch_dlg);
335 } while (current_panel->marked && !end_chown);
337 chown_done ();