>From 448e52ab8613a672b50e2a49578a07a44ed10986 Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Tue, 6 Aug 2024 21:53:25 -0700 Subject: [PATCH 5/5] maint: Use userspec from Gnulib. * gnulib.modules: Add userspec. * po/POTFILES.in: Add gnu/userspec.c. Remove src/userspec.c. * src/Makefile.am (cpio_SOURCES): Remove userspec.c. * src/extern.h (parse_user_spec): Remove declaration. * src/main.c: Include userspec.h. * src/userspec.c: Remove file. --- gnulib.modules | 1 + po/POTFILES.in | 2 +- src/Makefile.am | 3 +- src/extern.h | 4 - src/main.c | 1 + src/userspec.c | 243 ------------------------------------------------ 6 files changed, 4 insertions(+), 250 deletions(-) delete mode 100644 src/userspec.c diff --git a/gnulib.modules b/gnulib.modules index ee54d2f..6363887 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -30,6 +30,7 @@ stpcpy strerror timespec unlocked-io +userspec xalloc xalloc-die xgetcwd diff --git a/po/POTFILES.in b/po/POTFILES.in index 51d7618..0cc7a16 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -24,6 +24,7 @@ gnu/getopt.c gnu/obstack.c gnu/openat-die.c gnu/quotearg.c +gnu/userspec.c gnu/version-etc.c gnu/xalloc-die.c @@ -41,7 +42,6 @@ src/main.c src/makepath.c src/mt.c src/tar.c -src/userspec.c src/util.c tests/genfile.c diff --git a/src/Makefile.am b/src/Makefile.am index 5ee0e79..77df2e0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,8 +36,7 @@ cpio_SOURCES = \ main.c\ tar.c\ util.c\ - makepath.c\ - userspec.c + makepath.c noinst_HEADERS =\ cpio.h\ diff --git a/src/extern.h b/src/extern.h index 0b7fb83..6447359 100644 --- a/src/extern.h +++ b/src/extern.h @@ -151,10 +151,6 @@ int otoa (char *s, unsigned long *n); int is_tar_header (char *buf); int is_tar_filename_too_long (char *name); -/* userspec.c */ -const char *parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid, - char **username_arg, char **groupname_arg); - /* util.c */ void tape_empty_output_buffer (int out_des); void disk_empty_output_buffer (int out_des, bool flush); diff --git a/src/main.c b/src/main.c index d2135c8..7c889db 100644 --- a/src/main.c +++ b/src/main.c @@ -36,6 +36,7 @@ #include #include +#include #include "filetypes.h" #include "cpiohdr.h" diff --git a/src/userspec.c b/src/userspec.c deleted file mode 100644 index 43d565c..0000000 --- a/src/userspec.c +++ /dev/null @@ -1,243 +0,0 @@ -/* userspec.c -- Parse a user and group string. - Copyright (C) 1989-2024 Free Software Foundation, Inc. - - 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 3, 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 Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301 USA. */ - -/* Written by David MacKenzie . */ - -#include -#include -#include -#include -#include -#include "cpiohdr.h" -#include "extern.h" - -#ifndef HAVE_ENDPWENT -# define endpwent() -#endif -#ifndef HAVE_ENDGRENT -# define endgrent() -#endif - -/* Perform the equivalent of the statement `dest = strdup (src);', - but obtaining storage via alloca instead of from the heap. */ - -#define V_STRDUP(dest, src) \ - do \ - { \ - int _len = strlen ((src)); \ - (dest) = (char *) alloca (_len + 1); \ - strcpy (dest, src); \ - } \ - while (0) - -/* Return nonzero if STR represents an unsigned decimal integer, - otherwise return 0. */ - -static int -isnumber_p (const char *str) -{ - for (; *str; str++) - if (!isdigit (*str)) - return 0; - return 1; -} - -/* Extract from NAME, which has the form "[user][:.][group]", - a USERNAME, UID U, GROUPNAME, and GID G. - Either user or group, or both, must be present. - If the group is omitted but the ":" or "." separator is given, - use the given user's login group. - - USERNAME and GROUPNAME will be in newly malloc'd memory. - Either one might be NULL instead, indicating that it was not - given and the corresponding numeric ID was left unchanged. - - Return NULL if successful, a static error message string if not. */ - -const char * -parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid, - char **username_arg, char **groupname_arg) -{ - static const char *tired = "virtual memory exhausted"; - const char *error_msg; - char *spec; /* A copy we can write on. */ - struct passwd *pwd; - struct group *grp; - char *g, *u, *separator; - char *groupname; - - error_msg = NULL; - *username_arg = *groupname_arg = NULL; - groupname = NULL; - - V_STRDUP (spec, spec_arg); - - /* Find the separator if there is one. */ - separator = strchr (spec, ':'); - if (separator == NULL) - separator = strchr (spec, '.'); - - /* Replace separator with a NUL. */ - if (separator != NULL) - *separator = '\0'; - - /* Set U and G to non-zero length strings corresponding to user and - group specifiers or to NULL. */ - u = (*spec == '\0' ? NULL : spec); - - g = (separator == NULL || *(separator + 1) == '\0' - ? NULL - : separator + 1); - - if (u == NULL && g == NULL) - return "can not omit both user and group"; - - if (u != NULL) - { - if (*u == '+') - { - pwd = NULL; - ++u; - } - else - pwd = getpwnam (u); - - if (pwd == NULL) - { - if (!isnumber_p (u)) - error_msg = _("invalid user"); - else - { - int use_login_group; - use_login_group = (separator != NULL && g == NULL); - if (use_login_group) - error_msg = _("cannot get the login group of a numeric UID"); - else - *uid = atoi (u); - } - } - else - { - *uid = pwd->pw_uid; - if (g == NULL && separator != NULL) - { - /* A separator was given, but a group was not specified, - so get the login group. */ - *gid = pwd->pw_gid; - grp = getgrgid (pwd->pw_gid); - if (grp == NULL) - { - char nbuf[UINTMAX_STRSIZE_BOUND]; - V_STRDUP (groupname, umaxtostr (pwd->pw_gid, nbuf)); - } - else - { - V_STRDUP (groupname, grp->gr_name); - } - endgrent (); - } - } - endpwent (); - } - - if (g != NULL && error_msg == NULL) - { - /* Explicit group. */ - if (*g == '+') - { - grp = NULL; - ++g; - } - else - grp = getgrnam (g); - - if (grp == NULL) - { - if (!isnumber_p (g)) - error_msg = _("invalid group"); - else - *gid = atoi (g); - } - else - *gid = grp->gr_gid; - endgrent (); /* Save a file descriptor. */ - - if (error_msg == NULL) - V_STRDUP (groupname, g); - } - - if (error_msg == NULL) - { - if (u != NULL) - { - *username_arg = strdup (u); - if (*username_arg == NULL) - error_msg = tired; - } - - if (groupname != NULL && error_msg == NULL) - { - *groupname_arg = strdup (groupname); - if (*groupname_arg == NULL) - { - if (*username_arg != NULL) - { - free (*username_arg); - *username_arg = NULL; - } - error_msg = tired; - } - } - } - - return error_msg; -} - -#ifdef TEST - -#define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s)) - -int -main (int argc, char **argv) -{ - int i; - - for (i = 1; i < argc; i++) - { - const char *e; - char *username, *groupname; - uid_t uid; - gid_t gid; - char *tmp; - - tmp = strdup (argv[i]); - e = parse_user_spec (tmp, &uid, &gid, &username, &groupname); - free (tmp); - printf ("%s: %u %u %s %s %s\n", - argv[i], - (unsigned int) uid, - (unsigned int) gid, - NULL_CHECK (username), - NULL_CHECK (groupname), - NULL_CHECK (e)); - } - - exit (0); -} - -#endif -- 2.45.2