Make checkown --user --group behave more like chown

This commit is contained in:
Roy Marples 2007-12-19 10:49:03 +00:00
parent 47887ac7c3
commit 2f7218c984

View File

@ -127,52 +127,54 @@ static int parse_mode (mode_t *mode, char *text)
return (-1); return (-1);
} }
static struct passwd *get_user (char **name) static int parse_owner (struct passwd **user, struct group **group,
const char *owner)
{ {
struct passwd *pw; char *u = xstrdup (owner);
char *p = *name; char *g = strchr (u, ':');
char *token; int id = 0;
int tid; int retval = 0;
token = strsep (&p, ":"); if (g)
if (sscanf (token, "%d", &tid) != 1) *g++ = '\0';
pw = getpwnam (token);
if (user && *u) {
if (sscanf (u, "%d", &id) == 1)
*user = getpwuid (id);
else else
pw = getpwuid (tid); *user = getpwnam (u);
if (! *user)
if (pw) retval = -1;
*name = p;
return (pw);
} }
static struct group *get_group (const char *name) if (group && g && *g) {
{ if (sscanf (g, "%d", &id) == 1)
int tid; *group = getgrgid (id);
if (sscanf (name, "%d", &tid) != 1)
return (getgrnam (name));
else else
return (getgrgid (tid)); *group = getgrnam (g);
if (! *group)
retval = -1;
}
free (u);
return (retval);
} }
#include "_usage.h" #include "_usage.h"
#define extraopts "dir1 dir2 ..." #define extraopts "dir1 dir2 ..."
#define getoptstring "fm:g:u:" getoptstring_COMMON #define getoptstring "fm:o:" getoptstring_COMMON
static struct option longopts[] = { static struct option longopts[] = {
{ "directory", 0, NULL, 'd'}, { "directory", 0, NULL, 'd'},
{ "file", 0, NULL, 'f'}, { "file", 0, NULL, 'f'},
{ "mode", 1, NULL, 'm'}, { "mode", 1, NULL, 'm'},
{ "user", 1, NULL, 'u'}, { "owner", 1, NULL, 'o'},
{ "group", 1, NULL, 'g'},
longopts_COMMON longopts_COMMON
}; };
static const char * const longopts_help[] = { static const char * const longopts_help[] = {
"Check if a directory", "Check if a directory",
"Check if a file", "Check if a file",
"Mode to check", "Mode to check",
"User to check", "Owner to check (user:group)",
"Group to check",
longopts_help_COMMON longopts_help_COMMON
}; };
#include "_usage.c" #include "_usage.c"
@ -187,7 +189,6 @@ int checkown (int argc, char **argv)
struct group *gr = NULL; struct group *gr = NULL;
bool file = 0; bool file = 0;
char *p;
int retval = EXIT_SUCCESS; int retval = EXIT_SUCCESS;
applet = argv[0]; applet = argv[0];
@ -203,20 +204,12 @@ int checkown (int argc, char **argv)
file = 1; file = 1;
break; break;
case 'm': case 'm':
if (parse_mode (&mode, optarg)) if (parse_mode (&mode, optarg) != 0)
eerrorx ("%s: invalid mode `%s'", applet, optarg); eerrorx ("%s: invalid mode `%s'", applet, optarg);
break; break;
case 'u': case 'o':
p = optarg; if (parse_owner (&pw, &gr, optarg) != 0)
if (! (pw = get_user (&p))) eerrorx ("%s: owner `%s' not found", applet, optarg);
eerrorx ("%s: user `%s' not found", applet, optarg);
if (p && *p)
optarg = p;
else
break;
case 'g':
if (! (gr = get_group (optarg)))
eerrorx ("%s: group `%s' not found", applet, optarg);
break; break;
case_RC_COMMON_GETOPT case_RC_COMMON_GETOPT