1
0

db.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* It must be here to include config.h before another headers */
  2. #include "lub/db.h"
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <pwd.h>
  7. #include <grp.h>
  8. #include <unistd.h>
  9. struct passwd *lub_db_getpwnam(const char *name)
  10. {
  11. size_t size;
  12. char *buf;
  13. struct passwd *pwbuf;
  14. struct passwd *pw = NULL;
  15. int res = 0;
  16. #ifdef _SC_GETPW_R_SIZE_MAX
  17. size = sysconf(_SC_GETPW_R_SIZE_MAX);
  18. #else
  19. size = 1024;
  20. #endif
  21. pwbuf = malloc(sizeof(*pwbuf) + size);
  22. if (!pwbuf)
  23. return NULL;
  24. buf = (char *)pwbuf + sizeof(*pwbuf);
  25. res = getpwnam_r(name, pwbuf, buf, size, &pw);
  26. if (res || !pw) {
  27. free(pwbuf);
  28. if (res != 0)
  29. errno = res;
  30. else
  31. errno = ENOENT;
  32. return NULL;
  33. }
  34. return pw;
  35. }
  36. struct passwd *lub_db_getpwuid(uid_t uid)
  37. {
  38. size_t size;
  39. char *buf;
  40. struct passwd *pwbuf;
  41. struct passwd *pw = NULL;
  42. int res = 0;
  43. #ifdef _SC_GETPW_R_SIZE_MAX
  44. size = sysconf(_SC_GETPW_R_SIZE_MAX);
  45. #else
  46. size = 1024;
  47. #endif
  48. pwbuf = malloc(sizeof(*pwbuf) + size);
  49. if (!pwbuf)
  50. return NULL;
  51. buf = (char *)pwbuf + sizeof(*pwbuf);
  52. res = getpwuid_r(uid, pwbuf, buf, size, &pw);
  53. if (NULL == pw) {
  54. free(pwbuf);
  55. if (res != 0)
  56. errno = res;
  57. else
  58. errno = ENOENT;
  59. return NULL;
  60. }
  61. return pw;
  62. }
  63. struct group *lub_db_getgrnam(const char *name)
  64. {
  65. size_t size;
  66. char *buf;
  67. struct group *grbuf;
  68. struct group *gr = NULL;
  69. int res = 0;
  70. #ifdef _SC_GETGR_R_SIZE_MAX
  71. size = sysconf(_SC_GETGR_R_SIZE_MAX);
  72. #else
  73. size = 1024;
  74. #endif
  75. grbuf = malloc(sizeof(*grbuf) + size);
  76. if (!grbuf)
  77. return NULL;
  78. buf = (char *)grbuf + sizeof(*grbuf);
  79. res = getgrnam_r(name, grbuf, buf, size, &gr);
  80. if (NULL == gr) {
  81. free(grbuf);
  82. if (res != 0)
  83. errno = res;
  84. else
  85. errno = ENOENT;
  86. return NULL;
  87. }
  88. return gr;
  89. }
  90. struct group *lub_db_getgrgid(gid_t gid)
  91. {
  92. size_t size;
  93. char *buf;
  94. struct group *grbuf;
  95. struct group *gr = NULL;
  96. int res = 0;
  97. #ifdef _SC_GETGR_R_SIZE_MAX
  98. size = sysconf(_SC_GETGR_R_SIZE_MAX);
  99. #else
  100. size = 1024;
  101. #endif
  102. grbuf = malloc(sizeof(struct group) + size);
  103. if (!grbuf)
  104. return NULL;
  105. buf = (char *)grbuf + sizeof(struct group);
  106. res = getgrgid_r(gid, grbuf, buf, size, &gr);
  107. if (!gr) {
  108. free(grbuf);
  109. if (res != 0)
  110. errno = res;
  111. else
  112. errno = ENOENT;
  113. return NULL;
  114. }
  115. return gr;
  116. }