__getpwent_r.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * __getpwent_r.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. * March 7, 2001 -- Reworked to be reentrant by Erik Andersen
  22. * Oct 9, 2003 -- Reworked again by Erik Andersen to be fully reentrant
  23. */
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include "config.h"
  30. /* This isn't as flash as my previous version -- it doesn't dynamically
  31. scale down the gecos on too-long lines, but it also makes fewer syscalls,
  32. so it's probably nicer. Write me if you want the old version. Maybe I
  33. should include it as a build-time option... ?
  34. -Nat <ndf@linux.mit.edu> */
  35. int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pwd_fd)
  36. {
  37. char *endptr, *field_begin;
  38. char *gid_ptr=NULL;
  39. char *uid_ptr=NULL;
  40. int i, line_len;
  41. if (buflen<PWD_BUFFER_SIZE) {
  42. return ERANGE;
  43. }
  44. /* We use the restart label to handle malformatted lines */
  45. restart:
  46. /* Read the passwd line into the buffer for processing */
  47. if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) {
  48. return EIO;
  49. }
  50. field_begin = strchr(line_buff, '\n');
  51. if (field_begin != NULL)
  52. lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)), SEEK_CUR);
  53. else {
  54. /* The line is too long - skip it. :-\ */
  55. do {
  56. if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) {
  57. return EIO;
  58. }
  59. } while (!(field_begin = strchr(line_buff, '\n')));
  60. lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1, SEEK_CUR);
  61. goto restart;
  62. }
  63. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || *line_buff == '\t')
  64. goto restart;
  65. *field_begin = '\0';
  66. /* We've read the line; now parse it. */
  67. field_begin = line_buff;
  68. for (i = 0; i < 7; i++) {
  69. switch (i) {
  70. case 0:
  71. passwd->pw_name = field_begin;
  72. break;
  73. case 1:
  74. passwd->pw_passwd = field_begin;
  75. break;
  76. case 2:
  77. uid_ptr = field_begin;
  78. break;
  79. case 3:
  80. gid_ptr = field_begin;
  81. break;
  82. case 4:
  83. passwd->pw_gecos = field_begin;
  84. break;
  85. case 5:
  86. passwd->pw_dir = field_begin;
  87. break;
  88. case 6:
  89. passwd->pw_shell = field_begin;
  90. break;
  91. }
  92. if (i < 6) {
  93. field_begin = strchr(field_begin, ':');
  94. if (field_begin == NULL)
  95. goto restart;
  96. *field_begin++ = '\0';
  97. }
  98. }
  99. passwd->pw_gid = (gid_t) strtoul(gid_ptr, &endptr, 10);
  100. if (*endptr != '\0')
  101. goto restart;
  102. passwd->pw_uid = (uid_t) strtoul(uid_ptr, &endptr, 10);
  103. if (*endptr != '\0')
  104. goto restart;
  105. return 0;
  106. }