getspuid.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * getspuid.c - Based on getpwuid.c
  4. * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #include <features.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include "config.h"
  27. #ifdef __UCLIBC_HAS_THREADS__
  28. #include <pthread.h>
  29. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  30. # define LOCK pthread_mutex_lock(&mylock)
  31. # define UNLOCK pthread_mutex_unlock(&mylock);
  32. #else
  33. # define LOCK
  34. # define UNLOCK
  35. #endif
  36. int getspuid_r (uid_t uid, struct spwd *spwd,
  37. char *buff, size_t buflen, struct spwd **result)
  38. {
  39. int ret;
  40. char pwd_buff[PWD_BUFFER_SIZE];
  41. struct passwd password;
  42. *result = NULL;
  43. ret = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), NULL);
  44. if (ret != 0)
  45. return ret;
  46. ret = getspnam_r(password.pw_name, spwd, buff, buflen, result);
  47. *result = spwd;
  48. return ret;
  49. }
  50. struct spwd *getspuid(uid_t uid)
  51. {
  52. int ret;
  53. static char line_buff[PWD_BUFFER_SIZE];
  54. static struct spwd spwd;
  55. struct spwd *result;
  56. LOCK;
  57. if ((ret=getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
  58. UNLOCK;
  59. return &spwd;
  60. }
  61. UNLOCK;
  62. __set_errno(ret);
  63. return NULL;
  64. }