fgetgrent.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fgetgrent.c - This file is part of the libc-8086/grp 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 <stdio.h>
  23. #include <errno.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 fgetgrent_r (FILE *__restrict file, struct group *__restrict grp,
  35. char *__restrict buff, size_t buflen,
  36. struct group **__restrict result)
  37. {
  38. int ret;
  39. if (file == NULL) {
  40. return EINTR;
  41. }
  42. *result = NULL;
  43. flockfile(file);
  44. ret = __getgrent_r(grp, buff, buflen, fileno(file));
  45. funlockfile(file);
  46. if (ret == 0) {
  47. *result = grp;
  48. return 0;
  49. }
  50. __set_errno(ret);
  51. return ret;
  52. }
  53. struct group *fgetgrent(FILE * file)
  54. {
  55. int ret;
  56. struct group *result;
  57. static struct group grp;
  58. static char line_buff[PWD_BUFFER_SIZE];
  59. LOCK;
  60. ret=fgetgrent_r(file, &grp, line_buff, sizeof(line_buff), &result);
  61. if (ret == 0) {
  62. UNLOCK;
  63. return result;
  64. }
  65. UNLOCK;
  66. __set_errno(ret);
  67. return NULL;
  68. }