diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2015-01-04 02:34:52 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-01-04 02:34:52 +0100 |
commit | db4d1051ca5e9b4cc1fd2d1872194874f09e3b35 (patch) | |
tree | c6dd02d222f64d8c18082ec239659ca633377988 /libpwdgrp | |
parent | 5acf1346b463a41373f5a8c1df424c5f1646a4f9 (diff) | |
download | busybox-w32-db4d1051ca5e9b4cc1fd2d1872194874f09e3b35.tar.gz busybox-w32-db4d1051ca5e9b4cc1fd2d1872194874f09e3b35.tar.bz2 busybox-w32-db4d1051ca5e9b4cc1fd2d1872194874f09e3b35.zip |
libpwdgrp: another code shrink
function old new delta
massage_data_for_r_func - 110 +110
bb_internal_getpwent_r 173 100 -73
getXXnam_r 176 95 -81
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/2 up/down: 110/-154) Total: -44 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libpwdgrp')
-rw-r--r-- | libpwdgrp/pwd_grp.c | 69 |
1 files changed, 31 insertions, 38 deletions
diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c index 0fd458fdc..3b96cc4d6 100644 --- a/libpwdgrp/pwd_grp.c +++ b/libpwdgrp/pwd_grp.c | |||
@@ -316,27 +316,19 @@ static void *convert_to_struct(struct passdb *db, | |||
316 | return result; | 316 | return result; |
317 | } | 317 | } |
318 | 318 | ||
319 | /****** getXXnam/id_r */ | 319 | static int massage_data_for_r_func(struct passdb *db, |
320 | |||
321 | static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos, | ||
322 | char *buffer, size_t buflen, | 320 | char *buffer, size_t buflen, |
323 | void *result) | 321 | void **result, |
322 | char *buf) | ||
324 | { | 323 | { |
325 | void *struct_buf = *(void**)result; | 324 | void *result_buf = *result; |
326 | char *buf; | 325 | *result = NULL; |
327 | struct passdb *db = &get_S()->db[db_and_field_pos >> 2]; | ||
328 | |||
329 | *(void**)result = NULL; | ||
330 | buf = parse_file(db, name, 0 /*db_and_field_pos & 3*/); | ||
331 | /* "db_and_field_pos & 3" is commented out since so far we don't implement | ||
332 | * getXXXid_r() functions which would use that to pass 2 here */ | ||
333 | if (buf) { | 326 | if (buf) { |
334 | size_t size = S.tokenize_end - buf; | 327 | if (S.string_size > buflen) { |
335 | if (size > buflen) { | ||
336 | errno = ERANGE; | 328 | errno = ERANGE; |
337 | } else { | 329 | } else { |
338 | memcpy(buffer, buf, size); | 330 | memcpy(buffer, buf, S.string_size); |
339 | *(void**)result = convert_to_struct(db, buffer, struct_buf); | 331 | *result = convert_to_struct(db, buffer, result_buf); |
340 | } | 332 | } |
341 | free(buf); | 333 | free(buf); |
342 | } | 334 | } |
@@ -347,6 +339,22 @@ static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos, | |||
347 | return errno; | 339 | return errno; |
348 | } | 340 | } |
349 | 341 | ||
342 | /****** getXXnam/id_r */ | ||
343 | |||
344 | static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos, | ||
345 | char *buffer, size_t buflen, | ||
346 | void *result) | ||
347 | { | ||
348 | char *buf; | ||
349 | struct passdb *db = &get_S()->db[db_and_field_pos >> 2]; | ||
350 | |||
351 | buf = parse_file(db, name, 0 /*db_and_field_pos & 3*/); | ||
352 | /* "db_and_field_pos & 3" is commented out since so far we don't implement | ||
353 | * getXXXid_r() functions which would use that to pass 2 here */ | ||
354 | |||
355 | return massage_data_for_r_func(db, buffer, buflen, result, buf); | ||
356 | } | ||
357 | |||
350 | int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf, | 358 | int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf, |
351 | char *buffer, size_t buflen, | 359 | char *buffer, size_t buflen, |
352 | struct passwd **result) | 360 | struct passwd **result) |
@@ -369,15 +377,12 @@ int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer | |||
369 | 377 | ||
370 | /****** getXXent_r */ | 378 | /****** getXXent_r */ |
371 | 379 | ||
372 | static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen, | 380 | static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen, |
373 | void *result, | 381 | void *result) |
374 | unsigned db_idx) | ||
375 | { | 382 | { |
376 | char *buf; | 383 | char *buf; |
377 | struct passdb *db = &get_S()->db[db_idx]; | 384 | struct passdb *db = &get_S()->db[db_idx]; |
378 | 385 | ||
379 | *(void**)result = NULL; | ||
380 | |||
381 | if (!db->fp) { | 386 | if (!db->fp) { |
382 | db->fp = fopen_for_read(db->filename); | 387 | db->fp = fopen_for_read(db->filename); |
383 | if (!db->fp) { | 388 | if (!db->fp) { |
@@ -387,26 +392,14 @@ static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen, | |||
387 | } | 392 | } |
388 | 393 | ||
389 | buf = parse_common(db->fp, db, /*no search key:*/ NULL, 0); | 394 | buf = parse_common(db->fp, db, /*no search key:*/ NULL, 0); |
390 | if (buf) { | 395 | return massage_data_for_r_func(db, buffer, buflen, result, buf); |
391 | size_t size = S.tokenize_end - buf; | ||
392 | if (size > buflen) { | ||
393 | errno = ERANGE; | ||
394 | } else { | ||
395 | memcpy(buffer, buf, size); | ||
396 | *(void**)result = convert_to_struct(db, buffer, struct_buf); | ||
397 | } | ||
398 | free(buf); | ||
399 | } | ||
400 | /* "The reentrant functions return zero on success. | ||
401 | * In case of error, an error number is returned." | ||
402 | * NB: not finding the record is also a "success" here: | ||
403 | */ | ||
404 | return errno; | ||
405 | } | 396 | } |
406 | 397 | ||
407 | int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, struct passwd **result) | 398 | int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, |
399 | struct passwd **result) | ||
408 | { | 400 | { |
409 | return getXXent_r(struct_buf, buffer, buflen, result, 0); | 401 | *result = struct_buf; |
402 | return getXXent_r(0, buffer, buflen, result); | ||
410 | } | 403 | } |
411 | 404 | ||
412 | /****** getXXnam/id */ | 405 | /****** getXXnam/id */ |