From f15dfc557048ca28c8b71ecbcfb9b8f229f2e2e0 Mon Sep 17 00:00:00 2001 From: Glenn L McGrath Date: Wed, 15 Sep 2004 03:04:08 +0000 Subject: Tito writes, "This patch fixes all the bugs in id previously spotted by vodz and me. The binary size increased a bit, but now it should work as expected." --- libbb/Makefile.in | 2 +- libbb/my_getgrgid.c | 46 +++++++++++--------------------------- libbb/my_getpwuid.c | 45 ++++++++++--------------------------- libbb/my_getug.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 67 deletions(-) create mode 100644 libbb/my_getug.c (limited to 'libbb') diff --git a/libbb/Makefile.in b/libbb/Makefile.in index 26ed5b132..f86664f15 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in @@ -34,7 +34,7 @@ LIBBB_SRC:= \ human_readable.c inet_common.c inode_hash.c interface.c isdirectory.c \ kernel_version.c last_char_is.c llist_add_to.c login.c loop.c \ make_directory.c mode_string.c module_syscalls.c mtab.c mtab_file.c \ - my_getgrgid.c my_getgrnam.c my_getpwnam.c \ + my_getgrgid.c my_getgrnam.c my_getpwnam.c my_getug.c\ my_getpwuid.c obscure.c parse_mode.c parse_number.c perror_msg.c \ perror_msg_and_die.c print_file.c get_console.c \ process_escape_sequence.c procps.c pwd2spwd.c pw_encrypt.c qmodule.c \ diff --git a/libbb/my_getgrgid.c b/libbb/my_getgrgid.c index 8c530964c..9ac14a34e 100644 --- a/libbb/my_getgrgid.c +++ b/libbb/my_getgrgid.c @@ -22,48 +22,28 @@ /* Hacked by Tito Ragusa (c) 2004 to make it more * flexible : * - * if bufsize is > 0 char *group cannot be set to NULL - * on success groupname is written on static allocated buffer - * on failure gid as string is written to buffer and NULL is returned - * if bufsize is = 0 char *group can be set to NULL - * on success groupname is returned - * on failure NULL is returned - * if bufsize is < 0 char *group can be set to NULL - * on success groupname is returned - * on failure an error message is printed and the program exits + * if bufsize is > 0 char *group cannot be set to NULL. + * On success groupname is written on static allocated buffer group + * (and a pointer to it is returned). + * On failure gid as string is written to static allocated buffer + * group and NULL is returned. + * if bufsize is = 0 char *group can be set to NULL. + * On success groupname is returned. + * On failure NULL is returned. + * if bufsize is < 0 char *group can be set to NULL. + * On success groupname is returned. + * On failure an error message is printed and the program exits. */ -#include -#include -#include #include "libbb.h" -#include "pwd_.h" #include "grp_.h" - /* gets a groupname given a gid */ char * my_getgrgid(char *group, long gid, int bufsize) { - struct group *mygroup; + struct group *mygroup = getgrgid(gid); - mygroup = getgrgid(gid); - if (mygroup==NULL) { - if(bufsize > 0) { - assert(group != NULL); - snprintf(group, bufsize, "%ld", (long)gid); - } - if( bufsize < 0 ) { - bb_error_msg_and_die("unknown gid %ld", (long)gid); - } - return NULL; - } else { - if(bufsize > 0) - { - assert(group != NULL); - return safe_strncpy(group, mygroup->gr_name, bufsize); - } - return mygroup->gr_name; - } + return my_getug(group, (mygroup) ? mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g'); } diff --git a/libbb/my_getpwuid.c b/libbb/my_getpwuid.c index 1e8b11a09..3195a2182 100644 --- a/libbb/my_getpwuid.c +++ b/libbb/my_getpwuid.c @@ -22,49 +22,28 @@ /* Hacked by Tito Ragusa (c) 2004 to make it more * flexible : * - * if bufsize is > 0 char *user can not be set to NULL - * on success username is written on static allocated buffer - * on failure uid as string is written to buffer and NULL is returned - * if bufsize is = 0 char *user can be set to NULL - * on success username is returned - * on failure NULL is returned + * if bufsize is > 0 char *user can not be set to NULL. + * On success username is written on static allocated buffer name + * (and a pointer to it is returned). + * On failure uid as string is written to static allocated buffer name + * and NULL is returned. + * if bufsize is = 0 char *user can be set to NULL. + * On success username is returned. + * On failure NULL is returned. * if bufsize is < 0 char *user can be set to NULL - * on success username is returned - * on failure an error message is printed and the program exits + * On success username is returned. + * On failure an error message is printed and the program exits. */ -#include -#include -#include #include "libbb.h" #include "pwd_.h" -#include "grp_.h" - - /* gets a username given a uid */ char * my_getpwuid(char *name, long uid, int bufsize) { - struct passwd *myuser; + struct passwd *myuser = getpwuid(uid); - myuser = getpwuid(uid); - if (myuser==NULL) { - if(bufsize > 0) { - assert(name != NULL); - snprintf(name, bufsize, "%ld", (long)uid); - } - if (bufsize < 0 ) { - bb_error_msg_and_die("unknown uid %ld", (long)uid); - } - return NULL; - } else { - if(bufsize > 0 ) - { - assert(name != NULL); - return safe_strncpy(name, myuser->pw_name, bufsize); - } - return myuser->pw_name; - } + return my_getug(name, (myuser) ? myuser->pw_name : (char *)myuser , uid, bufsize, 'u'); } /* END CODE */ diff --git a/libbb/my_getug.c b/libbb/my_getug.c new file mode 100644 index 000000000..894dc5fdd --- /dev/null +++ b/libbb/my_getug.c @@ -0,0 +1,64 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 2004 by Tito Ragusa + * + * 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 2 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + /* + * + * if bufsize is > 0 char *idname can not be set to NULL. + * On success idname is written on static allocated buffer + * (and a pointer to it is returned). + * On failure uid or gid as string is written to static allocated buffer + * and NULL is returned. + * if bufsize is = 0 char *idname can be set to NULL. + * On success idname is returned. + * On failure NULL is returned. + * if bufsize is < 0 char *idname can be set to NULL. + * On success idname is returned. + * On failure an error message is printed and the program exits. + */ + +#include +#include +#include "libbb.h" + + +/* internal function for my_getpwuid and my_getgrgid */ +char * my_getug(char *buffer, char *idname, long id, int bufsize, char prefix) +{ + if(bufsize > 0 ) { + assert(buffer!=NULL); + if(idname) { + return safe_strncpy(buffer, idname, bufsize); + } + snprintf(buffer, bufsize, "%ld", id); + } else if(bufsize < 0 && !idname) { + bb_error_msg_and_die("unknown %cid %ld", prefix, id); + } + return idname; +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ -- cgit v1.2.3