getcwd function

String getcwd()

We provide a wrapper for each posix method that takes and return dart types.

We recommend using the dart wrappers as they deal with memory allocation/cleanup and type conversion.

Gets the absolute pathname of the current working directory. If an error occurs a a PosixException is thrown with the error.

Implementation

/// Gets the absolute pathname of the current working directory.
/// If an error occurs a a [PosixException] is thrown with the error.
String getcwd() {
  final cBuf = malloc.allocate<ffi.Int8>(PATH_MAX);

  final result = native_getwd(
    cBuf,
  );

  if (result == ffi.nullptr) {
    final error = copyCBuffToDartString(cBuf);
    throw PosixException(error, -1);
  }

  return copyCBuffToDartString(cBuf);
}