Skip to content

Commit 31e7c24

Browse files
committed
FEAT: new to-real-file native (posix version) for resolving canonicalized filenames (removing .. and . path components, simplification of sequences of multiple slashes, removal of trailing slashes, and the resolution of symbolic links).
1 parent e108cfc commit 31e7c24

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/core/n-io.c

+30
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,36 @@ static REBSER *Read_All_File(char *fname)
468468
return R_RET;
469469
}
470470

471+
/***********************************************************************
472+
**
473+
*/ REBNATIVE(to_real_file)
474+
/*
475+
// to-real-file: native [
476+
// "Returns canonicalized absolute pathname or none if path does not exists. Resolves symbolic links."
477+
// path [file! string!]
478+
// ]
479+
***********************************************************************/
480+
{
481+
REBVAL *path = D_ARG(1);
482+
REBINT len;
483+
REBSER *ser;
484+
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) {
490+
FREE_SERIES(ser);
491+
return R_NONE;
492+
}
493+
len = strlen(tmp);
494+
new = To_REBOL_Path(tmp, len, OS_WIDE, FALSE);
495+
Set_Series(REB_FILE, D_RET, new);
496+
FREE_SERIES(ser);
497+
FREE_MEM(tmp);
498+
return R_RET;
499+
}
500+
471501
// Blog: http://www.rebol.net/cgi-bin/r3blog.r?view=0319
472502
/***********************************************************************
473503
**

src/os/posix/host-lib.c

+17
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,23 @@ int pipe2(int pipefd[2], int flags); //to avoid "implicit-function-declaration"
663663
}
664664

665665

666+
/***********************************************************************
667+
**
668+
*/ char* OS_Real_Path(const char *path)
669+
/*
670+
** Returns a null-terminated string containing the canonicalized
671+
** absolute pathname corresponding to path. In the returned string,
672+
** symbolic links are resolved, as are . and .. pathname components.
673+
** Consecutive slash (/) characters are replaced by a single slash.
674+
**
675+
** The result should be freed after copy/conversion.
676+
**
677+
***********************************************************************/
678+
{
679+
return realpath(path, NULL); // Be sure to call free() after usage
680+
}
681+
682+
666683
/***********************************************************************
667684
**
668685
*/ void OS_File_Time(REBREQ *file, REBOL_DAT *dat)

src/os/win32/host-lib.c

+15
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,21 @@ static void *Task_Ready;
658658
return GetLastError();
659659
}
660660

661+
/***********************************************************************
662+
**
663+
*/ char* OS_Real_Path(const char *path)
664+
/*
665+
** Returns a null-terminated string containing the canonicalized
666+
** absolute pathname corresponding to path. In the returned string,
667+
** symbolic links are resolved, as are . and .. pathname components.
668+
** Consecutive slash (/) characters are replaced by a single slash.
669+
**
670+
** The result should be freed after copy/conversion.
671+
**
672+
***********************************************************************/
673+
{
674+
return NULL; // not yet implemented
675+
}
661676

662677
/***********************************************************************
663678
**

0 commit comments

Comments
 (0)