aboutsummaryrefslogtreecommitdiff
path: root/libpwdgrp
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2015-01-05 15:09:04 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2015-01-05 15:09:04 +0100
commitc5d4a04e450b961e354e985058bd69a0f7b7bc2d (patch)
treefb9ae0b48db1fc2535e2f5954ae184cad0f997cb /libpwdgrp
parent4bf88d9094fd208672ba48c809cbcc2753a7d726 (diff)
downloadbusybox-w32-c5d4a04e450b961e354e985058bd69a0f7b7bc2d.tar.gz
busybox-w32-c5d4a04e450b961e354e985058bd69a0f7b7bc2d.tar.bz2
busybox-w32-c5d4a04e450b961e354e985058bd69a0f7b7bc2d.zip
libpwdgrp: fixes suggested by Tito, comment tweaks
function old new delta bb_internal_getpwent_r 100 121 +21 parse_common 202 203 +1 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libpwdgrp')
-rw-r--r--libpwdgrp/pwd_grp.c27
1 files changed, 14 insertions, 13 deletions
diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c
index 3b96cc4d6..524acfeab 100644
--- a/libpwdgrp/pwd_grp.c
+++ b/libpwdgrp/pwd_grp.c
@@ -9,13 +9,13 @@
9 * Rewrite of some parts. Main differences are: 9 * Rewrite of some parts. Main differences are:
10 * 10 *
11 * 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically 11 * 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically
12 * allocated and reused by later calls. 12 * allocated.
13 * If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program 13 * If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program
14 * exit using the atexit function to make valgrind happy. 14 * exit using the atexit function to make valgrind happy.
15 * 2) the passwd/group files: 15 * 2) the passwd/group files:
16 * a) must contain the expected number of fields (as per count of field 16 * a) must contain the expected number of fields (as per count of field
17 * delimeters ":") or we will complain with a error message. 17 * delimeters ":") or we will complain with a error message.
18 * b) leading or trailing whitespace in fields is stripped. 18 * b) leading and trailing whitespace in fields is stripped.
19 * c) some fields are not allowed to be empty (e.g. username, uid/gid, 19 * c) some fields are not allowed to be empty (e.g. username, uid/gid,
20 * homedir, shell) and in this case NULL is returned and errno is 20 * homedir, shell) and in this case NULL is returned and errno is
21 * set to EINVAL. This behaviour could be easily changed by 21 * set to EINVAL. This behaviour could be easily changed by
@@ -23,7 +23,7 @@
23 * makes a field mandatory). 23 * makes a field mandatory).
24 * d) the string representing uid/gid must be convertible by strtoXX 24 * d) the string representing uid/gid must be convertible by strtoXX
25 * functions, or errno is set to EINVAL. 25 * functions, or errno is set to EINVAL.
26 * e) leading or trailing whitespace in group member names are stripped. 26 * e) leading and trailing whitespace in group member names is stripped.
27 * 3) the internal function for getgrouplist uses dynamically allocated buffer. 27 * 3) the internal function for getgrouplist uses dynamically allocated buffer.
28 * 4) at the moment only the functions really used by busybox code are 28 * 4) at the moment only the functions really used by busybox code are
29 * implemented, if you need a particular missing function it should be 29 * implemented, if you need a particular missing function it should be
@@ -34,15 +34,15 @@
34 34
35struct const_passdb { 35struct const_passdb {
36 const char *filename; 36 const char *filename;
37 const char def[7 + 2*ENABLE_USE_BB_SHADOW]; 37 char def[7 + 2*ENABLE_USE_BB_SHADOW];
38 const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW]; 38 uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
39 uint8_t numfields; 39 uint8_t numfields;
40 uint8_t size_of; 40 uint8_t size_of;
41}; 41};
42struct passdb { 42struct passdb {
43 const char *filename; 43 const char *filename;
44 const char def[7 + 2*ENABLE_USE_BB_SHADOW]; 44 char def[7 + 2*ENABLE_USE_BB_SHADOW];
45 const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW]; 45 uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
46 uint8_t numfields; 46 uint8_t numfields;
47 uint8_t size_of; 47 uint8_t size_of;
48 FILE *fp; 48 FILE *fp;
@@ -105,7 +105,7 @@ static const struct const_passdb const_sp_db = {
105 105
106/* We avoid having big global data. */ 106/* We avoid having big global data. */
107struct statics { 107struct statics {
108 /* It's ok to use same buffer (db[0].malloced) for getpwuid and getpwnam. 108 /* We use same buffer (db[0].malloced) for getpwuid and getpwnam.
109 * Manpage says: 109 * Manpage says:
110 * "The return value may point to a static area, and may be overwritten 110 * "The return value may point to a static area, and may be overwritten
111 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()." 111 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
@@ -203,7 +203,7 @@ static char *parse_common(FILE *fp, struct passdb *db,
203 goto free_and_next; 203 goto free_and_next;
204 } 204 }
205 205
206 if (!key) { 206 if (field_pos == -1) {
207 /* no key specified: sequential read, return a record */ 207 /* no key specified: sequential read, return a record */
208 break; 208 break;
209 } 209 }
@@ -292,7 +292,6 @@ static void *convert_to_struct(struct passdb *db,
292 ( ((intptr_t)S.tokenize_end + sizeof(members[0])) 292 ( ((intptr_t)S.tokenize_end + sizeof(members[0]))
293 & -(intptr_t)sizeof(members[0]) 293 & -(intptr_t)sizeof(members[0])
294 ); 294 );
295
296 ((struct group *)result)->gr_mem = members; 295 ((struct group *)result)->gr_mem = members;
297 while (--i >= 0) { 296 while (--i >= 0) {
298 if (buffer[0]) { 297 if (buffer[0]) {
@@ -391,7 +390,9 @@ static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen,
391 close_on_exec_on(fileno(db->fp)); 390 close_on_exec_on(fileno(db->fp));
392 } 391 }
393 392
394 buf = parse_common(db->fp, db, /*no search key:*/ NULL, 0); 393 buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
394 if (!buf && !errno)
395 errno = ENOENT;
395 return massage_data_for_r_func(db, buffer, buflen, result, buf); 396 return massage_data_for_r_func(db, buffer, buflen, result, buf);
396} 397}
397 398
@@ -493,7 +494,7 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
493 if (fp) { 494 if (fp) {
494 struct passdb *db = &get_S()->db[1]; 495 struct passdb *db = &get_S()->db[1];
495 char *buf; 496 char *buf;
496 while ((buf = parse_common(fp, db, NULL, 0)) != NULL) { 497 while ((buf = parse_common(fp, db, NULL, -1)) != NULL) {
497 char **m; 498 char **m;
498 struct group group; 499 struct group group;
499 if (!convert_to_struct(db, buf, &group)) 500 if (!convert_to_struct(db, buf, &group))
@@ -505,7 +506,7 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
505 continue; 506 continue;
506 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups); 507 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
507 group_list[ngroups++] = group.gr_gid; 508 group_list[ngroups++] = group.gr_gid;
508 break; 509 goto next;
509 } 510 }
510 next: 511 next:
511 free(buf); 512 free(buf);