* Makefile.am: Remove definition of CONFDIR, it's not used.
[midnight-commander.git] / src / chown.c
blob97fc1611729c429fb2f475690307c326211baf30
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 struct Dlg_head *ch_dlg;
67 static int need_update, end_chown;
68 static int current_file;
69 static int single_set;
70 static WListbox *l_user, *l_group;
72 static struct {
73 int ret_cmd, flags, y, x;
74 char *text;
75 } chown_but[BUTTONS] = {
76 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
77 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
78 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
79 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
80 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
83 #define LABELS 5
84 static struct {
85 int y, x;
86 WLabel *l;
87 } chown_label [LABELS] = {
88 { TY+2, TX+2 },
89 { TY+4, TX+2 },
90 { TY+6, TX+2 },
91 { TY+8, TX+2 },
92 { TY+10,TX+2 }
95 static void
96 chown_refresh (void)
98 attrset (COLOR_NORMAL);
99 dlg_erase (ch_dlg);
101 draw_box (ch_dlg, 1, 2, 16, 70);
102 draw_box (ch_dlg, UY, UX, 12, 21);
103 draw_box (ch_dlg, GY, GX, 12, 21);
104 draw_box (ch_dlg, TY, TX, 12, 19);
106 attrset (COLOR_NORMAL);
107 dlg_move (ch_dlg, TY + 1, TX + 1);
108 addstr (_(" Name "));
109 dlg_move (ch_dlg, TY + 3, TX + 1);
110 addstr (_(" Owner name "));
111 dlg_move (ch_dlg, TY + 5, TX + 1);
112 addstr (_(" Group name "));
113 dlg_move (ch_dlg, TY + 7, TX + 1);
114 addstr (_(" Size "));
115 dlg_move (ch_dlg, TY + 9, TX + 1);
116 addstr (_(" Permission "));
118 attrset (COLOR_HOT_NORMAL);
119 dlg_move (ch_dlg, 1, 28);
120 addstr (_(" Chown command "));
121 dlg_move (ch_dlg, UY, UX + 1);
122 addstr (_(" User name "));
123 dlg_move (ch_dlg, GY, GX + 1);
124 addstr (_(" Group name "));
125 dlg_move (ch_dlg, TY, TX + 1);
126 addstr (_(" File "));
129 static char *
130 next_file (void)
132 while (!cpanel->dir.list[current_file].f.marked)
133 current_file++;
135 return cpanel->dir.list[current_file].fname;
138 static int
139 chown_callback (Dlg_head * h, int Par, int Msg)
141 switch (Msg) {
142 case DLG_DRAW:
143 chown_refresh ();
144 break;
146 return 0;
149 static int
150 l_call (void *data)
152 return 1;
155 static void
156 init_chown (void)
158 int i;
159 struct passwd *l_pass;
160 struct group *l_grp;
162 do_refresh ();
163 end_chown = need_update = current_file = 0;
164 single_set = (cpanel->marked < 2) ? 3 : 0;
166 ch_dlg = create_dlg (0, 0, 18, 74, dialog_colors, chown_callback,
167 "[Chown]", "chown", DLG_CENTER);
169 #define XTRACT(i) BY+chown_but[i].y, BX+chown_but[i].x, chown_but[i].ret_cmd, chown_but[i].flags, _(chown_but[i].text), 0, 0, NULL
171 for (i = 0; i < BUTTONS-single_set; i++)
172 add_widget (ch_dlg, button_new (XTRACT (i)));
174 /* Add the widgets for the file information */
175 #define LX(i) chown_label [i].y, chown_label [i].x, "", NULL
176 for (i = 0; i < LABELS; i++){
177 chown_label [i].l = label_new (LX (i));
178 add_widget (ch_dlg, chown_label [i].l);
181 /* get new listboxes */
182 l_user = listbox_new (UY + 1, UX + 1, 19, 10, 0, l_call, NULL);
183 l_group = listbox_new (GY + 1, GX + 1, 19, 10, 0, l_call, NULL);
185 listbox_add_item (l_user, 0, 0, _("<Unknown user>"), NULL); /* add fields for unknown names (numbers) */
186 listbox_add_item (l_group, 0, 0, _("<Unknown group>"), NULL);
188 setpwent (); /* get and put user names in the listbox */
189 while ((l_pass = getpwent ())) {
190 listbox_add_item (l_user, 0, 0, l_pass->pw_name, NULL);
192 endpwent ();
194 setgrent (); /* get and put group names in the listbox */
195 while ((l_grp = getgrent ())) {
196 listbox_add_item (l_group, 0, 0, l_grp->gr_name, NULL);
198 endgrent ();
200 add_widget (ch_dlg, l_group);
201 add_widget (ch_dlg, l_user); /* add listboxes to the dialogs */
204 static void
205 chown_done (void)
207 if (need_update)
208 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
209 repaint_screen ();
212 static inline void
213 do_chown (uid_t u, gid_t g)
215 if (mc_chown (cpanel->dir.list [current_file].fname, u, g) == -1)
216 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
217 cpanel->dir.list [current_file].fname, unix_error_string (errno));
219 do_file_mark (cpanel, current_file, 0);
222 static void
223 apply_chowns (uid_t u, gid_t g)
225 char *fname;
227 need_update = end_chown = 1;
228 do_chown (u,g);
230 do {
231 fname = next_file ();
233 do_chown (u,g);
234 } while (cpanel->marked);
237 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
239 void
240 chown_cmd (void)
242 char *fname;
243 struct stat sf_stat;
244 WLEntry *fe;
245 uid_t new_user;
246 gid_t new_group;
247 char buffer [BUF_TINY];
249 do { /* do while any files remaining */
250 init_chown ();
251 new_user = new_group = -1;
253 if (cpanel->marked)
254 fname = next_file (); /* next marked file */
255 else
256 fname = selection (cpanel)->fname; /* single file */
258 if (!stat_file (fname, &sf_stat)){ /* get status of file */
259 destroy_dlg (ch_dlg);
260 break;
263 /* select in listboxes */
264 fe = listbox_search_text (l_user, get_owner(sf_stat.st_uid));
265 if (fe)
266 listbox_select_entry (l_user, fe);
268 fe = listbox_search_text (l_group, get_group(sf_stat.st_gid));
269 if (fe)
270 listbox_select_entry (l_group, fe);
272 chown_label (0, name_trunc (fname, 15));
273 chown_label (1, name_trunc (get_owner (sf_stat.st_uid), 15));
274 chown_label (2, name_trunc (get_group (sf_stat.st_gid), 15));
275 size_trunc_len (buffer, 15, sf_stat.st_size, 0);
276 chown_label (3, buffer);
277 chown_label (4, string_perm (sf_stat.st_mode));
279 run_dlg (ch_dlg);
281 switch (ch_dlg->ret_value) {
282 case B_CANCEL:
283 end_chown = 1;
284 break;
286 case B_SETUSR:
288 struct passwd *user;
290 user = getpwnam (l_user->current->text);
291 if (user){
292 new_user = user->pw_uid;
293 apply_chowns (new_user, new_group);
295 break;
297 case B_SETGRP:
299 struct group *grp;
301 grp = getgrnam (l_group->current->text);
302 if (grp){
303 new_group = grp->gr_gid;
304 apply_chowns (new_user, new_group);
306 break;
308 case B_SETALL:
309 case B_ENTER:
311 struct group *grp;
312 struct passwd *user;
314 grp = getgrnam (l_group->current->text);
315 if (grp)
316 new_group = grp->gr_gid;
317 user = getpwnam (l_user->current->text);
318 if (user)
319 new_user = user->pw_uid;
320 if (ch_dlg->ret_value==B_ENTER) {
321 need_update = 1;
322 if (mc_chown (fname, new_user, new_group) == -1)
323 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
324 fname, unix_error_string (errno));
325 } else
326 apply_chowns (new_user, new_group);
327 break;
331 if (cpanel->marked && ch_dlg->ret_value != B_CANCEL){
332 do_file_mark (cpanel, current_file, 0);
333 need_update = 1;
335 destroy_dlg (ch_dlg);
336 } while (cpanel->marked && !end_chown);
338 chown_done ();