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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <errno.h> /* For errno on SunOS systems */
24 #include <sys/types.h>
25 #include <sys/param.h>
39 #include "dialog.h" /* For do_refresh() */
41 /* Needed for the extern declarations of integer parameters */
43 #include "panel.h" /* Needed for the externs */
48 #include "wtools.h" /* For init_box_colors */
49 #include "../vfs/vfs.h"
65 #define B_SETALL B_USER
66 #define B_SETUSR B_USER + 1
67 #define B_SETGRP B_USER + 2
69 /* struct stat *sf_stat; */
70 static int need_update
, end_chown
;
71 static int current_file
;
72 static int single_set
;
73 static WListbox
*l_user
, *l_group
;
76 int ret_cmd
, flags
, y
, x
;
78 } chown_but
[BUTTONS
] = {
79 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel") },
80 { B_ENTER
, DEFPUSH_BUTTON
, 0, 40, N_("&Set") },
81 { B_SETUSR
, NORMAL_BUTTON
, 0, 25, N_("Set &users") },
82 { B_SETGRP
, NORMAL_BUTTON
, 0, 11, N_("Set &groups") },
83 { B_SETALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
90 } chown_label
[LABELS
] = {
102 attrset (COLOR_NORMAL
);
105 draw_box (ch_dlg
, 1, 2, 16, 70);
106 draw_box (ch_dlg
, UY
, UX
, 12, 21);
107 draw_box (ch_dlg
, GY
, GX
, 12, 21);
108 draw_box (ch_dlg
, TY
, TX
, 12, 19);
110 attrset (COLOR_NORMAL
);
111 dlg_move (ch_dlg
, TY
+ 1, TX
+ 1);
112 addstr (_(" Name "));
113 dlg_move (ch_dlg
, TY
+ 3, TX
+ 1);
114 addstr (_(" Owner name "));
115 dlg_move (ch_dlg
, TY
+ 5, TX
+ 1);
116 addstr (_(" Group name "));
117 dlg_move (ch_dlg
, TY
+ 7, TX
+ 1);
118 addstr (_(" Size "));
119 dlg_move (ch_dlg
, TY
+ 9, TX
+ 1);
120 addstr (_(" Permission "));
122 attrset (COLOR_HOT_NORMAL
);
123 dlg_move (ch_dlg
, 1, 28);
124 addstr (_(" Chown command "));
125 dlg_move (ch_dlg
, UY
, UX
+ 1);
126 addstr (_(" User name "));
127 dlg_move (ch_dlg
, GY
, GX
+ 1);
128 addstr (_(" Group name "));
129 dlg_move (ch_dlg
, TY
, TX
+ 1);
130 addstr (_(" File "));
137 while (!cpanel
->dir
.list
[current_file
].f
.marked
)
140 return cpanel
->dir
.list
[current_file
].fname
;
144 chown_callback (Dlg_head
* h
, int Par
, int Msg
)
166 struct passwd
*l_pass
;
170 end_chown
= need_update
= current_file
= 0;
171 single_set
= (cpanel
->marked
< 2) ? 3 : 0;
173 ch_dlg
= create_dlg (0, 0, 18, 74, dialog_colors
, chown_callback
,
174 "[Chown]", "chown", DLG_CENTER
);
176 #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
178 for (i
= 0; i
< BUTTONS
-single_set
; i
++)
179 add_widget (ch_dlg
, button_new (XTRACT (i
)));
181 /* Add the widgets for the file information */
182 #define LX(i) chown_label [i].y, chown_label [i].x, "", NULL
183 for (i
= 0; i
< LABELS
; i
++){
184 chown_label
[i
].l
= label_new (LX (i
));
185 add_widget (ch_dlg
, chown_label
[i
].l
);
188 /* get new listboxes */
189 l_user
= listbox_new (UY
+ 1, UX
+ 1, 19, 10, 0, l_call
, NULL
);
190 l_group
= listbox_new (GY
+ 1, GX
+ 1, 19, 10, 0, l_call
, NULL
);
192 listbox_add_item (l_user
, 0, 0, _("<Unknown user>"), NULL
); /* add fields for unknown names (numbers) */
193 listbox_add_item (l_group
, 0, 0, _("<Unknown group>"), NULL
);
195 setpwent (); /* get and put user names in the listbox */
196 while ((l_pass
= getpwent ())) {
197 listbox_add_item (l_user
, 0, 0, l_pass
->pw_name
, NULL
);
201 setgrent (); /* get and put group names in the listbox */
202 while ((l_grp
= getgrent ())) {
203 listbox_add_item (l_group
, 0, 0, l_grp
->gr_name
, NULL
);
207 add_widget (ch_dlg
, l_group
);
208 add_widget (ch_dlg
, l_user
); /* add listboxes to the dialogs */
215 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
220 do_chown (uid_t u
, gid_t g
)
222 if (mc_chown (cpanel
->dir
.list
[current_file
].fname
, u
, g
) == -1)
223 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
224 cpanel
->dir
.list
[current_file
].fname
, unix_error_string (errno
));
226 do_file_mark (cpanel
, current_file
, 0);
230 apply_chowns (uid_t u
, gid_t g
)
234 need_update
= end_chown
= 1;
238 fname
= next_file ();
241 } while (cpanel
->marked
);
244 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
254 char buffer
[BUF_TINY
];
258 if (!vfs_current_is_local ()) {
259 if (vfs_current_is_extfs ()) {
260 message (1, _(" Oops... "),
261 _(" I can't run the Chown command on an extfs "));
263 } else if (vfs_current_is_tarfs ()) {
264 message (1, _(" Oops... "),
265 _(" I can't run the Chown command on a tarfs "));
271 do { /* do while any files remaining */
273 new_user
= new_group
= -1;
276 fname
= next_file (); /* next marked file */
278 fname
= selection (cpanel
)->fname
; /* single file */
280 if (!stat_file (fname
, &sf_stat
)){ /* get status of file */
281 destroy_dlg (ch_dlg
);
285 /* select in listboxes */
286 fe
= listbox_search_text (l_user
, get_owner(sf_stat
.st_uid
));
288 listbox_select_entry (l_user
, fe
);
290 fe
= listbox_search_text (l_group
, get_group(sf_stat
.st_gid
));
292 listbox_select_entry (l_group
, fe
);
294 chown_label (0, name_trunc (fname
, 15));
295 chown_label (1, name_trunc (get_owner (sf_stat
.st_uid
), 15));
296 chown_label (2, name_trunc (get_group (sf_stat
.st_gid
), 15));
297 g_snprintf (buffer
, sizeof (buffer
), "%d", c_fsize
);
298 chown_label (3, buffer
);
299 chown_label (4, string_perm (sf_stat
.st_mode
));
303 switch (ch_dlg
->ret_value
) {
312 user
= getpwnam (l_user
->current
->text
);
314 new_user
= user
->pw_uid
;
315 apply_chowns (new_user
, new_group
);
323 grp
= getgrnam (l_group
->current
->text
);
325 new_group
= grp
->gr_gid
;
326 apply_chowns (new_user
, new_group
);
336 grp
= getgrnam (l_group
->current
->text
);
338 new_group
= grp
->gr_gid
;
339 user
= getpwnam (l_user
->current
->text
);
341 new_user
= user
->pw_uid
;
342 if (ch_dlg
->ret_value
==B_ENTER
) {
344 if (mc_chown (fname
, new_user
, new_group
) == -1)
345 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
346 fname
, unix_error_string (errno
));
348 apply_chowns (new_user
, new_group
);
353 if (cpanel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
){
354 do_file_mark (cpanel
, current_file
, 0);
357 destroy_dlg (ch_dlg
);
358 } while (cpanel
->marked
&& !end_chown
);