getpwnam.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * getpwnam.c - This file is part of the libc-8086/pwd package for ELKS,
  4. * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
  5. * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free
  19. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <features.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <paths.h>
  28. #include "config.h"
  29. #ifdef __UCLIBC_HAS_THREADS__
  30. #include <pthread.h>
  31. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  32. # define LOCK pthread_mutex_lock(&mylock)
  33. # define UNLOCK pthread_mutex_unlock(&mylock);
  34. #else
  35. # define LOCK
  36. # define UNLOCK
  37. #endif
  38. int getpwnam_r (const char *name, struct passwd *password,
  39. char *buff, size_t buflen, struct passwd **result)
  40. {
  41. int ret;
  42. int passwd_fd;
  43. *result = NULL;
  44. if (name == NULL) {
  45. return EINVAL;
  46. }
  47. if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) {
  48. return ENOENT;
  49. }
  50. while ((ret=__getpwent_r(password, buff, buflen, passwd_fd)) == 0) {
  51. if (!strcmp(password->pw_name, name)) {
  52. close(passwd_fd);
  53. *result = password;
  54. return 0;
  55. }
  56. }
  57. close(passwd_fd);
  58. return ret;
  59. }
  60. struct passwd *getpwnam(const char *name)
  61. {
  62. int ret;
  63. static char line_buff[PWD_BUFFER_SIZE];
  64. static struct passwd pwd;
  65. struct passwd *result;
  66. LOCK;
  67. if ((ret=getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
  68. UNLOCK;
  69. return &pwd;
  70. }
  71. __set_errno(ret);
  72. UNLOCK;
  73. return NULL;
  74. }