getspnam.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * getspnam.c - Based on getpwnam.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 <unistd.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <fcntl.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 getspnam_r (const char *name, struct spwd *spwd,
  35. char *buff, size_t buflen, struct spwd **crap)
  36. {
  37. int spwd_fd;
  38. if (name == NULL) {
  39. __set_errno(EINVAL);
  40. return -1;
  41. }
  42. if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0)
  43. return -1;
  44. while (__getspent_r(spwd, buff, buflen, spwd_fd) != -1)
  45. if (!strcmp(spwd->sp_namp, name)) {
  46. close(spwd_fd);
  47. return 0;
  48. }
  49. close(spwd_fd);
  50. return -1;
  51. }
  52. struct spwd *getspnam(const char *name)
  53. {
  54. static char line_buff[PWD_BUFFER_SIZE];
  55. static struct spwd spwd;
  56. LOCK;
  57. if (getspnam_r(name, &spwd, line_buff, sizeof(line_buff), NULL) != -1) {
  58. UNLOCK;
  59. return &spwd;
  60. }
  61. UNLOCK;
  62. return NULL;
  63. }