Re: [hackers] [PATCH v2 2/2][sbase] chgrp: parse gid if operand is not group name
On 2019-07-03, Richard Ipsum <richardipsum_AT_fastmail.co.uk> wrote:
> ---
> chgrp.c | 22 +++++++++++++++-------
> 1 file changed, 15 insertions(+), 7 deletions(-)
>
> diff --git a/chgrp.c b/chgrp.c
> index 7ee3553..ddced31 100644
> --- a/chgrp.c
> +++ b/chgrp.c
> _AT_@ -44,6 +44,7 @@ int
> main(int argc, char *argv[])
> {
> struct group *gr;
> + char *group;
> struct recursor r = { .fn = chgrp, .hist = NULL, .depth = 0, .maxdepth =
> 1,
> .follow = 'P', .flags = 0 };
>
> _AT_@ -66,14 +67,21 @@ main(int argc, char *argv[])
> if (argc < 2)
> usage();
>
> - errno = 0;
> - if (!(gr = getgrnam(argv[0]))) {
> - if (errno)
> - eprintf("getgrnam %s:", argv[0]);
> - else
> - eprintf("getgrnam %s: no such group\n", argv[0]);
> + group = argv[0];
> + if (group && *group) {
I don't think you need to check if group != NULL here, since we know
argc >= 2 at this point.
Also, do we need to check *group? Is it an error to call getgrnam(3)
with an empty string? I assume it would just fail to find a group.
> + errno = 0;
> + gr = getgrnam(group);
> + if (gr) {
> + gid = gr->gr_gid;
> + } else {
> + if (errno)
> + eprintf("getgrnam %s:", group);
> + gid = estrtonum(group, 0, UINT_MAX);
> + }
> }
> - gid = gr->gr_gid;
> +
> + if (gid == -1)
> + usage();
>
> for (argc--, argv++; *argv; argc--, argv++)
> recurse(*argv, NULL, &r);
> --
> 2.21.0
Received on Thu Jul 04 2019 - 07:44:57 CEST
This archive was generated by hypermail 2.3.0
: Thu Jul 04 2019 - 07:48:26 CEST