From 23cfaab47de7392c1ba7d601a05fb36da3629b28 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 7 Feb 2015 21:21:02 +0100 Subject: libpwdgrp: use getpwent() instead of getpwent_r() function old new delta massage_data_for_non_r_func - 90 +90 bb_internal_getpwent - 69 +69 getXXnam_r 94 162 +68 fill_bounds 131 128 -3 deluser_main 355 310 -45 complete_username 123 78 -45 getXXnam 163 90 -73 massage_data_for_r_func 103 - -103 bb_internal_getpwent_r 121 - -121 ------------------------------------------------------------------------------ (add/remove: 2/2 grow/shrink: 1/5 up/down: 227/-407) Total: -163 bytes Signed-off-by: Denys Vlasenko --- libpwdgrp/pwd_grp.c | 58 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 15 deletions(-) (limited to 'libpwdgrp') diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c index 90647e9d3..7ec704ee4 100644 --- a/libpwdgrp/pwd_grp.c +++ b/libpwdgrp/pwd_grp.c @@ -336,6 +336,22 @@ static int massage_data_for_r_func(struct passdb *db, return errno; } +static void* massage_data_for_non_r_func(struct passdb *db, char *buf) +{ + if (!buf) + return NULL; + + free(db->malloced); + /* We enlarge buf and move string data up, freeing space + * for struct passwd/group/spwd at the beginning. This way, + * entire result of getXXnam is in a single malloced block. + * This enables easy creation of xmalloc_getpwnam() API. + */ + db->malloced = buf = xrealloc(buf, db->size_of + S.string_size); + memmove(buf + db->size_of, buf, S.string_size); + return convert_to_struct(db, buf + db->size_of, buf); +} + /****** getXXnam/id_r */ static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos, @@ -372,6 +388,7 @@ int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer } #endif +#ifdef UNUSED /****** getXXent_r */ static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen, @@ -400,17 +417,39 @@ int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, *result = struct_buf; return getXXent_r(0, buffer, buflen, result); } +#endif + +/****** getXXent */ + +static void* FAST_FUNC getXXent(uintptr_t db_idx) +{ + char *buf; + struct passdb *db = &get_S()->db[db_idx]; + + if (!db->fp) { + db->fp = fopen_for_read(db->filename); + if (!db->fp) { + return NULL; + } + close_on_exec_on(fileno(db->fp)); + } + + buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1); + return massage_data_for_non_r_func(db, buf); +} + +struct passwd* FAST_FUNC getpwent(void) +{ + return getXXent(0); +} /****** getXXnam/id */ static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos) { char *buf; - void *result; struct passdb *db = &get_S()->db[db_and_field_pos >> 2]; - result = NULL; - if (!db->fp) { db->fp = fopen_for_read(db->filename); if (!db->fp) { @@ -420,18 +459,7 @@ static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos) } buf = parse_common(db->fp, db, name, db_and_field_pos & 3); - if (buf) { - free(db->malloced); - /* We enlarge buf and move string data up, freeing space - * for struct passwd/group/spwd at the beginning. This way, - * entire result of getXXnam is in a single malloced block. - * This enables easy creation of xmalloc_getpwnam() API. - */ - db->malloced = buf = xrealloc(buf, db->size_of + S.string_size); - memmove(buf + db->size_of, buf, S.string_size); - result = convert_to_struct(db, buf + db->size_of, buf); - } - return result; + return massage_data_for_non_r_func(db, buf); } struct passwd* FAST_FUNC getpwnam(const char *name) -- cgit v1.2.3