grp.odin 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package posix
  2. import "core:c"
  3. when ODIN_OS == .Darwin {
  4. foreign import lib "system:System.framework"
  5. } else {
  6. foreign import lib "system:c"
  7. }
  8. // grp.h - group structure
  9. foreign lib {
  10. /*
  11. Closes the group database.
  12. Checking status would be done by setting errno to 0, calling this, and checking errno.
  13. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/endgrent.html ]]
  14. */
  15. endgrent :: proc() ---
  16. /*
  17. Rewinds the group database so getgrent() returns the first entry again.
  18. Checking status would be done by setting errno to 0, calling this, and checking errno.
  19. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/endgrent.html ]]
  20. */
  21. setgrent :: proc() ---
  22. /*
  23. Returns a pointer to an entry of the group database.
  24. Opens the group database if it isn't.
  25. Returns: nil on failure (setting errno) or EOF (not setting errno), the entry otherwise
  26. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/endgrent.html ]]
  27. */
  28. getgrent :: proc() -> ^group ---
  29. /*
  30. Searches for an entry with a matching gid in the group database.
  31. Returns: nil (setting errno) on failure, a pointer to the entry on success
  32. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgrgid.html ]]
  33. */
  34. getgrgid :: proc(gid: gid_t) -> ^group ---
  35. /*
  36. Searches for an entry with a matching gid in the group database.
  37. Updates grp with the matching entry and stores it (or a nil pointer (setting errno)) into result.
  38. Strings are allocated into the given buffer, you can call `sysconf(._GETGR_R_SIZE_MAX)` for an appropriate size.
  39. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgrgid.html ]]
  40. */
  41. getgrgid_r :: proc(gid: gid_t, grp: ^group, buffer: [^]byte, bufsize: c.size_t, result: ^^group) -> Errno ---
  42. /*
  43. Searches for an entry with a matching gid in the group database.
  44. Returns: nil (setting errno) on failure, a pointer to the entry on success
  45. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgrnam.html ]]
  46. */
  47. getgrnam :: proc(name: cstring) -> ^group ---
  48. /*
  49. Searches for an entry with a matching gid in the group database.
  50. Updates grp with the matching entry and stores it (or a nil pointer (setting errno)) into result.
  51. Strings are allocated into the given buffer, you can call `sysconf(._GETGR_R_SIZE_MAX)` for an appropriate size.
  52. Example:
  53. length := posix.sysconf(._GETGR_R_SIZE_MAX)
  54. if length == -1 {
  55. length = 1024
  56. }
  57. result: posix.group
  58. resultp: ^posix.group
  59. e: posix.Errno
  60. buffer: [dynamic]byte
  61. defer delete(buffer)
  62. for {
  63. mem_err := resize(&buffer, length)
  64. assert(mem_err == nil)
  65. e = posix.getgrnam_r("nobody", &result, raw_data(buffer), len(buffer), &resultp)
  66. if e != .ERANGE {
  67. break
  68. }
  69. length *= 2
  70. assert(length > 0)
  71. }
  72. if e != .NONE {
  73. panic(string(posix.strerror(e)))
  74. }
  75. fmt.println(result)
  76. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgrnam.html ]]
  77. */
  78. getgrnam_r :: proc(name: cstring, grp: ^group, buffer: [^]byte, bufsize: c.size_t, result: ^^group) -> Errno ---
  79. }
  80. when ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD {
  81. gid_t :: distinct c.uint32_t
  82. group :: struct {
  83. gr_name: cstring, /* [PSX] group name */
  84. gr_passwd: cstring, /* group password */
  85. gr_gid: gid_t, /* [PSX] group id */
  86. gr_mem: [^]cstring, /* [PSX] group members */
  87. }
  88. } else {
  89. #panic("posix is unimplemented for the current target")
  90. }