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.
22 #include <errno.h> /* For errno on SunOS systems */
24 #include <sys/types.h>
36 #include "dialog.h" /* For do_refresh() */
38 /* Needed for the extern declarations of integer parameters */
40 #include "panel.h" /* Needed for the externs */
45 #include "wtools.h" /* For init_box_colors */
46 #include "../vfs/vfs.h"
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
;
74 int ret_cmd
, flags
, y
, x
;
76 } chown_but
[BUTTONS
] = {
77 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel") },
78 { B_ENTER
, DEFPUSH_BUTTON
, 0, 40, N_("&Set") },
79 { B_SETUSR
, NORMAL_BUTTON
, 0, 25, N_("Set &users") },
80 { B_SETGRP
, NORMAL_BUTTON
, 0, 11, N_("Set &groups") },
81 { B_SETALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
88 } chown_label
[LABELS
] = {
100 attrset (COLOR_NORMAL
);
103 draw_box (ch_dlg
, 1, 2, 16, 70);
104 draw_box (ch_dlg
, UY
, UX
, 12, 21);
105 draw_box (ch_dlg
, GY
, GX
, 12, 21);
106 draw_box (ch_dlg
, TY
, TX
, 12, 19);
108 attrset (COLOR_NORMAL
);
109 dlg_move (ch_dlg
, TY
+ 1, TX
+ 1);
110 addstr (_(" Name "));
111 dlg_move (ch_dlg
, TY
+ 3, TX
+ 1);
112 addstr (_(" Owner name "));
113 dlg_move (ch_dlg
, TY
+ 5, TX
+ 1);
114 addstr (_(" Group name "));
115 dlg_move (ch_dlg
, TY
+ 7, TX
+ 1);
116 addstr (_(" Size "));
117 dlg_move (ch_dlg
, TY
+ 9, TX
+ 1);
118 addstr (_(" Permission "));
120 attrset (COLOR_HOT_NORMAL
);
121 dlg_move (ch_dlg
, 1, 28);
122 addstr (_(" Chown command "));
123 dlg_move (ch_dlg
, UY
, UX
+ 1);
124 addstr (_(" User name "));
125 dlg_move (ch_dlg
, GY
, GX
+ 1);
126 addstr (_(" Group name "));
127 dlg_move (ch_dlg
, TY
, TX
+ 1);
128 addstr (_(" File "));
135 while (!cpanel
->dir
.list
[current_file
].f
.marked
)
138 return cpanel
->dir
.list
[current_file
].fname
;
142 chown_callback (Dlg_head
* h
, int Par
, int Msg
)
164 struct passwd
*l_pass
;
168 end_chown
= need_update
= current_file
= 0;
169 single_set
= (cpanel
->marked
< 2) ? 3 : 0;
171 ch_dlg
= create_dlg (0, 0, 18, 74, dialog_colors
, chown_callback
,
172 "[Chown]", "chown", DLG_CENTER
);
174 #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
176 for (i
= 0; i
< BUTTONS
-single_set
; i
++)
177 add_widget (ch_dlg
, button_new (XTRACT (i
)));
179 /* Add the widgets for the file information */
180 #define LX(i) chown_label [i].y, chown_label [i].x, "", NULL
181 for (i
= 0; i
< LABELS
; i
++){
182 chown_label
[i
].l
= label_new (LX (i
));
183 add_widget (ch_dlg
, chown_label
[i
].l
);
186 /* get new listboxes */
187 l_user
= listbox_new (UY
+ 1, UX
+ 1, 19, 10, 0, l_call
, NULL
);
188 l_group
= listbox_new (GY
+ 1, GX
+ 1, 19, 10, 0, l_call
, NULL
);
190 listbox_add_item (l_user
, 0, 0, _("<Unknown user>"), NULL
); /* add fields for unknown names (numbers) */
191 listbox_add_item (l_group
, 0, 0, _("<Unknown group>"), NULL
);
193 setpwent (); /* get and put user names in the listbox */
194 while ((l_pass
= getpwent ())) {
195 listbox_add_item (l_user
, 0, 0, l_pass
->pw_name
, NULL
);
199 setgrent (); /* get and put group names in the listbox */
200 while ((l_grp
= getgrent ())) {
201 listbox_add_item (l_group
, 0, 0, l_grp
->gr_name
, NULL
);
205 add_widget (ch_dlg
, l_group
);
206 add_widget (ch_dlg
, l_user
); /* add listboxes to the dialogs */
213 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
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);
228 apply_chowns (uid_t u
, gid_t g
)
232 need_update
= end_chown
= 1;
236 fname
= next_file ();
239 } while (cpanel
->marked
);
242 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
252 char buffer
[BUF_TINY
];
254 do { /* do while any files remaining */
256 new_user
= new_group
= -1;
259 fname
= next_file (); /* next marked file */
261 fname
= selection (cpanel
)->fname
; /* single file */
263 if (!stat_file (fname
, &sf_stat
)){ /* get status of file */
264 destroy_dlg (ch_dlg
);
268 /* select in listboxes */
269 fe
= listbox_search_text (l_user
, get_owner(sf_stat
.st_uid
));
271 listbox_select_entry (l_user
, fe
);
273 fe
= listbox_search_text (l_group
, get_group(sf_stat
.st_gid
));
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 g_snprintf (buffer
, sizeof (buffer
), "%d", c_fsize
);
281 chown_label (3, buffer
);
282 chown_label (4, string_perm (sf_stat
.st_mode
));
286 switch (ch_dlg
->ret_value
) {
295 user
= getpwnam (l_user
->current
->text
);
297 new_user
= user
->pw_uid
;
298 apply_chowns (new_user
, new_group
);
306 grp
= getgrnam (l_group
->current
->text
);
308 new_group
= grp
->gr_gid
;
309 apply_chowns (new_user
, new_group
);
319 grp
= getgrnam (l_group
->current
->text
);
321 new_group
= grp
->gr_gid
;
322 user
= getpwnam (l_user
->current
->text
);
324 new_user
= user
->pw_uid
;
325 if (ch_dlg
->ret_value
==B_ENTER
) {
327 if (mc_chown (fname
, new_user
, new_group
) == -1)
328 message (1, MSG_ERROR
, _(" Couldn't chown \"%s\" \n %s "),
329 fname
, unix_error_string (errno
));
331 apply_chowns (new_user
, new_group
);
336 if (cpanel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
){
337 do_file_mark (cpanel
, current_file
, 0);
340 destroy_dlg (ch_dlg
);
341 } while (cpanel
->marked
&& !end_chown
);