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."
This commit is contained in:
Glenn L McGrath
2004-09-15 03:04:08 +00:00
parent 995d96a99d
commit f15dfc5570
6 changed files with 126 additions and 108 deletions

View File

@ -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 \

View File

@ -22,48 +22,28 @@
/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> 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 <stdio.h>
#include <string.h>
#include <assert.h>
#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');
}

View File

@ -22,49 +22,28 @@
/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> 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 <stdio.h>
#include <string.h>
#include <assert.h>
#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 */

64
libbb/my_getug.c Normal file
View File

@ -0,0 +1,64 @@
/* vi: set sw=4 ts=4: */
/*
* Utility routines.
*
* Copyright (C) 2004 by Tito Ragusa <farmatito@tiscali.it>
*
* 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 <stdio.h>
#include <assert.h>
#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:
*/