fgetpwent.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fgetpwent.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 <errno.h>
  24. #include <stdio.h>
  25. #include "config.h"
  26. #ifdef __UCLIBC_HAS_THREADS__
  27. #include <pthread.h>
  28. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  29. # define LOCK pthread_mutex_lock(&mylock)
  30. # define UNLOCK pthread_mutex_unlock(&mylock);
  31. #else
  32. # define LOCK
  33. # define UNLOCK
  34. #endif
  35. int fgetpwent_r (FILE *file, struct passwd *password,
  36. char *buff, size_t buflen, struct passwd **result)
  37. {
  38. int res;
  39. if (file == NULL) {
  40. return EINTR;
  41. }
  42. *result = NULL;
  43. res = __getpwent_r(password, buff, buflen, fileno(file));
  44. *result = password;
  45. return res;
  46. }
  47. struct passwd *fgetpwent(FILE * file)
  48. {
  49. int ret;
  50. static char line_buff[PWD_BUFFER_SIZE];
  51. static struct passwd pwd;
  52. struct passwd *result;
  53. LOCK;
  54. if ((ret=fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
  55. UNLOCK;
  56. return &pwd;
  57. }
  58. UNLOCK;
  59. __set_errno(ret);
  60. return NULL;
  61. }