Commit af14afac af14afac25a99e356600bd4b07f7e70cad4a96b4 by Sergey Poznyakoff

(mu_register_retriever): Register a retrieving function.

(mu_retrieve): Retrieve information using stack of retriever functions.
(mu_getpwnam,mu_register_getpwnam): Rewritten using retriever functions.
(mu_getpwuid,mu_register_getpwuid): Likewise.
1 parent 17361803
...@@ -40,7 +40,6 @@ ...@@ -40,7 +40,6 @@
40 40
41 #include <mailutils/error.h> 41 #include <mailutils/error.h>
42 #include <mailutils/iterator.h> 42 #include <mailutils/iterator.h>
43 #include <mailutils/list.h>
44 #include <mailutils/mutil.h> 43 #include <mailutils/mutil.h>
45 44
46 /* convert a sequence of hex characters into an integer */ 45 /* convert a sequence of hex characters into an integer */
...@@ -405,32 +404,30 @@ mu_cpystr (char *dst, const char *src, size_t size) ...@@ -405,32 +404,30 @@ mu_cpystr (char *dst, const char *src, size_t size)
405 return len; 404 return len;
406 } 405 }
407 406
408 static list_t _app_getpwnam = NULL; 407 /* General retrieve stack support: */
409 408
410 void 409 void
411 mu_register_getpwnam (struct passwd *(*fun) __P((const char *))) 410 mu_register_retriever (list_t *pflist, mu_retrieve_fp fun)
412 { 411 {
413 if (!_app_getpwnam && list_create (&_app_getpwnam)) 412 if (!*pflist && list_create (pflist))
414 return; 413 return;
415 list_append (_app_getpwnam, fun); 414 list_append (*pflist, fun);
416 } 415 }
417 416
418 struct passwd * 417 void *
419 mu_getpwnam (const char *name) 418 mu_retrieve (list_t flist, void *data)
420 { 419 {
421 struct passwd *p; 420 void *p = NULL;
422 iterator_t itr; 421 iterator_t itr;
423 422
424 p = getpwnam (name); 423 if (iterator_create (&itr, flist) == 0)
425
426 if (!p && iterator_create (&itr, _app_getpwnam) == 0)
427 { 424 {
428 struct passwd *(*fun) __P((const char *)); 425 mu_retrieve_fp fun;
429 for (iterator_first (itr); !p && !iterator_is_done (itr); 426 for (iterator_first (itr); !p && !iterator_is_done (itr);
430 iterator_next (itr)) 427 iterator_next (itr))
431 { 428 {
432 iterator_current (itr, (void **)&fun); 429 iterator_current (itr, (void **)&fun);
433 p = (*fun) (name); 430 p = (*fun) (data);
434 } 431 }
435 432
436 iterator_destroy (&itr); 433 iterator_destroy (&itr);
...@@ -438,39 +435,41 @@ mu_getpwnam (const char *name) ...@@ -438,39 +435,41 @@ mu_getpwnam (const char *name)
438 return p; 435 return p;
439 } 436 }
440 437
441 static list_t _app_getpwuid = NULL; 438 /* getpwd support: */
439
440 static list_t _app_getpwnam = NULL;
442 441
443 void 442 void
444 mu_register_getpwuid (struct passwd *(*fun) __P((uid_t))) 443 mu_register_getpwnam (struct passwd *(*fun) __P((const char *)))
445 { 444 {
446 if (!_app_getpwuid && list_create (&_app_getpwuid)) 445 mu_register_retriever (&_app_getpwnam, (mu_retrieve_fp)fun);
447 return;
448 list_append (_app_getpwuid, fun);
449 } 446 }
450 447
451 struct passwd * 448 struct passwd *
452 mu_getpwuid (uid_t uid) 449 mu_getpwnam (const char *name)
453 { 450 {
454 struct passwd *p; 451 struct passwd *p = getpwnam (name);
455 iterator_t itr; 452 return p ? p : mu_retrieve (_app_getpwnam, (void*) name);
453 }
456 454
457 p = getpwuid (uid); 455 /* getpwuid support: */
458 456
459 if (!p && iterator_create (&itr, _app_getpwuid) == 0) 457 static list_t _app_getpwuid = NULL;
460 {
461 struct passwd *(*fun) __P((uid_t));
462 for (iterator_first (itr); !p && !iterator_is_done (itr);
463 iterator_next (itr))
464 {
465 iterator_current (itr, (void **)&fun);
466 p = (*fun) (uid);
467 }
468 458
469 iterator_destroy (&itr); 459 void
470 } 460 mu_register_getpwuid (struct passwd *(*fun) __P((uid_t)))
471 return p; 461 {
462 mu_register_retriever (&_app_getpwuid, (mu_retrieve_fp)fun);
463 }
464
465 struct passwd *
466 mu_getpwuid (uid_t uid)
467 {
468 struct passwd *p = getpwuid (uid);
469 return p ? p : mu_retrieve (_app_getpwuid, &uid);
472 } 470 }
473 471
472 /* Virtual domains */
474 473
475 int mu_virtual_domain; 474 int mu_virtual_domain;
476 475
...@@ -699,7 +698,7 @@ mu_tempfile (const char *tmpdir, char **namep) ...@@ -699,7 +698,7 @@ mu_tempfile (const char *tmpdir, char **namep)
699 * had to look it up, so if somebody else is curious, thats where 698 * had to look it up, so if somebody else is curious, thats where
700 * to find it. 699 * to find it.
701 */ 700 */
702 int mu_spawnvp(const char* prog, const char* const av_[], int* stat) 701 int mu_spawnvp (const char* prog, const char* const av_[], int* stat)
703 { 702 {
704 pid_t pid; 703 pid_t pid;
705 int err = 0; 704 int err = 0;
......