logo

utils-std

Collection of commonly available Unix tools
commit: 46143b945adadd876cb050b34d3c5e201492b004
parent 2a2221d859e0280975b5d5d0c8fd4bee87ea1feb
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed,  1 May 2024 01:40:17 +0200

cmd/chown: move {user,group}_parse into lib/user_group_parse.c

Diffstat:

MMakefile4++--
Mcmd/chown.c77+++++------------------------------------------------------------------------
Alib/user_group_parse.c81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alib/user_group_parse.h12++++++++++++
Mmakeless.sh2+-
5 files changed, 101 insertions(+), 75 deletions(-)

diff --git a/Makefile b/Makefile @@ -124,9 +124,9 @@ cmd/tr: cmd/tr.c lib/tr_str.c lib/tr_str.h Makefile rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} $(CC) -std=c99 $(CFLAGS) -o $@ cmd/tr.c lib/tr_str.c $(LDFLAGS) $(LDSTATIC) -cmd/chown: cmd/chown.c lib/path.c lib/path.h Makefile +cmd/chown: cmd/chown.c lib/path.c lib/path.h lib/user_group_parse.c lib/user_group_parse.h Makefile rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} - $(CC) -std=c99 $(CFLAGS) -o $@ cmd/chown.c lib/path.c $(LDFLAGS) $(LDSTATIC) + $(CC) -std=c99 $(CFLAGS) -o $@ cmd/chown.c lib/path.c lib/user_group_parse.c $(LDFLAGS) $(LDSTATIC) cmd/realpath: cmd/realpath.c lib/path.c lib/path.h Makefile rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} diff --git a/cmd/chown.c b/cmd/chown.c @@ -10,14 +10,13 @@ #endif #include "../lib/path.h" +#include "../lib/user_group_parse.h" #include <assert.h> #include <dirent.h> // fdopendir, readdir, closedir #include <errno.h> #include <fcntl.h> // AT_FDCWD, fchownat -#include <grp.h> // getgrnam #include <limits.h> // PATH_MAX -#include <pwd.h> // getpwnam #include <stdbool.h> #include <stdio.h> // fprintf #include <stdlib.h> // abort, exit @@ -25,7 +24,7 @@ #include <sys/stat.h> // fstatat, S_ISDIR #include <unistd.h> // getopt -static char *argv0 = NULL; +char *argv0 = NULL; static uid_t user = (uid_t)-1; static uid_t group = (uid_t)-1; @@ -41,72 +40,6 @@ enum chown_follow_symlinks }; static int -parse_user(char *str) -{ - if(str == NULL) return -1; - if(str[0] == 0) return 0; - - assert(errno == 0); - char *endptr = NULL; - unsigned int id = strtoul(str, &endptr, 0); - if(errno == 0) - { - user = id; - return 0; - } - - errno = 0; - - struct passwd *pw = getpwnam(str); - if(pw == NULL) - { - char *e = strerror(errno); - if(errno == 0) e = "Entry Not Found"; - - fprintf(stderr, "%s: Error: Failed to get entry for username '%s': %s\n", argv0, str, e); - - return -1; - } - - user = pw->pw_uid; - - return 0; -} - -static int -parse_group(char *str) -{ - if(str == NULL) return -1; - if(str[0] == 0) return 0; - - assert(errno == 0); - char *endptr = NULL; - unsigned int id = strtoul(str, &endptr, 0); - if(errno == 0) - { - group = id; - return 0; - } - - errno = 0; - - struct group *gr = getgrnam(str); - if(gr == NULL) - { - char *e = strerror(errno); - if(errno == 0) e = "Entry Not Found"; - - fprintf(stderr, "%s: Error: Failed to get entry for group '%s': %s\n", argv0, str, e); - - return -1; - } - - group = gr->gr_gid; - - return 0; -} - -static int do_fchownat(int fd, char *name, char *acc_path, enum chown_follow_symlinks follow_symlinks) { struct stat stats; @@ -323,13 +256,13 @@ main(int argc, char *argv[]) gname[0] = 0; gname++; - if(parse_group(gname) < 0) return 1; + if(parse_group(gname, &group) < 0) return 1; } - if(parse_user(argv[0]) < 0) return 1; + if(parse_user(argv[0], &user) < 0) return 1; } else if(strcmp(argv0, "chgrp") == 0) { - if(parse_group(argv[0]) < 0) return 1; + if(parse_group(argv[0], &group) < 0) return 1; } else { diff --git a/lib/user_group_parse.c b/lib/user_group_parse.c @@ -0,0 +1,81 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: MPL-2.0 + +#define _POSIX_C_SOURCE 200809L + +#include "./user_group_parse.h" + +#include <assert.h> +#include <errno.h> +#include <grp.h> // getgrnam +#include <pwd.h> // getpwnam +#include <stdio.h> // fprintf +#include <stdlib.h> // strtoul +#include <string.h> // strerror + +int +parse_user(char *str, uid_t *user) +{ + if(str == NULL) return -1; + if(str[0] == 0) return 0; + + assert(errno == 0); + char *endptr = NULL; + unsigned int id = strtoul(str, &endptr, 0); + if(errno == 0) + { + *user = id; + return 0; + } + + errno = 0; + + struct passwd *pw = getpwnam(str); + if(pw == NULL) + { + char *e = strerror(errno); + if(errno == 0) e = "Entry Not Found"; + + fprintf(stderr, "%s: Error: Failed to get entry for username '%s': %s\n", argv0, str, e); + + return -1; + } + + *user = pw->pw_uid; + + return 0; +} + +int +parse_group(char *str, gid_t *group) +{ + if(str == NULL) return -1; + if(str[0] == 0) return 0; + + assert(errno == 0); + char *endptr = NULL; + unsigned int id = strtoul(str, &endptr, 0); + if(errno == 0) + { + *group = id; + return 0; + } + + errno = 0; + + struct group *gr = getgrnam(str); + if(gr == NULL) + { + char *e = strerror(errno); + if(errno == 0) e = "Entry Not Found"; + + fprintf(stderr, "%s: Error: Failed to get entry for group '%s': %s\n", argv0, str, e); + + return -1; + } + + *group = gr->gr_gid; + + return 0; +} diff --git a/lib/user_group_parse.h b/lib/user_group_parse.h @@ -0,0 +1,12 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: MPL-2.0 + +#define _POSIX_C_SOURCE 200809L + +#include <sys/types.h> // uid_t, gid_t + +extern char *argv0; + +int parse_user(char *str, uid_t *user); +int parse_group(char *str, gid_t *group); diff --git a/makeless.sh b/makeless.sh @@ -15,7 +15,7 @@ $CC -std=c99 $CFLAGS -o cmd/base64 cmd/base64.c $LDFLAGS $LDSTATIC $CC -std=c99 $CFLAGS -o cmd/basename cmd/basename.c $LDFLAGS $LDSTATIC $CC -std=c99 $CFLAGS -o cmd/cat cmd/cat.c $LDFLAGS $LDSTATIC $CC -std=c99 $CFLAGS -o cmd/chmod cmd/chmod.c lib/mode.c lib/symbolize_mode.c $LDFLAGS $LDSTATIC -$CC -std=c99 $CFLAGS -o cmd/chown cmd/chown.c lib/path.c $LDFLAGS $LDSTATIC +$CC -std=c99 $CFLAGS -o cmd/chown cmd/chown.c lib/path.c lib/user_group_parse.c $LDFLAGS $LDSTATIC $CC -std=c99 $CFLAGS -o cmd/chroot cmd/chroot.c $LDFLAGS $LDSTATIC $CC -std=c99 $CFLAGS -o cmd/date cmd/date.c lib/iso_parse.c $LDFLAGS $LDSTATIC $CC -std=c99 $CFLAGS -o cmd/df cmd/df.c lib/humanize.c $LDFLAGS $LDSTATIC