grp.odin 3.4 KB

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