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

View File

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
F439D28E2ABA7DCC00B92CAE /* unistd.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F439D28D2ABA7DCC00B92CAE /* unistd.hpp */; };
F4AE3B802AB440A000464926 /* dyld.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F4AE3B7E2AB440A000464926 /* dyld.hpp */; };
F4AE3B812AB440A000464926 /* common.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F4AE3B7F2AB440A000464926 /* common.hpp */; };
F4AE3BA22AB44AC500464926 /* Tests.mm in Sources */ = {isa = PBXBuildFile; fileRef = F4AE3BA12AB44AC400464926 /* Tests.mm */; };
@ -26,6 +27,7 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
F439D28D2ABA7DCC00B92CAE /* unistd.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = unistd.hpp; path = include/MacStd/unistd.hpp; sourceTree = "<group>"; };
F4AE3B732AB43EB600464926 /* libMacStd.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMacStd.a; sourceTree = BUILT_PRODUCTS_DIR; };
F4AE3B7E2AB440A000464926 /* dyld.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = dyld.hpp; path = include/MacStd/dyld.hpp; sourceTree = "<group>"; };
F4AE3B7F2AB440A000464926 /* common.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = common.hpp; path = include/MacStd/common.hpp; sourceTree = "<group>"; };
@ -63,6 +65,7 @@
F4AE3BCF2AB4561A00464926 /* pch.hpp */,
F4AE3B742AB43EB600464926 /* Products */,
F4AE3BA02AB44AC400464926 /* Tests */,
F439D28D2ABA7DCC00B92CAE /* unistd.hpp */,
);
sourceTree = "<group>";
};
@ -93,6 +96,7 @@
F4AE3BD12AB4561A00464926 /* pch.hpp in Headers */,
F4AE3B802AB440A000464926 /* dyld.hpp in Headers */,
F4AE3B812AB440A000464926 /* common.hpp in Headers */,
F439D28E2ABA7DCC00B92CAE /* unistd.hpp in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -347,7 +351,7 @@
CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = si.amebis.MacStd.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
@ -365,7 +369,7 @@
CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = si.amebis.MacStd.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";

View File

@ -5,6 +5,7 @@
#import <XCTest/XCTest.h>
#include "../include/MacStd/dyld.hpp"
#include "../include/MacStd/unistd.hpp"
@interface Tests : XCTestCase
@end
@ -24,6 +25,11 @@
XCTAssert(_NSGetExecutablePath(path) == 0);
}
- (void)test_getcwd {
std::string path;
XCTAssert(getcwd(path));
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{

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;
}

View File

@ -6,3 +6,4 @@
#pragma once
#include "../include/MacStd/dyld.hpp"
#include "../include/MacStd/unistd.hpp"