getgroups function

List<int> getgroups()

Retrieves the list of groups for the current users.

Implementation

List<int> getgroups() {
  final groups = <int>[];

  _getgroups ??= Libc().dylib.lookupFunction<
      ffi.Int32 Function(ffi.Int32, ffi.Pointer<ffi.Uint32>),
      _dart_getgroups>('getgroups');

  /// get how many groups there are.
  final size = _getgroups!(0, ffi.nullptr);

  using((arena) {
    // allocate the list.
    final list = arena<ffi.Uint32>(size);

    final count = _getgroups!(
      size,
      list,
    );

    if (count == -1) {
      throw PosixException('getgroups call failed', errno());
    }

    for (var i = 0; i < size; i++) {
      groups.add(list[i]);
    }
  });
  return groups;
}