2 Chown command -- for the Midnight Commander
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * \brief Source: chown command
35 #include <sys/types.h>
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
43 #include "lib/vfs/vfs.h"
44 #include "lib/strutil.h"
46 #include "lib/widget.h"
48 #include "src/setup.h" /* panels_options */
50 /* Needed for the extern declarations of integer parameters */
52 #include "midnight.h" /* current_panel */
56 /*** global variables ****************************************************************************/
58 /*** file scope macro definitions ****************************************************************/
74 #define B_SETALL B_USER
75 #define B_SETUSR (B_USER + 1)
76 #define B_SETGRP (B_USER + 2)
80 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
82 /*** file scope type declarations ****************************************************************/
84 /*** file scope variables ************************************************************************/
86 static int need_update
, end_chown
;
87 static int current_file
;
88 static int single_set
;
89 static WListbox
*l_user
, *l_group
;
94 int ret_cmd
, flags
, y
, x
;
96 } chown_but
[BUTTONS
] = {
97 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel") },
98 { B_ENTER
, DEFPUSH_BUTTON
, 0, 40, N_("&Set") },
99 { B_SETUSR
, NORMAL_BUTTON
, 0, 25, N_("Set &users") },
100 { B_SETGRP
, NORMAL_BUTTON
, 0, 11, N_("Set &groups") },
101 { B_SETALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
107 } chown_label
[LABELS
] = {
108 { TY
+ 2, TX
+ 2, NULL
},
109 { TY
+ 4, TX
+ 2, NULL
},
110 { TY
+ 6, TX
+ 2, NULL
},
111 { TY
+ 8, TX
+ 2, NULL
},
112 { TY
+ 10, TX
+ 2, NULL
}
116 /*** file scope functions ************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
120 chown_refresh (Dlg_head
* h
)
122 common_dialog_repaint (h
);
124 tty_setcolor (COLOR_NORMAL
);
126 dlg_move (h
, TY
+ 1, TX
+ 2);
127 tty_print_string (_("Name"));
128 dlg_move (h
, TY
+ 3, TX
+ 2);
129 tty_print_string (_("Owner name"));
130 dlg_move (h
, TY
+ 5, TX
+ 2);
131 tty_print_string (_("Group name"));
132 dlg_move (h
, TY
+ 7, TX
+ 2);
133 tty_print_string (_("Size"));
134 dlg_move (h
, TY
+ 9, TX
+ 2);
135 tty_print_string (_("Permission"));
138 /* --------------------------------------------------------------------------------------------- */
143 while (!current_panel
->dir
.list
[current_file
].f
.marked
)
146 return current_panel
->dir
.list
[current_file
].fname
;
149 /* --------------------------------------------------------------------------------------------- */
152 chown_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
161 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
165 /* --------------------------------------------------------------------------------------------- */
171 struct passwd
*l_pass
;
176 end_chown
= need_update
= current_file
= 0;
177 single_set
= (current_panel
->marked
< 2) ? 3 : 0;
180 create_dlg (TRUE
, 0, 0, 18, 74, dialog_colors
, chown_callback
, "[Chown]",
181 _("Chown command"), DLG_CENTER
| DLG_REVERSE
);
183 for (i
= 0; i
< BUTTONS
- single_set
; i
++)
185 button_new (BY
+ chown_but
[i
].y
, BX
+ chown_but
[i
].x
,
186 chown_but
[i
].ret_cmd
, chown_but
[i
].flags
, _(chown_but
[i
].text
), 0));
188 /* Add the widgets for the file information */
189 for (i
= 0; i
< LABELS
; i
++)
191 chown_label
[i
].l
= label_new (chown_label
[i
].y
, chown_label
[i
].x
, "");
192 add_widget (ch_dlg
, chown_label
[i
].l
);
195 /* get new listboxes */
196 l_user
= listbox_new (UY
+ 1, UX
+ 1, 10, 19, FALSE
, NULL
);
197 l_group
= listbox_new (GY
+ 1, GX
+ 1, 10, 19, FALSE
, NULL
);
199 /* add fields for unknown names (numbers) */
200 listbox_add_item (l_user
, LISTBOX_APPEND_AT_END
, 0, _("<Unknown user>"), NULL
);
201 listbox_add_item (l_group
, LISTBOX_APPEND_AT_END
, 0, _("<Unknown group>"), NULL
);
203 /* get and put user names in the listbox */
205 while ((l_pass
= getpwent ()) != NULL
)
207 listbox_add_item (l_user
, LISTBOX_APPEND_SORTED
, 0, l_pass
->pw_name
, NULL
);
211 /* get and put group names in the listbox */
213 while ((l_grp
= getgrent ()) != NULL
)
215 listbox_add_item (l_group
, LISTBOX_APPEND_SORTED
, 0, l_grp
->gr_name
, NULL
);
219 add_widget (ch_dlg
, groupbox_new (TY
, TX
, 12, 19, _("File")));
221 /* add listboxes to the dialogs */
222 add_widget (ch_dlg
, l_group
);
223 add_widget (ch_dlg
, groupbox_new (GY
, GX
, 12, 21, _("Group name")));
224 add_widget (ch_dlg
, l_user
);
225 add_widget (ch_dlg
, groupbox_new (UY
, UX
, 12, 21, _("User name")));
230 /* --------------------------------------------------------------------------------------------- */
236 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
240 /* --------------------------------------------------------------------------------------------- */
243 do_chown (uid_t u
, gid_t g
)
247 vpath
= vfs_path_from_str (current_panel
->dir
.list
[current_file
].fname
);
248 if (mc_chown (vpath
, u
, g
) == -1)
249 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
250 current_panel
->dir
.list
[current_file
].fname
, unix_error_string (errno
));
252 vfs_path_free (vpath
);
253 do_file_mark (current_panel
, current_file
, 0);
256 /* --------------------------------------------------------------------------------------------- */
259 apply_chowns (uid_t u
, gid_t g
)
263 need_update
= end_chown
= 1;
268 fname
= next_file ();
272 while (current_panel
->marked
);
275 /* --------------------------------------------------------------------------------------------- */
276 /*** public functions ****************************************************************************/
277 /* --------------------------------------------------------------------------------------------- */
287 char buffer
[BUF_TINY
];
290 { /* do while any files remaining */
293 ch_dlg
= init_chown ();
294 new_user
= new_group
= -1;
296 if (current_panel
->marked
)
297 fname
= next_file (); /* next marked file */
299 fname
= selection (current_panel
)->fname
; /* single file */
301 vpath
= vfs_path_from_str (fname
);
302 if (mc_stat (vpath
, &sf_stat
) != 0)
303 { /* get status of file */
304 destroy_dlg (ch_dlg
);
305 vfs_path_free (vpath
);
308 vfs_path_free (vpath
);
310 /* select in listboxes */
311 listbox_select_entry (l_user
, listbox_search_text (l_user
, get_owner (sf_stat
.st_uid
)));
312 listbox_select_entry (l_group
, listbox_search_text (l_group
, get_group (sf_stat
.st_gid
)));
314 chown_label (0, str_trunc (fname
, 15));
315 chown_label (1, str_trunc (get_owner (sf_stat
.st_uid
), 15));
316 chown_label (2, str_trunc (get_group (sf_stat
.st_gid
), 15));
317 size_trunc_len (buffer
, 15, sf_stat
.st_size
, 0, panels_options
.kilobyte_si
);
318 chown_label (3, buffer
);
319 chown_label (4, string_perm (sf_stat
.st_mode
));
321 switch (run_dlg (ch_dlg
))
332 listbox_get_current (l_user
, &text
, NULL
);
333 user
= getpwnam (text
);
336 new_user
= user
->pw_uid
;
337 apply_chowns (new_user
, new_group
);
347 listbox_get_current (l_group
, &text
, NULL
);
348 grp
= getgrnam (text
);
351 new_group
= grp
->gr_gid
;
352 apply_chowns (new_user
, new_group
);
364 listbox_get_current (l_group
, &text
, NULL
);
365 grp
= getgrnam (text
);
367 new_group
= grp
->gr_gid
;
368 listbox_get_current (l_user
, &text
, NULL
);
369 user
= getpwnam (text
);
371 new_user
= user
->pw_uid
;
372 if (ch_dlg
->ret_value
== B_ENTER
)
374 vfs_path_t
*fname_vpath
;
376 fname_vpath
= vfs_path_from_str (fname
);
378 if (mc_chown (fname_vpath
, new_user
, new_group
) == -1)
379 message (D_ERROR
, MSG_ERROR
, _("Cannot chown \"%s\"\n%s"),
380 fname
, unix_error_string (errno
));
381 vfs_path_free (vpath
);
384 apply_chowns (new_user
, new_group
);
389 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
)
391 do_file_mark (current_panel
, current_file
, 0);
395 destroy_dlg (ch_dlg
);
397 while (current_panel
->marked
&& !end_chown
);
402 /* --------------------------------------------------------------------------------------------- */