diff options
Diffstat (limited to 'libpwdgrp/pwd_grp.c')
-rw-r--r-- | libpwdgrp/pwd_grp.c | 58 |
1 files changed, 43 insertions, 15 deletions
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, | |||
336 | return errno; | 336 | return errno; |
337 | } | 337 | } |
338 | 338 | ||
339 | static void* massage_data_for_non_r_func(struct passdb *db, char *buf) | ||
340 | { | ||
341 | if (!buf) | ||
342 | return NULL; | ||
343 | |||
344 | free(db->malloced); | ||
345 | /* We enlarge buf and move string data up, freeing space | ||
346 | * for struct passwd/group/spwd at the beginning. This way, | ||
347 | * entire result of getXXnam is in a single malloced block. | ||
348 | * This enables easy creation of xmalloc_getpwnam() API. | ||
349 | */ | ||
350 | db->malloced = buf = xrealloc(buf, db->size_of + S.string_size); | ||
351 | memmove(buf + db->size_of, buf, S.string_size); | ||
352 | return convert_to_struct(db, buf + db->size_of, buf); | ||
353 | } | ||
354 | |||
339 | /****** getXXnam/id_r */ | 355 | /****** getXXnam/id_r */ |
340 | 356 | ||
341 | static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos, | 357 | 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 | |||
372 | } | 388 | } |
373 | #endif | 389 | #endif |
374 | 390 | ||
391 | #ifdef UNUSED | ||
375 | /****** getXXent_r */ | 392 | /****** getXXent_r */ |
376 | 393 | ||
377 | static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen, | 394 | 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, | |||
400 | *result = struct_buf; | 417 | *result = struct_buf; |
401 | return getXXent_r(0, buffer, buflen, result); | 418 | return getXXent_r(0, buffer, buflen, result); |
402 | } | 419 | } |
420 | #endif | ||
421 | |||
422 | /****** getXXent */ | ||
423 | |||
424 | static void* FAST_FUNC getXXent(uintptr_t db_idx) | ||
425 | { | ||
426 | char *buf; | ||
427 | struct passdb *db = &get_S()->db[db_idx]; | ||
428 | |||
429 | if (!db->fp) { | ||
430 | db->fp = fopen_for_read(db->filename); | ||
431 | if (!db->fp) { | ||
432 | return NULL; | ||
433 | } | ||
434 | close_on_exec_on(fileno(db->fp)); | ||
435 | } | ||
436 | |||
437 | buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1); | ||
438 | return massage_data_for_non_r_func(db, buf); | ||
439 | } | ||
440 | |||
441 | struct passwd* FAST_FUNC getpwent(void) | ||
442 | { | ||
443 | return getXXent(0); | ||
444 | } | ||
403 | 445 | ||
404 | /****** getXXnam/id */ | 446 | /****** getXXnam/id */ |
405 | 447 | ||
406 | static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos) | 448 | static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos) |
407 | { | 449 | { |
408 | char *buf; | 450 | char *buf; |
409 | void *result; | ||
410 | struct passdb *db = &get_S()->db[db_and_field_pos >> 2]; | 451 | struct passdb *db = &get_S()->db[db_and_field_pos >> 2]; |
411 | 452 | ||
412 | result = NULL; | ||
413 | |||
414 | if (!db->fp) { | 453 | if (!db->fp) { |
415 | db->fp = fopen_for_read(db->filename); | 454 | db->fp = fopen_for_read(db->filename); |
416 | if (!db->fp) { | 455 | if (!db->fp) { |
@@ -420,18 +459,7 @@ static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos) | |||
420 | } | 459 | } |
421 | 460 | ||
422 | buf = parse_common(db->fp, db, name, db_and_field_pos & 3); | 461 | buf = parse_common(db->fp, db, name, db_and_field_pos & 3); |
423 | if (buf) { | 462 | return massage_data_for_non_r_func(db, buf); |
424 | free(db->malloced); | ||
425 | /* We enlarge buf and move string data up, freeing space | ||
426 | * for struct passwd/group/spwd at the beginning. This way, | ||
427 | * entire result of getXXnam is in a single malloced block. | ||
428 | * This enables easy creation of xmalloc_getpwnam() API. | ||
429 | */ | ||
430 | db->malloced = buf = xrealloc(buf, db->size_of + S.string_size); | ||
431 | memmove(buf + db->size_of, buf, S.string_size); | ||
432 | result = convert_to_struct(db, buf + db->size_of, buf); | ||
433 | } | ||
434 | return result; | ||
435 | } | 463 | } |
436 | 464 | ||
437 | struct passwd* FAST_FUNC getpwnam(const char *name) | 465 | struct passwd* FAST_FUNC getpwnam(const char *name) |