diff options
Diffstat (limited to 'libbb/my_getpwuid.c')
-rw-r--r-- | libbb/my_getpwuid.c | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/libbb/my_getpwuid.c b/libbb/my_getpwuid.c index 53f6c77ee..1e8b11a09 100644 --- a/libbb/my_getpwuid.c +++ b/libbb/my_getpwuid.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 *user can not be set to NULL | ||
26 | * on success username is written on static allocated buffer | ||
27 | * on failure uid as string is written to buffer and NULL is returned | ||
28 | * if bufsize is = 0 char *user can be set to NULL | ||
29 | * on success username is returned | ||
30 | * on failure NULL is returned | ||
31 | * if bufsize is < 0 char *user can be set to NULL | ||
32 | * on success username 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" |
@@ -34,10 +49,21 @@ char * my_getpwuid(char *name, long uid, int bufsize) | |||
34 | 49 | ||
35 | myuser = getpwuid(uid); | 50 | myuser = getpwuid(uid); |
36 | if (myuser==NULL) { | 51 | if (myuser==NULL) { |
37 | snprintf(name, bufsize, "%ld", (long)uid); | 52 | if(bufsize > 0) { |
53 | assert(name != NULL); | ||
54 | snprintf(name, bufsize, "%ld", (long)uid); | ||
55 | } | ||
56 | if (bufsize < 0 ) { | ||
57 | bb_error_msg_and_die("unknown uid %ld", (long)uid); | ||
58 | } | ||
38 | return NULL; | 59 | return NULL; |
39 | } else { | 60 | } else { |
40 | return safe_strncpy(name, myuser->pw_name, bufsize); | 61 | if(bufsize > 0 ) |
62 | { | ||
63 | assert(name != NULL); | ||
64 | return safe_strncpy(name, myuser->pw_name, bufsize); | ||
65 | } | ||
66 | return myuser->pw_name; | ||
41 | } | 67 | } |
42 | } | 68 | } |
43 | 69 | ||