__getpwent_r.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * __getpwent_r.c - This file is part of the libc-8086/pwd package for ELKS,
  3. * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
  4. * Copyright (C) 2001 Erik Andersen <andersee@debian.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * March 7, 2001 -- Reworked to be reentrant by Erik Andersen
  21. */
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <fcntl.h>
  26. #include "config.h"
  27. /* This isn't as flash as my previous version -- it doesn't dynamically
  28. scale down the gecos on too-long lines, but it also makes fewer syscalls,
  29. so it's probably nicer. Write me if you want the old version. Maybe I
  30. should include it as a build-time option... ?
  31. -Nat <ndf@linux.mit.edu> */
  32. int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pwd_fd)
  33. {
  34. char *field_begin;
  35. char *endptr;
  36. char *gid_ptr=NULL;
  37. char *uid_ptr=NULL;
  38. int line_len;
  39. int i;
  40. /* We use the restart label to handle malformatted lines */
  41. restart:
  42. /* Read the passwd line into the static buffer using a minimal of
  43. syscalls. */
  44. if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
  45. return -1;
  46. field_begin = strchr(line_buff, '\n');
  47. if (field_begin != NULL)
  48. lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)),
  49. SEEK_CUR);
  50. else { /* The line is too long - skip it. :-\ */
  51. do {
  52. if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
  53. return -1;
  54. } while (!(field_begin = strchr(line_buff, '\n')));
  55. lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1,
  56. SEEK_CUR);
  57. goto restart;
  58. }
  59. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
  60. *line_buff == '\t')
  61. goto restart;
  62. *field_begin = '\0';
  63. /* We've read the line; now parse it. */
  64. field_begin = line_buff;
  65. for (i = 0; i < 7; i++) {
  66. switch (i) {
  67. case 0:
  68. passwd->pw_name = field_begin;
  69. break;
  70. case 1:
  71. passwd->pw_passwd = field_begin;
  72. break;
  73. case 2:
  74. uid_ptr = field_begin;
  75. break;
  76. case 3:
  77. gid_ptr = field_begin;
  78. break;
  79. case 4:
  80. passwd->pw_gecos = field_begin;
  81. break;
  82. case 5:
  83. passwd->pw_dir = field_begin;
  84. break;
  85. case 6:
  86. passwd->pw_shell = field_begin;
  87. break;
  88. }
  89. if (i < 6) {
  90. field_begin = strchr(field_begin, ':');
  91. if (field_begin == NULL)
  92. goto restart;
  93. *field_begin++ = '\0';
  94. }
  95. }
  96. passwd->pw_gid = (gid_t) strtoul(gid_ptr, &endptr, 10);
  97. if (*endptr != '\0')
  98. goto restart;
  99. passwd->pw_uid = (uid_t) strtoul(uid_ptr, &endptr, 10);
  100. if (*endptr != '\0')
  101. goto restart;
  102. return 0;
  103. }