grent.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * grent.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. /*
  23. * setgrent(), endgrent(), and getgrent() are mutually-dependent functions,
  24. * so they are all included in the same object file, and thus all linked
  25. * in together.
  26. */
  27. #define _GNU_SOURCE
  28. #include <features.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <paths.h>
  32. #include <errno.h>
  33. #include "config.h"
  34. #ifdef __UCLIBC_HAS_THREADS__
  35. #include <pthread.h>
  36. static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  37. # define LOCK pthread_mutex_lock(&mylock)
  38. # define UNLOCK pthread_mutex_unlock(&mylock);
  39. #else
  40. # define LOCK
  41. # define UNLOCK
  42. #endif
  43. /* file descriptor for the group file currently open */
  44. static int grp_fd = -9;
  45. void setgrent(void)
  46. {
  47. LOCK;
  48. if (grp_fd > -1)
  49. close(grp_fd);
  50. grp_fd = open(_PATH_GROUP, O_RDONLY);
  51. UNLOCK;
  52. }
  53. void endgrent(void)
  54. {
  55. LOCK;
  56. if (grp_fd > -1)
  57. close(grp_fd);
  58. grp_fd = -9;
  59. UNLOCK;
  60. }
  61. int getgrent_r (struct group *grp, char *buff,
  62. size_t buflen, struct group **result)
  63. {
  64. int ret=EINVAL;
  65. *result = NULL;
  66. LOCK;
  67. /* Open /etc/group if it has never been opened */
  68. if (grp_fd == -9) {
  69. setgrent();
  70. }
  71. if (grp_fd == -1) {
  72. UNLOCK;
  73. return -1;
  74. }
  75. ret=__getgrent_r(grp, buff, buflen, grp_fd);
  76. if (ret == 0) {
  77. UNLOCK;
  78. *result = grp;
  79. return 0;
  80. }
  81. UNLOCK;
  82. __set_errno(ret);
  83. return ret;
  84. }
  85. struct group *getgrent(void)
  86. {
  87. int ret;
  88. struct group *result;
  89. static struct group grp;
  90. static char line_buff[PWD_BUFFER_SIZE];
  91. ret = getgrent_r(&grp, line_buff, sizeof(line_buff), &result);
  92. if (ret == 0) {
  93. return &grp;
  94. }
  95. __set_errno(ret);
  96. return NULL;
  97. }