diff options
Diffstat (limited to 'libbb/my_getgrgid.c')
-rw-r--r-- | libbb/my_getgrgid.c | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/libbb/my_getgrgid.c b/libbb/my_getgrgid.c index e6b877687..8c530964c 100644 --- a/libbb/my_getgrgid.c +++ b/libbb/my_getgrgid.c | |||
@@ -19,8 +19,23 @@ | |||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | */ | 20 | */ |
21 | 21 | ||
22 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more | ||
23 | * flexible : | ||
24 | * | ||
25 | * if bufsize is > 0 char *group cannot be set to NULL | ||
26 | * on success groupname is written on static allocated buffer | ||
27 | * on failure gid as string is written to buffer and NULL is returned | ||
28 | * if bufsize is = 0 char *group can be set to NULL | ||
29 | * on success groupname is returned | ||
30 | * on failure NULL is returned | ||
31 | * if bufsize is < 0 char *group can be set to NULL | ||
32 | * on success groupname is returned | ||
33 | * on failure an error message is printed and the program exits | ||
34 | */ | ||
35 | |||
22 | #include <stdio.h> | 36 | #include <stdio.h> |
23 | #include <string.h> | 37 | #include <string.h> |
38 | #include <assert.h> | ||
24 | #include "libbb.h" | 39 | #include "libbb.h" |
25 | #include "pwd_.h" | 40 | #include "pwd_.h" |
26 | #include "grp_.h" | 41 | #include "grp_.h" |
@@ -33,10 +48,21 @@ char * my_getgrgid(char *group, long gid, int bufsize) | |||
33 | 48 | ||
34 | mygroup = getgrgid(gid); | 49 | mygroup = getgrgid(gid); |
35 | if (mygroup==NULL) { | 50 | if (mygroup==NULL) { |
36 | snprintf(group, bufsize, "%ld", gid); | 51 | if(bufsize > 0) { |
52 | assert(group != NULL); | ||
53 | snprintf(group, bufsize, "%ld", (long)gid); | ||
54 | } | ||
55 | if( bufsize < 0 ) { | ||
56 | bb_error_msg_and_die("unknown gid %ld", (long)gid); | ||
57 | } | ||
37 | return NULL; | 58 | return NULL; |
38 | } else { | 59 | } else { |
39 | return safe_strncpy(group, mygroup->gr_name, bufsize); | 60 | if(bufsize > 0) |
61 | { | ||
62 | assert(group != NULL); | ||
63 | return safe_strncpy(group, mygroup->gr_name, bufsize); | ||
64 | } | ||
65 | return mygroup->gr_name; | ||
40 | } | 66 | } |
41 | } | 67 | } |
42 | 68 | ||