Skip to content

Commit d996567

Browse files
committed
FEAT: win32 variant of to-real-file native
1 parent 31e7c24 commit d996567

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/core/n-io.c

+18-12
Original file line numberDiff line numberDiff line change
@@ -473,28 +473,34 @@ static REBSER *Read_All_File(char *fname)
473473
*/ REBNATIVE(to_real_file)
474474
/*
475475
// to-real-file: native [
476-
// "Returns canonicalized absolute pathname or none if path does not exists. Resolves symbolic links."
476+
// "Returns canonicalized absolute pathname. On Posix resolves symbolic links and returns NONE if file does not exists!"
477477
// path [file! string!]
478478
// ]
479479
***********************************************************************/
480480
{
481481
REBVAL *path = D_ARG(1);
482-
REBINT len;
483-
REBSER *ser;
482+
REBSER *ser = NULL;
484483
REBSER *new;
485-
char *tmp;
486-
487-
ser = Value_To_OS_Path(path, TRUE);
488-
tmp = OS_REAL_PATH(cs_cast(VAL_BIN(path)));
489-
if(!tmp) {
484+
REBCHR *tmp;
485+
// First normalize to OS native wide string
486+
ser = Value_To_OS_Path(path, FALSE);
487+
// Above function does . and .. pre-processing, which does also the OS_REAL_PATH.
488+
// So it could be replaced with version, which just prepares the input to required OS wide!
489+
if (!ser) {
490490
FREE_SERIES(ser);
491491
return R_NONE;
492492
}
493-
len = strlen(tmp);
494-
new = To_REBOL_Path(tmp, len, OS_WIDE, FALSE);
493+
// Try to call realpath on posix or _fullpath on Windows
494+
// Returned string must be released once done!
495+
tmp = OS_REAL_PATH(SERIES_DATA(ser));
496+
if (!tmp) return R_NONE;
497+
498+
// Convert OS native wide string back to Rebol file type
499+
new = To_REBOL_Path(tmp, 0, OS_WIDE, FALSE);
500+
OS_FREE(tmp);
501+
if (!new) return R_NONE;
502+
495503
Set_Series(REB_FILE, D_RET, new);
496-
FREE_SERIES(ser);
497-
FREE_MEM(tmp);
498504
return R_RET;
499505
}
500506

src/os/win32/host-lib.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ static void *Task_Ready;
660660

661661
/***********************************************************************
662662
**
663-
*/ char* OS_Real_Path(const char *path)
663+
*/ REBCHR* OS_Real_Path(const REBCHR *path)
664664
/*
665665
** Returns a null-terminated string containing the canonicalized
666666
** absolute pathname corresponding to path. In the returned string,
@@ -671,7 +671,7 @@ static void *Task_Ready;
671671
**
672672
***********************************************************************/
673673
{
674-
return NULL; // not yet implemented
674+
return _wfullpath(NULL, path, MAX_PATH);
675675
}
676676

677677
/***********************************************************************

0 commit comments

Comments
 (0)