getspnam.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * getspnam.c - Based on getpwnam.c
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. */
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <shadow.h>
  24. #define PWD_BUFFER_SIZE 256
  25. int getspnam_r (const char *name, struct spwd *spwd,
  26. char *buff, size_t buflen, struct spwd **crap)
  27. {
  28. int spwd_fd;
  29. if (name == NULL) {
  30. __set_errno(EINVAL);
  31. return -1;
  32. }
  33. if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0)
  34. return -1;
  35. while (__getspent_r(spwd, buff, buflen, spwd_fd) != -1)
  36. if (!strcmp(spwd->sp_namp, name)) {
  37. close(spwd_fd);
  38. return 0;
  39. }
  40. close(spwd_fd);
  41. return -1;
  42. }
  43. struct spwd *getspnam(const char *name)
  44. {
  45. static char line_buff[PWD_BUFFER_SIZE];
  46. static struct spwd spwd;
  47. if (getspnam_r(name, &spwd, line_buff, PWD_BUFFER_SIZE, NULL) != -1) {
  48. return &spwd;
  49. }
  50. return NULL;
  51. }