Just a little correction at the it.po file.
[midnight-commander.git] / src / chown.c
blobf1181f05aca993fc90464862b8d308a3e1485aa5
1 /* Chown command -- for the Midnight Commander
2 Copyright (C) 1994 Radek Doulik
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include <config.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <errno.h> /* For errno on SunOS systems */
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
30 #include "global.h"
31 #include "tty.h"
32 #include "win.h"
33 #include "color.h"
34 #include "dlg.h"
35 #include "widget.h"
36 #include "dialog.h" /* For do_refresh() */
38 /* Needed for the extern declarations of integer parameters */
39 #include "dir.h"
40 #include "panel.h" /* Needed for the externs */
41 #include "chmod.h"
42 #include "main.h"
43 #include "chown.h"
44 #include "wtools.h" /* For init_box_colors */
45 #include "../vfs/vfs.h"
47 #define UX 5
48 #define UY 2
50 #define GX 27
51 #define GY 2
53 #define BX 5
54 #define BY 15
56 #define TX 50
57 #define TY 2
59 #define BUTTONS 5
61 #define B_SETALL B_USER
62 #define B_SETUSR B_USER + 1
63 #define B_SETGRP B_USER + 2
65 static int need_update, end_chown;
66 static int current_file;
67 static int single_set;
68 static WListbox *l_user, *l_group;
70 static struct {
71 int ret_cmd, flags, y, x;
72 char *text;
73 } chown_but[BUTTONS] = {
74 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
75 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
76 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
77 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
78 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
81 #define LABELS 5
82 static struct {
83 int y, x;
84 WLabel *l;
85 } chown_label [LABELS] = {
86 { TY+2, TX+2 },
87 { TY+4, TX+2 },
88 { TY+6, TX+2 },
89 { TY+8, TX+2 },
90 { TY+10,TX+2 }
93 static void
94 chown_refresh (Dlg_head *h)
96 common_dialog_repaint (h);
98 attrset (COLOR_NORMAL);
100 draw_box (h, UY, UX, 12, 21);
101 draw_box (h, GY, GX, 12, 21);
102 draw_box (h, TY, TX, 12, 19);
104 dlg_move (h, TY + 1, TX + 1);
105 addstr (_(" Name "));
106 dlg_move (h, TY + 3, TX + 1);
107 addstr (_(" Owner name "));
108 dlg_move (h, TY + 5, TX + 1);
109 addstr (_(" Group name "));
110 dlg_move (h, TY + 7, TX + 1);
111 addstr (_(" Size "));
112 dlg_move (h, TY + 9, TX + 1);
113 addstr (_(" Permission "));
115 attrset (COLOR_HOT_NORMAL);
116 dlg_move (h, UY, UX + 1);
117 addstr (_(" User name "));
118 dlg_move (h, GY, GX + 1);
119 addstr (_(" Group name "));
120 dlg_move (h, TY, TX + 1);
121 addstr (_(" File "));
124 static char *
125 next_file (void)
127 while (!cpanel->dir.list[current_file].f.marked)
128 current_file++;
130 return cpanel->dir.list[current_file].fname;
133 static int
134 chown_callback (Dlg_head * h, int Par, int Msg)
136 switch (Msg) {
137 case DLG_DRAW:
138 chown_refresh (h);
139 break;
141 return 0;
144 static int
145 l_call (void *data)
147 return 1;
150 static Dlg_head *
151 init_chown (void)
153 int i;
154 struct passwd *l_pass;
155 struct group *l_grp;
156 Dlg_head *ch_dlg;
158 do_refresh ();
159 end_chown = need_update = current_file = 0;
160 single_set = (cpanel->marked < 2) ? 3 : 0;
162 ch_dlg =
163 create_dlg (0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
164 _(" Chown command "), DLG_CENTER);
166 for (i = 0; i < BUTTONS - single_set; i++)
167 add_widget (ch_dlg,
168 button_new (BY + chown_but[i].y, BX + chown_but[i].x,
169 chown_but[i].ret_cmd, chown_but[i].flags,
170 _(chown_but[i].text), 0, 0, NULL));
172 /* Add the widgets for the file information */
173 for (i = 0; i < LABELS; i++) {
174 chown_label[i].l =
175 label_new (chown_label[i].y, chown_label[i].x, "", NULL);
176 add_widget (ch_dlg, chown_label[i].l);
179 /* get new listboxes */
180 l_user = listbox_new (UY + 1, UX + 1, 19, 10, 0, l_call, NULL);
181 l_group = listbox_new (GY + 1, GX + 1, 19, 10, 0, l_call, NULL);
183 /* add fields for unknown names (numbers) */
184 listbox_add_item (l_user, 0, 0, _("<Unknown user>"), NULL);
185 listbox_add_item (l_group, 0, 0, _("<Unknown group>"), NULL);
187 /* get and put user names in the listbox */
188 setpwent ();
189 while ((l_pass = getpwent ())) {
190 listbox_add_item (l_user, 0, 0, l_pass->pw_name, NULL);
192 endpwent ();
194 /* get and put group names in the listbox */
195 setgrent ();
196 while ((l_grp = getgrent ())) {
197 listbox_add_item (l_group, 0, 0, l_grp->gr_name, NULL);
199 endgrent ();
201 /* add listboxes to the dialogs */
202 add_widget (ch_dlg, l_group);
203 add_widget (ch_dlg, l_user);
205 return ch_dlg;
208 static void
209 chown_done (void)
211 if (need_update)
212 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
213 repaint_screen ();
216 static inline void
217 do_chown (uid_t u, gid_t g)
219 if (mc_chown (cpanel->dir.list [current_file].fname, u, g) == -1)
220 message (1, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
221 cpanel->dir.list [current_file].fname, unix_error_string (errno));
223 do_file_mark (cpanel, current_file, 0);
226 static void
227 apply_chowns (uid_t u, gid_t g)
229 char *fname;
231 need_update = end_chown = 1;
232 do_chown (u,g);
234 do {
235 fname = next_file ();
237 do_chown (u,g);
238 } while (cpanel->marked);
241 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
243 void
244 chown_cmd (void)
246 char *fname;
247 struct stat sf_stat;
248 WLEntry *fe;
249 Dlg_head *ch_dlg;
250 uid_t new_user;
251 gid_t new_group;
252 char buffer [BUF_TINY];
254 do { /* do while any files remaining */
255 ch_dlg = init_chown ();
256 new_user = new_group = -1;
258 if (cpanel->marked)
259 fname = next_file (); /* next marked file */
260 else
261 fname = selection (cpanel)->fname; /* single file */
263 if (!stat_file (fname, &sf_stat)){ /* get status of file */
264 destroy_dlg (ch_dlg);
265 break;
268 /* select in listboxes */
269 fe = listbox_search_text (l_user, get_owner(sf_stat.st_uid));
270 if (fe)
271 listbox_select_entry (l_user, fe);
273 fe = listbox_search_text (l_group, get_group(sf_stat.st_gid));
274 if (fe)
275 listbox_select_entry (l_group, fe);
277 chown_label (0, name_trunc (fname, 15));
278 chown_label (1, name_trunc (get_owner (sf_stat.st_uid), 15));
279 chown_label (2, name_trunc (get_group (sf_stat.st_gid), 15));
280 size_trunc_len (buffer, 15, sf_stat.st_size, 0);
281 chown_label (3, buffer);
282 chown_label (4, string_perm (sf_stat.st_mode));
284 run_dlg (ch_dlg);
286 switch (ch_dlg->ret_value) {
287 case B_CANCEL:
288 end_chown = 1;
289 break;
291 case B_SETUSR:
293 struct passwd *user;
295 user = getpwnam (l_user->current->text);
296 if (user){
297 new_user = user->pw_uid;
298 apply_chowns (new_user, new_group);
300 break;
302 case B_SETGRP:
304 struct group *grp;
306 grp = getgrnam (l_group->current->text);
307 if (grp){
308 new_group = grp->gr_gid;
309 apply_chowns (new_user, new_group);
311 break;
313 case B_SETALL:
314 case B_ENTER:
316 struct group *grp;
317 struct passwd *user;
319 grp = getgrnam (l_group->current->text);
320 if (grp)
321 new_group = grp->gr_gid;
322 user = getpwnam (l_user->current->text);
323 if (user)
324 new_user = user->pw_uid;
325 if (ch_dlg->ret_value==B_ENTER) {
326 need_update = 1;
327 if (mc_chown (fname, new_user, new_group) == -1)
328 message (1, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
329 fname, unix_error_string (errno));
330 } else
331 apply_chowns (new_user, new_group);
332 break;
336 if (cpanel->marked && ch_dlg->ret_value != B_CANCEL){
337 do_file_mark (cpanel, current_file, 0);
338 need_update = 1;
340 destroy_dlg (ch_dlg);
341 } while (cpanel->marked && !end_chown);
343 chown_done ();