getspuid.c 1.9 KB

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