getspnam.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 **result)
  36. {
  37. int spwd_fd;
  38. if (name == NULL) {
  39. return EINVAL;
  40. }
  41. if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0)
  42. return errno;
  43. *result = NULL;
  44. while (__getspent_r(spwd, buff, buflen, spwd_fd) == 0)
  45. if (!strcmp(spwd->sp_namp, name)) {
  46. close(spwd_fd);
  47. *result = spwd;
  48. return 0;
  49. }
  50. close(spwd_fd);
  51. return EINVAL;
  52. }
  53. struct spwd *getspnam(const char *name)
  54. {
  55. int ret;
  56. static char line_buff[PWD_BUFFER_SIZE];
  57. static struct spwd spwd;
  58. struct spwd *result;
  59. LOCK;
  60. if ((ret=getspnam_r(name, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
  61. UNLOCK;
  62. return &spwd;
  63. }
  64. UNLOCK;
  65. __set_errno(ret);
  66. return NULL;
  67. }