Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions Sources/CoreFoundation/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#define SECURITY_WIN32
#include <Security.h>

#define getcwd _NS_getcwd
#define open _NS_open

#endif
Expand All @@ -69,11 +68,6 @@ char **_CFArgv(void) { return *_NSGetArgv(); }
int _CFArgc(void) { return *_NSGetArgc(); }
#endif


CF_PRIVATE Boolean _CFGetCurrentDirectory(char *path, int maxlen) {
return getcwd(path, maxlen) != NULL;
}

#if TARGET_OS_WIN32
// Returns the path to the CF DLL, which we can then use to find resources like char sets
bool bDllPathCached = false;
Expand Down Expand Up @@ -1131,24 +1125,6 @@ CF_EXPORT int _NS_unlink(const char *name) {
return res;
}

// Warning: this doesn't support dstbuf as null even though 'getcwd' does
CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size) {
if (!dstbuf) {
CFLog(kCFLogLevelWarning, CFSTR("CFPlatform: getcwd called with null buffer"));
return 0;
}

wchar_t *buf = _wgetcwd(NULL, 0);
if (!buf) {
return NULL;
}

// Convert result to UTF8
copyToNarrowFileSystemRepresentation(buf, (CFIndex)size, dstbuf);
free(buf);
return dstbuf;
}

CF_EXPORT char *_NS_getenv(const char *name) {
// todo: wide getenv
// We have to be careful what happens here, because getenv is called during cf initialization, and things like cfstring may not be working yet
Expand Down
1 change: 0 additions & 1 deletion Sources/CoreFoundation/include/ForFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ CF_EXPORT int _NS_mkdir(const char *name);
CF_EXPORT int _NS_rmdir(const char *name);
CF_EXPORT int _NS_chmod(const char *name, int mode);
CF_EXPORT int _NS_unlink(const char *name);
CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size); // Warning: this doesn't support dstbuf as null even though 'getcwd' does
CF_EXPORT char *_NS_getenv(const char *name);
CF_EXPORT int _NS_rename(const char *oldName, const char *newName);
CF_EXPORT int _NS_open(const char *name, int oflag, int pmode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ CF_EXPORT int _NS_mkdir(const char *name);
CF_EXPORT int _NS_rmdir(const char *name);
CF_EXPORT int _NS_chmod(const char *name, int mode);
CF_EXPORT int _NS_unlink(const char *name);
CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size); // Warning: this doesn't support dstbuf as null even though 'getcwd' does
CF_EXPORT char *_NS_getenv(const char *name);
CF_EXPORT int _NS_rename(const char *oldName, const char *newName);
CF_EXPORT int _NS_open(const char *name, int oflag, int pmode);
Expand Down
25 changes: 25 additions & 0 deletions Sources/Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,28 @@ extension FileManager {
}
}
}

// Used in CFURL.c (use FileManager API so that platform specifics like long path prefix strip can be handled in a single place)
@_cdecl("_CFGetCurrentDirectory")
internal func _CFGetCurrentDirectory(_ buffer: UnsafeMutablePointer<CChar>?, _ size: Int) -> Bool {
guard let buffer = buffer, size > 0 else {
return false // Invalid parameters
}

let currentPath = FileManager.default.currentDirectoryPath

// Convert to C string representation
let cString = currentPath.utf8CString
let requiredSize = cString.count // includes null terminator

if requiredSize > size {
return false // Buffer too small
}

// Copy the string to the buffer
cString.withUnsafeBufferPointer { sourceBuffer in
buffer.initialize(from: sourceBuffer.baseAddress!, count: requiredSize)
}

return true // Success
}