summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-07-27 11:20:10 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-07-27 11:20:10 +0000
commit3734b946bfef55c8f63d367422da5c7aa7b972db (patch)
tree1bdd3e14523002dc8276d167b17f900e40fa6dff /libbb
parent661f6fad77b672a5f6648b01275eb9ff19c59139 (diff)
downloadbusybox-w32-3734b946bfef55c8f63d367422da5c7aa7b972db.tar.gz
busybox-w32-3734b946bfef55c8f63d367422da5c7aa7b972db.tar.bz2
busybox-w32-3734b946bfef55c8f63d367422da5c7aa7b972db.zip
bb_getpwuid, bb_getgrgid: change order of arguments to more intuitive one;
comment thoroughly when they die and when they dont.
Diffstat (limited to 'libbb')
-rw-r--r--libbb/bb_pwd.c90
-rw-r--r--libbb/procps.c5
2 files changed, 33 insertions, 62 deletions
diff --git a/libbb/bb_pwd.c b/libbb/bb_pwd.c
index 3af1994d5..2bdb66230 100644
--- a/libbb/bb_pwd.c
+++ b/libbb/bb_pwd.c
@@ -11,22 +11,20 @@
11 11
12#define assert(x) ((void)0) 12#define assert(x) ((void)0)
13 13
14/*
15 * if bufsize is > 0 char *buffer cannot be set to NULL.
16 * If idname is not NULL it is written on the static
17 * allocated buffer (and a pointer to it is returned).
18 * if idname is NULL, id as string is written to the static
19 * allocated buffer and NULL is returned.
20 * if bufsize is = 0 char *buffer can be set to NULL.
21 * If idname exists a pointer to it is returned,
22 * else NULL is returned.
23 * if bufsize is < 0 char *buffer can be set to NULL.
24 * If idname exists a pointer to it is returned,
25 * else an error message is printed and the program exits.
26 */
27
28/* internal function for bb_getpwuid and bb_getgrgid */ 14/* internal function for bb_getpwuid and bb_getgrgid */
29static char* bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix) 15/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
16 * flexible:
17 *
18 * bufsize > 0: If idname is not NULL it is copied to buffer,
19 * and buffer is returned. Else id as string is written
20 * to buffer, and NULL is returned.
21 *
22 * bufsize == 0: idname is returned.
23 *
24 * bufsize < 0: If idname is not NULL it is returned.
25 * Else an error message is printed and the program exits.
26 */
27static char* bb_getug(char *buffer, int bufsize, char *idname, long id, char prefix)
30{ 28{
31 if (bufsize > 0) { 29 if (bufsize > 0) {
32 assert(buffer != NULL); 30 assert(buffer != NULL);
@@ -40,31 +38,29 @@ static char* bb_getug(char *buffer, char *idname, long id, int bufsize, char pre
40 return idname; 38 return idname;
41} 39}
42 40
43/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more 41/* bb_getpwuid, bb_getgrgid:
44 * flexible : 42 * bb_getXXXid(buf, bufsz, id) - copy user/group name or id
45 * 43 * as a string to buf, return user/group name or NULL
46 * if bufsize is > 0 char *group cannot be set to NULL. 44 * bb_getXXXid(NULL, 0, id) - return user/group name or NULL
47 * On success groupname is written on static allocated buffer 45 * bb_getXXXid(NULL, -1, id) - return user/group name or exit
48 * group (and a pointer to it is returned).
49 * On failure gid as string is written to static allocated
50 * buffer group and NULL is returned.
51 * if bufsize is = 0 char *group can be set to NULL.
52 * On success groupname is returned.
53 * On failure NULL is returned.
54 * if bufsize is < 0 char *group can be set to NULL.
55 * On success groupname is returned.
56 * On failure an error message is printed and
57 * the program exits.
58 */ 46 */
47/* gets a username given a uid */
48char* bb_getpwuid(char *name, int bufsize, long uid)
49{
50 struct passwd *myuser = getpwuid(uid);
59 51
52 return bb_getug(name, bufsize,
53 (myuser ? myuser->pw_name : (char*)myuser),
54 uid, 'u');
55}
60/* gets a groupname given a gid */ 56/* gets a groupname given a gid */
61char* bb_getgrgid(char *group, long gid, int bufsize) 57char* bb_getgrgid(char *group, int bufsize, long gid)
62{ 58{
63 struct group *mygroup = getgrgid(gid); 59 struct group *mygroup = getgrgid(gid);
64 60
65 return bb_getug(group, 61 return bb_getug(group, bufsize,
66 mygroup ? mygroup->gr_name : (char *)mygroup, 62 (mygroup ? mygroup->gr_name : (char*)mygroup),
67 gid, bufsize, 'g'); 63 gid, 'g');
68} 64}
69 65
70/* returns a gid given a group name */ 66/* returns a gid given a group name */
@@ -91,32 +87,6 @@ long xuname2uid(const char *name)
91 return myuser->pw_uid; 87 return myuser->pw_uid;
92} 88}
93 89
94/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
95 * flexible :
96 *
97 * if bufsize is > 0 char *name cannot be set to NULL.
98 * On success username is written on the static allocated
99 * buffer name (and a pointer to it is returned).
100 * On failure uid as string is written to the static
101 * allocated buffer name and NULL is returned.
102 * if bufsize is = 0 char *name can be set to NULL.
103 * On success username is returned.
104 * On failure NULL is returned.
105 * if bufsize is < 0 char *name can be set to NULL
106 * On success username is returned.
107 * On failure an error message is printed and
108 * the program exits.
109 */
110
111/* gets a username given a uid */
112char* bb_getpwuid(char *name, long uid, int bufsize)
113{
114 struct passwd *myuser = getpwuid(uid);
115
116 return bb_getug(name, myuser ? myuser->pw_name : (char *)myuser,
117 uid, bufsize, 'u');
118}
119
120unsigned long get_ug_id(const char *s, 90unsigned long get_ug_id(const char *s,
121 long (*xname2id)(const char *)) 91 long (*xname2id)(const char *))
122{ 92{
diff --git a/libbb/procps.c b/libbb/procps.c
index 37593700a..aa207af6f 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -52,7 +52,7 @@ static int get_cached(cache_t *cp, unsigned id)
52} 52}
53#endif 53#endif
54 54
55typedef char* ug_func(char *name, long uid, int bufsize); 55typedef char* ug_func(char *name, int bufsize, long uid);
56static char* get_cached(cache_t *cp, unsigned id, ug_func* fp) 56static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
57{ 57{
58 int i; 58 int i;
@@ -62,7 +62,8 @@ static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
62 i = cp->size++; 62 i = cp->size++;
63 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache)); 63 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
64 cp->cache[i].id = id; 64 cp->cache[i].id = id;
65 fp(cp->cache[i].name, id, sizeof(cp->cache[i].name)); 65 /* Never fails. Generates numeric string if name isn't found */
66 fp(cp->cache[i].name, sizeof(cp->cache[i].name), id);
66 return cp->cache[i].name; 67 return cp->cache[i].name;
67} 68}
68const char* get_cached_username(uid_t uid) 69const char* get_cached_username(uid_t uid)