unistd: add getcwd

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman
2023-09-20 03:24:24 +02:00
parent bcd2fd127a
commit 7374e0dc38
4 changed files with 47 additions and 2 deletions

34
include/MacStd/unistd.hpp Normal file
View File

@@ -0,0 +1,34 @@
/*
SPDX-License-Identifier: MIT
Copyright © 2023 Amebis
*/
#pragma once
#include "common.hpp"
#include <unistd.h>
#include <string>
///
/// Copies the absolute pathname of the current working directory
///
/// \sa [getcwd function](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getcwd.3.html)
///
inline const char* getcwd(std::string& path)
{
char stack_buffer[MACSTD_STACK_BUFFER_BYTES];
auto result = getcwd(stack_buffer, MACSTD_STACK_BUFFER_BYTES);
if (result) {
path = result;
return path.data();
}
if (errno == ERANGE) {
result = getcwd(NULL, 0);
if (result) {
path = result;
free(result);
return path.data();
}
}
return NULL;
}