1998-02-27 07:54:42 +03:00
|
|
|
/* Chown command -- for the Midnight Commander
|
|
|
|
Copyright (C) 1994 Radek Doulik
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2000-08-23 02:50:00 +04:00
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
1998-02-27 07:54:42 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h> /* For errno on SunOS systems */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
Glibing..... (2)
Wed Jan 27 03:17:44 1999 Timur Bakeyev <mc@bat.ru>
* Converted memory managment to Glib. Now we use g_new()/g_malloc()/
g_strdup()/g_free() routings. Also, copy_strings() replaced by
g_strconcat(), strcasecmp() -> g_strcasecmp(),and sprintf() by
g_snprintf().
* Some sequences of malloc()/sprintf() changed to g_strdup_printf().
* mad.[ch]: Modified, to work with new GLib's memory managment. Fixed
a missing #undef for tempnam, which caused dead loop. Add several new
functions to emulate GLib memory managment.
*main.c, mad.[ch]: Add a new switch "-M", which allows to redirect MAD
messages to the file.
* util.[ch], utilunix.c: Modified, deleted our variants of strcasecmp()
and strdup() - we have g_ equivalences. Remove get_full_name() - it is
similar to concat_dir_and_file(). Some other tricks with g_* functions.
* global.h: Modified, extended. Now it is main memory mangment include -
i.e. all inclusions of <stdlib.h>, <malloc.h>, <glib.h>, "fs.h", "mem.h",
"util.h" and "mad.h" done there. This elimanates problem with proper or-
der of #include's.
* All around the source - changed order of #include's, most of them gone
to global.h (see above), minor changes, like "0" -> NULL in string func-
tions.
1999-01-27 04:08:30 +03:00
|
|
|
#include "global.h"
|
2001-09-07 21:52:19 +04:00
|
|
|
#include "tty.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
#include "win.h"
|
|
|
|
#include "color.h"
|
2003-10-25 03:20:30 +04:00
|
|
|
#include "dialog.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
#include "widget.h"
|
|
|
|
|
|
|
|
/* Needed for the extern declarations of integer parameters */
|
|
|
|
#include "dir.h"
|
|
|
|
#include "panel.h" /* Needed for the externs */
|
|
|
|
#include "chmod.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "chown.h"
|
|
|
|
#include "wtools.h" /* For init_box_colors */
|
|
|
|
|
|
|
|
#define UX 5
|
|
|
|
#define UY 2
|
|
|
|
|
|
|
|
#define GX 27
|
|
|
|
#define GY 2
|
|
|
|
|
|
|
|
#define BX 5
|
|
|
|
#define BY 15
|
|
|
|
|
|
|
|
#define TX 50
|
|
|
|
#define TY 2
|
|
|
|
|
|
|
|
#define BUTTONS 5
|
|
|
|
|
|
|
|
#define B_SETALL B_USER
|
2003-09-11 01:33:12 +04:00
|
|
|
#define B_SETUSR (B_USER + 1)
|
|
|
|
#define B_SETGRP (B_USER + 2)
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
static int need_update, end_chown;
|
|
|
|
static int current_file;
|
|
|
|
static int single_set;
|
|
|
|
static WListbox *l_user, *l_group;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
int ret_cmd, flags, y, x;
|
|
|
|
char *text;
|
|
|
|
} chown_but[BUTTONS] = {
|
1999-01-31 01:45:53 +03:00
|
|
|
{ B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
|
|
|
|
{ B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
|
|
|
|
{ B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
|
|
|
|
{ B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
|
|
|
|
{ B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
|
1998-02-27 07:54:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#define LABELS 5
|
|
|
|
static struct {
|
|
|
|
int y, x;
|
|
|
|
WLabel *l;
|
|
|
|
} chown_label [LABELS] = {
|
|
|
|
{ TY+2, TX+2 },
|
|
|
|
{ TY+4, TX+2 },
|
|
|
|
{ TY+6, TX+2 },
|
|
|
|
{ TY+8, TX+2 },
|
|
|
|
{ TY+10,TX+2 }
|
|
|
|
};
|
|
|
|
|
1998-12-03 00:27:27 +03:00
|
|
|
static void
|
2002-09-02 22:03:15 +04:00
|
|
|
chown_refresh (Dlg_head *h)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2002-09-02 22:03:15 +04:00
|
|
|
common_dialog_repaint (h);
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
attrset (COLOR_NORMAL);
|
|
|
|
|
2002-09-02 22:03:15 +04:00
|
|
|
draw_box (h, UY, UX, 12, 21);
|
|
|
|
draw_box (h, GY, GX, 12, 21);
|
|
|
|
draw_box (h, TY, TX, 12, 19);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-09-02 22:03:15 +04:00
|
|
|
dlg_move (h, TY + 1, TX + 1);
|
1999-01-31 01:45:53 +03:00
|
|
|
addstr (_(" Name "));
|
2002-09-02 22:03:15 +04:00
|
|
|
dlg_move (h, TY + 3, TX + 1);
|
1999-01-31 01:45:53 +03:00
|
|
|
addstr (_(" Owner name "));
|
2002-09-02 22:03:15 +04:00
|
|
|
dlg_move (h, TY + 5, TX + 1);
|
1999-01-31 01:45:53 +03:00
|
|
|
addstr (_(" Group name "));
|
2002-09-02 22:03:15 +04:00
|
|
|
dlg_move (h, TY + 7, TX + 1);
|
1999-01-31 01:45:53 +03:00
|
|
|
addstr (_(" Size "));
|
2002-09-02 22:03:15 +04:00
|
|
|
dlg_move (h, TY + 9, TX + 1);
|
1999-01-31 01:45:53 +03:00
|
|
|
addstr (_(" Permission "));
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
attrset (COLOR_HOT_NORMAL);
|
2002-09-02 22:03:15 +04:00
|
|
|
dlg_move (h, UY, UX + 1);
|
1999-01-31 01:45:53 +03:00
|
|
|
addstr (_(" User name "));
|
2002-09-02 22:03:15 +04:00
|
|
|
dlg_move (h, GY, GX + 1);
|
1999-01-31 01:45:53 +03:00
|
|
|
addstr (_(" Group name "));
|
2002-09-02 22:03:15 +04:00
|
|
|
dlg_move (h, TY, TX + 1);
|
1999-01-31 01:45:53 +03:00
|
|
|
addstr (_(" File "));
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-12-03 00:27:27 +03:00
|
|
|
static char *
|
|
|
|
next_file (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
while (!cpanel->dir.list[current_file].f.marked)
|
|
|
|
current_file++;
|
|
|
|
|
|
|
|
return cpanel->dir.list[current_file].fname;
|
|
|
|
}
|
|
|
|
|
2003-09-08 01:24:01 +04:00
|
|
|
static cb_ret_t
|
|
|
|
chown_callback (Dlg_head *h, dlg_msg_t msg, int parm)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-09-08 01:24:01 +04:00
|
|
|
switch (msg) {
|
1998-02-27 07:54:42 +03:00
|
|
|
case DLG_DRAW:
|
2002-09-02 22:03:15 +04:00
|
|
|
chown_refresh (h);
|
2003-09-08 01:24:01 +04:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return default_dlg_callback (h, msg, parm);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-02 22:03:15 +04:00
|
|
|
static Dlg_head *
|
1998-12-03 00:27:27 +03:00
|
|
|
init_chown (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct passwd *l_pass;
|
|
|
|
struct group *l_grp;
|
2002-09-02 22:03:15 +04:00
|
|
|
Dlg_head *ch_dlg;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
do_refresh ();
|
|
|
|
end_chown = need_update = current_file = 0;
|
2002-09-02 22:03:15 +04:00
|
|
|
single_set = (cpanel->marked < 2) ? 3 : 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-09-02 22:03:15 +04:00
|
|
|
ch_dlg =
|
|
|
|
create_dlg (0, 0, 18, 74, dialog_colors, chown_callback, "[Chown]",
|
2003-09-13 01:22:01 +04:00
|
|
|
_(" Chown command "), DLG_CENTER | DLG_REVERSE);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-09-02 22:03:15 +04:00
|
|
|
for (i = 0; i < BUTTONS - single_set; i++)
|
|
|
|
add_widget (ch_dlg,
|
|
|
|
button_new (BY + chown_but[i].y, BX + chown_but[i].x,
|
|
|
|
chown_but[i].ret_cmd, chown_but[i].flags,
|
2003-09-01 06:07:02 +04:00
|
|
|
_(chown_but[i].text), 0));
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Add the widgets for the file information */
|
2002-09-02 22:03:15 +04:00
|
|
|
for (i = 0; i < LABELS; i++) {
|
|
|
|
chown_label[i].l =
|
2003-09-01 04:18:37 +04:00
|
|
|
label_new (chown_label[i].y, chown_label[i].x, "");
|
2002-09-02 22:03:15 +04:00
|
|
|
add_widget (ch_dlg, chown_label[i].l);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get new listboxes */
|
2003-09-01 11:31:56 +04:00
|
|
|
l_user = listbox_new (UY + 1, UX + 1, 19, 10, NULL);
|
|
|
|
l_group = listbox_new (GY + 1, GX + 1, 19, 10, NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-09-02 22:03:15 +04:00
|
|
|
/* add fields for unknown names (numbers) */
|
|
|
|
listbox_add_item (l_user, 0, 0, _("<Unknown user>"), NULL);
|
1998-03-25 08:16:00 +03:00
|
|
|
listbox_add_item (l_group, 0, 0, _("<Unknown group>"), NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-09-02 22:03:15 +04:00
|
|
|
/* get and put user names in the listbox */
|
|
|
|
setpwent ();
|
1998-02-27 07:54:42 +03:00
|
|
|
while ((l_pass = getpwent ())) {
|
|
|
|
listbox_add_item (l_user, 0, 0, l_pass->pw_name, NULL);
|
|
|
|
}
|
1998-03-19 07:51:17 +03:00
|
|
|
endpwent ();
|
2002-09-02 22:03:15 +04:00
|
|
|
|
|
|
|
/* get and put group names in the listbox */
|
|
|
|
setgrent ();
|
1998-02-27 07:54:42 +03:00
|
|
|
while ((l_grp = getgrent ())) {
|
|
|
|
listbox_add_item (l_group, 0, 0, l_grp->gr_name, NULL);
|
|
|
|
}
|
1998-03-19 07:51:17 +03:00
|
|
|
endgrent ();
|
2002-09-02 22:03:15 +04:00
|
|
|
|
|
|
|
/* add listboxes to the dialogs */
|
1998-02-27 07:54:42 +03:00
|
|
|
add_widget (ch_dlg, l_group);
|
2002-09-02 22:03:15 +04:00
|
|
|
add_widget (ch_dlg, l_user);
|
|
|
|
|
|
|
|
return ch_dlg;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-12-03 00:27:27 +03:00
|
|
|
static void
|
|
|
|
chown_done (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
if (need_update)
|
1998-04-30 07:21:52 +04:00
|
|
|
update_panels (UP_OPTIMIZE, UP_KEEPSEL);
|
1998-02-27 07:54:42 +03:00
|
|
|
repaint_screen ();
|
|
|
|
}
|
|
|
|
|
1998-12-03 00:27:27 +03:00
|
|
|
static inline void
|
|
|
|
do_chown (uid_t u, gid_t g)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
if (mc_chown (cpanel->dir.list [current_file].fname, u, g) == -1)
|
2002-12-08 04:12:18 +03:00
|
|
|
message (1, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
|
1998-02-27 07:54:42 +03:00
|
|
|
cpanel->dir.list [current_file].fname, unix_error_string (errno));
|
|
|
|
|
|
|
|
do_file_mark (cpanel, current_file, 0);
|
|
|
|
}
|
|
|
|
|
1998-12-03 00:27:27 +03:00
|
|
|
static void
|
|
|
|
apply_chowns (uid_t u, gid_t g)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
need_update = end_chown = 1;
|
|
|
|
do_chown (u,g);
|
|
|
|
|
|
|
|
do {
|
|
|
|
fname = next_file ();
|
|
|
|
|
|
|
|
do_chown (u,g);
|
|
|
|
} while (cpanel->marked);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
|
|
|
|
|
1998-12-03 00:27:27 +03:00
|
|
|
void
|
|
|
|
chown_cmd (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
struct stat sf_stat;
|
|
|
|
WLEntry *fe;
|
2002-09-02 22:03:15 +04:00
|
|
|
Dlg_head *ch_dlg;
|
1998-02-27 07:54:42 +03:00
|
|
|
uid_t new_user;
|
|
|
|
gid_t new_group;
|
Glibing..... (2)
Wed Jan 27 03:17:44 1999 Timur Bakeyev <mc@bat.ru>
* Converted memory managment to Glib. Now we use g_new()/g_malloc()/
g_strdup()/g_free() routings. Also, copy_strings() replaced by
g_strconcat(), strcasecmp() -> g_strcasecmp(),and sprintf() by
g_snprintf().
* Some sequences of malloc()/sprintf() changed to g_strdup_printf().
* mad.[ch]: Modified, to work with new GLib's memory managment. Fixed
a missing #undef for tempnam, which caused dead loop. Add several new
functions to emulate GLib memory managment.
*main.c, mad.[ch]: Add a new switch "-M", which allows to redirect MAD
messages to the file.
* util.[ch], utilunix.c: Modified, deleted our variants of strcasecmp()
and strdup() - we have g_ equivalences. Remove get_full_name() - it is
similar to concat_dir_and_file(). Some other tricks with g_* functions.
* global.h: Modified, extended. Now it is main memory mangment include -
i.e. all inclusions of <stdlib.h>, <malloc.h>, <glib.h>, "fs.h", "mem.h",
"util.h" and "mad.h" done there. This elimanates problem with proper or-
der of #include's.
* All around the source - changed order of #include's, most of them gone
to global.h (see above), minor changes, like "0" -> NULL in string func-
tions.
1999-01-27 04:08:30 +03:00
|
|
|
char buffer [BUF_TINY];
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
do { /* do while any files remaining */
|
2002-09-02 22:03:15 +04:00
|
|
|
ch_dlg = init_chown ();
|
1998-02-27 07:54:42 +03:00
|
|
|
new_user = new_group = -1;
|
|
|
|
|
|
|
|
if (cpanel->marked)
|
|
|
|
fname = next_file (); /* next marked file */
|
|
|
|
else
|
|
|
|
fname = selection (cpanel)->fname; /* single file */
|
|
|
|
|
2003-10-24 22:38:02 +04:00
|
|
|
if (mc_stat (fname, &sf_stat) != 0) { /* get status of file */
|
1998-02-27 07:54:42 +03:00
|
|
|
destroy_dlg (ch_dlg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* select in listboxes */
|
|
|
|
fe = listbox_search_text (l_user, get_owner(sf_stat.st_uid));
|
|
|
|
if (fe)
|
|
|
|
listbox_select_entry (l_user, fe);
|
|
|
|
|
|
|
|
fe = listbox_search_text (l_group, get_group(sf_stat.st_gid));
|
|
|
|
if (fe)
|
|
|
|
listbox_select_entry (l_group, fe);
|
|
|
|
|
|
|
|
chown_label (0, name_trunc (fname, 15));
|
|
|
|
chown_label (1, name_trunc (get_owner (sf_stat.st_uid), 15));
|
|
|
|
chown_label (2, name_trunc (get_group (sf_stat.st_gid), 15));
|
2002-02-24 10:17:47 +03:00
|
|
|
size_trunc_len (buffer, 15, sf_stat.st_size, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
chown_label (3, buffer);
|
|
|
|
chown_label (4, string_perm (sf_stat.st_mode));
|
|
|
|
|
|
|
|
run_dlg (ch_dlg);
|
|
|
|
|
|
|
|
switch (ch_dlg->ret_value) {
|
|
|
|
case B_CANCEL:
|
|
|
|
end_chown = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case B_SETUSR:
|
|
|
|
{
|
|
|
|
struct passwd *user;
|
|
|
|
|
|
|
|
user = getpwnam (l_user->current->text);
|
|
|
|
if (user){
|
|
|
|
new_user = user->pw_uid;
|
|
|
|
apply_chowns (new_user, new_group);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case B_SETGRP:
|
|
|
|
{
|
|
|
|
struct group *grp;
|
|
|
|
|
|
|
|
grp = getgrnam (l_group->current->text);
|
|
|
|
if (grp){
|
|
|
|
new_group = grp->gr_gid;
|
|
|
|
apply_chowns (new_user, new_group);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case B_SETALL:
|
|
|
|
case B_ENTER:
|
|
|
|
{
|
|
|
|
struct group *grp;
|
|
|
|
struct passwd *user;
|
|
|
|
|
|
|
|
grp = getgrnam (l_group->current->text);
|
|
|
|
if (grp)
|
|
|
|
new_group = grp->gr_gid;
|
|
|
|
user = getpwnam (l_user->current->text);
|
|
|
|
if (user)
|
|
|
|
new_user = user->pw_uid;
|
|
|
|
if (ch_dlg->ret_value==B_ENTER) {
|
|
|
|
need_update = 1;
|
|
|
|
if (mc_chown (fname, new_user, new_group) == -1)
|
2002-12-08 04:12:18 +03:00
|
|
|
message (1, MSG_ERROR, _(" Cannot chown \"%s\" \n %s "),
|
1998-02-27 07:54:42 +03:00
|
|
|
fname, unix_error_string (errno));
|
|
|
|
} else
|
|
|
|
apply_chowns (new_user, new_group);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpanel->marked && ch_dlg->ret_value != B_CANCEL){
|
|
|
|
do_file_mark (cpanel, current_file, 0);
|
|
|
|
need_update = 1;
|
|
|
|
}
|
|
|
|
destroy_dlg (ch_dlg);
|
|
|
|
} while (cpanel->marked && !end_chown);
|
|
|
|
|
|
|
|
chown_done ();
|
|
|
|
}
|