small typo
[midnight-commander.git] / src / chown.c
blob7ec112ae0f960b5db37b63324b97e6eba109e84d
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 "file.h"
42 #include "chmod.h"
43 #include "main.h"
44 #include "chown.h"
45 #include "wtools.h" /* For init_box_colors */
46 #include "../vfs/vfs.h"
48 #define UX 5
49 #define UY 2
51 #define GX 27
52 #define GY 2
54 #define BX 5
55 #define BY 15
57 #define TX 50
58 #define TY 2
60 #define BUTTONS 5
62 #define B_SETALL B_USER
63 #define B_SETUSR B_USER + 1
64 #define B_SETGRP B_USER + 2
66 static int need_update, end_chown;
67 static int current_file;
68 static int single_set;
69 static WListbox *l_user, *l_group;
71 static struct {
72 int ret_cmd, flags, y, x;
73 char *text;
74 } chown_but[BUTTONS] = {
75 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
76 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
77 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
78 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
79 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
82 #define LABELS 5
83 static struct {
84 int y, x;
85 WLabel *l;
86 } chown_label [LABELS] = {
87 { TY+2, TX+2 },
88 { TY+4, TX+2 },
89 { TY+6, TX+2 },
90 { TY+8, TX+2 },
91 { TY+10,TX+2 }
94 static void
95 chown_refresh (Dlg_head *h)
97 common_dialog_repaint (h);
99 attrset (COLOR_NORMAL);
101 draw_box (h, UY, UX, 12, 21);
102 draw_box (h, GY, GX, 12, 21);
103 draw_box (h, TY, TX, 12, 19);
105 dlg_move (h, TY + 1, TX + 1);
106 addstr (_(" Name "));
107 dlg_move (h, TY + 3, TX + 1);
108 addstr (_(" Owner name "));
109 dlg_move (h, TY + 5, TX + 1);
110 addstr (_(" Group name "));
111 dlg_move (h, TY + 7, TX + 1);
112 addstr (_(" Size "));
113 dlg_move (h, TY + 9, TX + 1);
114 addstr (_(" Permission "));
116 attrset (COLOR_HOT_NORMAL);
117 dlg_move (h, UY, UX + 1);
118 addstr (_(" User name "));
119 dlg_move (h, GY, GX + 1);
120 addstr (_(" Group name "));
121 dlg_move (h, TY, TX + 1);
122 addstr (_(" File "));
125 static char *
126 next_file (void)
128 while (!cpanel->dir.list[current_file].f.marked)
129 current_file++;
131 return cpanel->dir.list[current_file].fname;
134 static int
135 chown_callback (Dlg_head * h, int Par, int Msg)
137 switch (Msg) {
138 case DLG_DRAW:
139 chown_refresh (h);
140 break;
142 return 0;
145 static int
146 l_call (void *data)
148 return 1;
151 static Dlg_head *
152 init_chown (void)
154 int i;
155 struct passwd *l_pass;
156 struct group *l_grp;
157 Dlg_head *ch_dlg;
159 do_refresh ();
160 end_chown = need_update = current_file = 0;
161 single_set = (cpanel->marked < 2) ? 3 : 0;
163 ch_dlg =
164 create_dlg (0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
165 _(" Chown command "), DLG_CENTER);
167 for (i = 0; i < BUTTONS - single_set; i++)
168 add_widget (ch_dlg,
169 button_new (BY + chown_but[i].y, BX + chown_but[i].x,
170 chown_but[i].ret_cmd, chown_but[i].flags,
171 _(chown_but[i].text), 0, 0, NULL));
173 /* Add the widgets for the file information */
174 for (i = 0; i < LABELS; i++) {
175 chown_label[i].l =
176 label_new (chown_label[i].y, chown_label[i].x, "", NULL);
177 add_widget (ch_dlg, chown_label[i].l);
180 /* get new listboxes */
181 l_user = listbox_new (UY + 1, UX + 1, 19, 10, 0, l_call, NULL);
182 l_group = listbox_new (GY + 1, GX + 1, 19, 10, 0, l_call, NULL);
184 /* add fields for unknown names (numbers) */
185 listbox_add_item (l_user, 0, 0, _("<Unknown user>"), NULL);
186 listbox_add_item (l_group, 0, 0, _("<Unknown group>"), NULL);
188 /* get and put user names in the listbox */
189 setpwent ();
190 while ((l_pass = getpwent ())) {
191 listbox_add_item (l_user, 0, 0, l_pass->pw_name, NULL);
193 endpwent ();
195 /* get and put group names in the listbox */
196 setgrent ();
197 while ((l_grp = getgrent ())) {
198 listbox_add_item (l_group, 0, 0, l_grp->gr_name, NULL);
200 endgrent ();
202 /* add listboxes to the dialogs */
203 add_widget (ch_dlg, l_group);
204 add_widget (ch_dlg, l_user);
206 return ch_dlg;
209 static void
210 chown_done (void)
212 if (need_update)
213 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
214 repaint_screen ();
217 static inline void
218 do_chown (uid_t u, gid_t g)
220 if (mc_chown (cpanel->dir.list [current_file].fname, u, g) == -1)
221 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
222 cpanel->dir.list [current_file].fname, unix_error_string (errno));
224 do_file_mark (cpanel, current_file, 0);
227 static void
228 apply_chowns (uid_t u, gid_t g)
230 char *fname;
232 need_update = end_chown = 1;
233 do_chown (u,g);
235 do {
236 fname = next_file ();
238 do_chown (u,g);
239 } while (cpanel->marked);
242 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
244 void
245 chown_cmd (void)
247 char *fname;
248 struct stat sf_stat;
249 WLEntry *fe;
250 Dlg_head *ch_dlg;
251 uid_t new_user;
252 gid_t new_group;
253 char buffer [BUF_TINY];
255 do { /* do while any files remaining */
256 ch_dlg = init_chown ();
257 new_user = new_group = -1;
259 if (cpanel->marked)
260 fname = next_file (); /* next marked file */
261 else
262 fname = selection (cpanel)->fname; /* single file */
264 if (!stat_file (fname, &sf_stat)){ /* get status of file */
265 destroy_dlg (ch_dlg);
266 break;
269 /* select in listboxes */
270 fe = listbox_search_text (l_user, get_owner(sf_stat.st_uid));
271 if (fe)
272 listbox_select_entry (l_user, fe);
274 fe = listbox_search_text (l_group, get_group(sf_stat.st_gid));
275 if (fe)
276 listbox_select_entry (l_group, fe);
278 chown_label (0, name_trunc (fname, 15));
279 chown_label (1, name_trunc (get_owner (sf_stat.st_uid), 15));
280 chown_label (2, name_trunc (get_group (sf_stat.st_gid), 15));
281 size_trunc_len (buffer, 15, sf_stat.st_size, 0);
282 chown_label (3, buffer);
283 chown_label (4, string_perm (sf_stat.st_mode));
285 run_dlg (ch_dlg);
287 switch (ch_dlg->ret_value) {
288 case B_CANCEL:
289 end_chown = 1;
290 break;
292 case B_SETUSR:
294 struct passwd *user;
296 user = getpwnam (l_user->current->text);
297 if (user){
298 new_user = user->pw_uid;
299 apply_chowns (new_user, new_group);
301 break;
303 case B_SETGRP:
305 struct group *grp;
307 grp = getgrnam (l_group->current->text);
308 if (grp){
309 new_group = grp->gr_gid;
310 apply_chowns (new_user, new_group);
312 break;
314 case B_SETALL:
315 case B_ENTER:
317 struct group *grp;
318 struct passwd *user;
320 grp = getgrnam (l_group->current->text);
321 if (grp)
322 new_group = grp->gr_gid;
323 user = getpwnam (l_user->current->text);
324 if (user)
325 new_user = user->pw_uid;
326 if (ch_dlg->ret_value==B_ENTER) {
327 need_update = 1;
328 if (mc_chown (fname, new_user, new_group) == -1)
329 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
330 fname, unix_error_string (errno));
331 } else
332 apply_chowns (new_user, new_group);
333 break;
337 if (cpanel->marked && ch_dlg->ret_value != B_CANCEL){
338 do_file_mark (cpanel, current_file, 0);
339 need_update = 1;
341 destroy_dlg (ch_dlg);
342 } while (cpanel->marked && !end_chown);
344 chown_done ();