getspnam.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * getspnam.c - Based on getpwnam.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 <unistd.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <fcntl.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 getspnam_r (const char *name, struct spwd *spwd,
  37. char *buff, size_t buflen, struct spwd **result)
  38. {
  39. int spwd_fd;
  40. if (name == NULL) {
  41. return EINVAL;
  42. }
  43. if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0)
  44. return errno;
  45. *result = NULL;
  46. while (__getspent_r(spwd, buff, buflen, spwd_fd) == 0)
  47. if (!strcmp(spwd->sp_namp, name)) {
  48. close(spwd_fd);
  49. *result = spwd;
  50. return 0;
  51. }
  52. close(spwd_fd);
  53. return EINVAL;
  54. }
  55. struct spwd *getspnam(const char *name)
  56. {
  57. int ret;
  58. static char line_buff[PWD_BUFFER_SIZE];
  59. static struct spwd spwd;
  60. struct spwd *result;
  61. LOCK;
  62. if ((ret=getspnam_r(name, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
  63. UNLOCK;
  64. return &spwd;
  65. }
  66. UNLOCK;
  67. __set_errno(ret);
  68. return NULL;
  69. }