gethostname function

String gethostname()

Get the name of the current host.

Throws a PosixException if an error occurs The result is null-terminated if LEN is large enough for the full name and the terminator.

Implementation

String gethostname() {
  const bufSize = 64 + 1;
  final cName = malloc.allocate<Utf8>(bufSize);

  _gethostname ??= Libc().dylib.lookupFunction<
      ffi.Int32 Function(ffi.Pointer<Utf8>, ffi.Uint64),
      _dart_gethostname>('gethostname');
  final result = _gethostname!(
    cName,
    bufSize,
  );

  _throwIfErrno('gethostname', result, cName);

  final hostname = cName.toDartString();
  malloc.free(cName);

  return hostname;
}