aboutsummaryrefslogtreecommitdiff
path: root/libpwdgrp
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2015-01-04 02:02:39 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2015-01-04 02:02:39 +0100
commit5acf1346b463a41373f5a8c1df424c5f1646a4f9 (patch)
tree738ca95b20188dd15af2e41626db9776b4a45fd9 /libpwdgrp
parent12fc86956057ac3e679dd276ba3897260b95157e (diff)
downloadbusybox-w32-5acf1346b463a41373f5a8c1df424c5f1646a4f9.tar.gz
busybox-w32-5acf1346b463a41373f5a8c1df424c5f1646a4f9.tar.bz2
busybox-w32-5acf1346b463a41373f5a8c1df424c5f1646a4f9.zip
libpwdgrp: code shrink
function old new delta parse_common 206 205 -1 getgrouplist_internal 229 225 -4 bb_internal_getpwent_r 182 173 -9 getXXnam 232 222 -10 getXXnam_r 216 176 -40 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-64) Total: -64 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libpwdgrp')
-rw-r--r--libpwdgrp/pwd_grp.c41
1 files changed, 16 insertions, 25 deletions
diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c
index 65fd5261d..0fd458fdc 100644
--- a/libpwdgrp/pwd_grp.c
+++ b/libpwdgrp/pwd_grp.c
@@ -186,8 +186,7 @@ static int tokenize(char *buffer, int ch)
186/* Returns !NULL on success and matching line broken up in fields by '\0' in buf. 186/* Returns !NULL on success and matching line broken up in fields by '\0' in buf.
187 * We require the expected number of fields to be found. 187 * We require the expected number of fields to be found.
188 */ 188 */
189static char *parse_common(FILE *fp, const char *filename, 189static char *parse_common(FILE *fp, struct passdb *db,
190 int n_fields,
191 const char *key, int field_pos) 190 const char *key, int field_pos)
192{ 191{
193 int count = 0; 192 int count = 0;
@@ -198,9 +197,9 @@ static char *parse_common(FILE *fp, const char *filename,
198 /* Skip empty lines, comment lines */ 197 /* Skip empty lines, comment lines */
199 if (buf[0] == '\0' || buf[0] == '#') 198 if (buf[0] == '\0' || buf[0] == '#')
200 goto free_and_next; 199 goto free_and_next;
201 if (tokenize(buf, ':') != n_fields) { 200 if (tokenize(buf, ':') != db->numfields) {
202 /* number of fields is wrong */ 201 /* number of fields is wrong */
203 bb_error_msg("bad record at %s:%u", filename, count); 202 bb_error_msg("bad record at %s:%u", db->filename, count);
204 goto free_and_next; 203 goto free_and_next;
205 } 204 }
206 205
@@ -223,7 +222,7 @@ static char *parse_common(FILE *fp, const char *filename,
223 * for 3 pointers: alignment padding, group name, NULL. 222 * for 3 pointers: alignment padding, group name, NULL.
224 * +1 for every additional group. 223 * +1 for every additional group.
225 */ 224 */
226 if (buf && n_fields == sizeof(GR_DEF)-1) { /* if we read group file... */ 225 if (buf && db->numfields == sizeof(GR_DEF)-1) { /* if we read group file... */
227 int cnt = 3; 226 int cnt = 3;
228 char *p = buf; 227 char *p = buf;
229 while (p < S.tokenize_end) 228 while (p < S.tokenize_end)
@@ -237,15 +236,14 @@ static char *parse_common(FILE *fp, const char *filename,
237 return buf; 236 return buf;
238} 237}
239 238
240static char *parse_file(const char *filename, 239static char *parse_file(struct passdb *db,
241 int n_fields,
242 const char *key, int field_pos) 240 const char *key, int field_pos)
243{ 241{
244 char *buf = NULL; 242 char *buf = NULL;
245 FILE *fp = fopen_for_read(filename); 243 FILE *fp = fopen_for_read(db->filename);
246 244
247 if (fp) { 245 if (fp) {
248 buf = parse_common(fp, filename, n_fields, key, field_pos); 246 buf = parse_common(fp, db, key, field_pos);
249 fclose(fp); 247 fclose(fp);
250 } 248 }
251 return buf; 249 return buf;
@@ -326,12 +324,10 @@ static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
326{ 324{
327 void *struct_buf = *(void**)result; 325 void *struct_buf = *(void**)result;
328 char *buf; 326 char *buf;
329 struct passdb *db; 327 struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
330 get_S();
331 db = &S.db[db_and_field_pos >> 2];
332 328
333 *(void**)result = NULL; 329 *(void**)result = NULL;
334 buf = parse_file(db->filename, db->numfields, name, 0 /*db_and_field_pos & 3*/); 330 buf = parse_file(db, name, 0 /*db_and_field_pos & 3*/);
335 /* "db_and_field_pos & 3" is commented out since so far we don't implement 331 /* "db_and_field_pos & 3" is commented out since so far we don't implement
336 * getXXXid_r() functions which would use that to pass 2 here */ 332 * getXXXid_r() functions which would use that to pass 2 here */
337 if (buf) { 333 if (buf) {
@@ -378,9 +374,7 @@ static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
378 unsigned db_idx) 374 unsigned db_idx)
379{ 375{
380 char *buf; 376 char *buf;
381 struct passdb *db; 377 struct passdb *db = &get_S()->db[db_idx];
382 get_S();
383 db = &S.db[db_idx];
384 378
385 *(void**)result = NULL; 379 *(void**)result = NULL;
386 380
@@ -392,7 +386,7 @@ static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
392 close_on_exec_on(fileno(db->fp)); 386 close_on_exec_on(fileno(db->fp));
393 } 387 }
394 388
395 buf = parse_common(db->fp, db->filename, db->numfields, /*no search key:*/ NULL, 0); 389 buf = parse_common(db->fp, db, /*no search key:*/ NULL, 0);
396 if (buf) { 390 if (buf) {
397 size_t size = S.tokenize_end - buf; 391 size_t size = S.tokenize_end - buf;
398 if (size > buflen) { 392 if (size > buflen) {
@@ -421,9 +415,7 @@ static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
421{ 415{
422 char *buf; 416 char *buf;
423 void *result; 417 void *result;
424 struct passdb *db; 418 struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
425 get_S();
426 db = &S.db[db_and_field_pos >> 2];
427 419
428 result = NULL; 420 result = NULL;
429 421
@@ -435,7 +427,7 @@ static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
435 close_on_exec_on(fileno(db->fp)); 427 close_on_exec_on(fileno(db->fp));
436 } 428 }
437 429
438 buf = parse_common(db->fp, db->filename, db->numfields, name, db_and_field_pos & 3); 430 buf = parse_common(db->fp, db, name, db_and_field_pos & 3);
439 if (buf) { 431 if (buf) {
440 free(db->malloced); 432 free(db->malloced);
441 /* We enlarge buf and move string data up, freeing space 433 /* We enlarge buf and move string data up, freeing space
@@ -499,8 +491,6 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
499 gid_t *group_list; 491 gid_t *group_list;
500 int ngroups; 492 int ngroups;
501 493
502 get_S();
503
504 /* We alloc space for 8 gids at a time. */ 494 /* We alloc space for 8 gids at a time. */
505 group_list = xzalloc(8 * sizeof(group_list[0])); 495 group_list = xzalloc(8 * sizeof(group_list[0]));
506 group_list[0] = gid; 496 group_list[0] = gid;
@@ -508,11 +498,12 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
508 498
509 fp = fopen_for_read(_PATH_GROUP); 499 fp = fopen_for_read(_PATH_GROUP);
510 if (fp) { 500 if (fp) {
501 struct passdb *db = &get_S()->db[1];
511 char *buf; 502 char *buf;
512 while ((buf = parse_common(fp, _PATH_GROUP, sizeof(GR_DEF)-1, NULL, 0)) != NULL) { 503 while ((buf = parse_common(fp, db, NULL, 0)) != NULL) {
513 char **m; 504 char **m;
514 struct group group; 505 struct group group;
515 if (!convert_to_struct(&S.db[1], buf, &group)) 506 if (!convert_to_struct(db, buf, &group))
516 goto next; 507 goto next;
517 if (group.gr_gid == gid) 508 if (group.gr_gid == gid)
518 goto next; 509 goto next;