1
0

db.c 2.1 KB

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