From 2f7218c9849e667ba20d76e4dfe46b4bb195063a Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Wed, 19 Dec 2007 10:49:03 +0000 Subject: Make checkown --user --group behave more like chown --- src/checkown.c | 77 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/src/checkown.c b/src/checkown.c index e90c24ac..ecec917c 100644 --- a/src/checkown.c +++ b/src/checkown.c @@ -127,52 +127,54 @@ static int parse_mode (mode_t *mode, char *text) 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 *p = *name; - char *token; - int tid; - - token = strsep (&p, ":"); - if (sscanf (token, "%d", &tid) != 1) - pw = getpwnam (token); - else - pw = getpwuid (tid); - - if (pw) - *name = p; - - return (pw); -} + char *u = xstrdup (owner); + char *g = strchr (u, ':'); + int id = 0; + int retval = 0; + + if (g) + *g++ = '\0'; + + if (user && *u) { + if (sscanf (u, "%d", &id) == 1) + *user = getpwuid (id); + else + *user = getpwnam (u); + if (! *user) + retval = -1; + } -static struct group *get_group (const char *name) -{ - int tid; + if (group && g && *g) { + if (sscanf (g, "%d", &id) == 1) + *group = getgrgid (id); + else + *group = getgrnam (g); + if (! *group) + retval = -1; + } - if (sscanf (name, "%d", &tid) != 1) - return (getgrnam (name)); - else - return (getgrgid (tid)); + free (u); + return (retval); } #include "_usage.h" #define extraopts "dir1 dir2 ..." -#define getoptstring "fm:g:u:" getoptstring_COMMON +#define getoptstring "fm:o:" getoptstring_COMMON static struct option longopts[] = { { "directory", 0, NULL, 'd'}, { "file", 0, NULL, 'f'}, { "mode", 1, NULL, 'm'}, - { "user", 1, NULL, 'u'}, - { "group", 1, NULL, 'g'}, + { "owner", 1, NULL, 'o'}, longopts_COMMON }; static const char * const longopts_help[] = { "Check if a directory", "Check if a file", "Mode to check", - "User to check", - "Group to check", + "Owner to check (user:group)", longopts_help_COMMON }; #include "_usage.c" @@ -187,7 +189,6 @@ int checkown (int argc, char **argv) struct group *gr = NULL; bool file = 0; - char *p; int retval = EXIT_SUCCESS; applet = argv[0]; @@ -203,20 +204,12 @@ int checkown (int argc, char **argv) file = 1; break; case 'm': - if (parse_mode (&mode, optarg)) + if (parse_mode (&mode, optarg) != 0) eerrorx ("%s: invalid mode `%s'", applet, optarg); break; - case 'u': - p = optarg; - if (! (pw = get_user (&p))) - 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); + case 'o': + if (parse_owner (&pw, &gr, optarg) != 0) + eerrorx ("%s: owner `%s' not found", applet, optarg); break; case_RC_COMMON_GETOPT -- cgit v1.2.3