1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-08 15:29:37 +00:00

Improve FindAbsolutePath implementation

This commit is contained in:
cosineblast 2024-01-16 16:02:57 -03:00
parent d34a6df002
commit 085509c2e1
2 changed files with 56 additions and 13 deletions

View File

@ -1,24 +1,67 @@
#include <stdlib.h>
#if defined(_WIN32)
# include <windows.h>
# include "xmalloc.h"
#endif
#if defined(_WIN32)
char *FindAbsolutePath (const char *path) {
return _fullpath (NULL, path, MAX_PATH);
char *FindAbsolutePath (const char *Path)
/*
** Determines the absolute path of the given relative path.
** If the path points to a symlink, resolves such symlink.
** The absolute path for the file is stored in a malloced buffer.
** Returns NULL if some error occured.
** The returned path's separator is system specific.
*/
{
HANDLE Handle = CreateFileA (Path,
FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (Handle == INVALID_HANDLE_VALUE) {
return NULL;
}
size_t BufferSize = MAX_PATH + 10;
char* Buffer = xmalloc(BufferSize);
DWORD Status = GetFinalPathNameByHandleA (Handle,
Buffer,
BufferSize,
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (Status == 0) {
free(Buffer);
CloseHandle(Handle);
return NULL;
}
CloseHandle(Handle);
return Buffer;
}
#else
extern char* realpath (const char* path, char* resolved_path);
/* this uses the POSIX1.-2008 version of the function,
which solves the problem of finding a maximum path length for the file */
char* FindAbsolutePath (const char* path) {
char* FindAbsolutePath (const char* path)
/*
** Determines the absolute path of the given relative path.
** If the path points to a symlink, resolves such symlink.
** The absolute path for the file is stored in a malloced buffer.
** Returns NULL if some error occured.
** The returned path's separator is system specific.
*/
{
return realpath (path, NULL);
}

View File

@ -41,14 +41,14 @@
/*****************************************************************************/
/**
* Determines the absolute path of the given relative path.
* The absolute path for the file is stored in a malloced buffer.
* Returns NULL if some error occured.
* The returned path's separator is system specific.
*/
char *FindAbsolutePath (const char *path);
/*
** Determines the absolute path of the given relative path.
** If the path points to a symlink, resolves such symlink.
** The absolute path for the file is stored in a malloced buffer.
** Returns NULL if some error occured.
** The returned path's separator is system specific.
*/
/* End of pathutil.h */