Add os.realpath() to resolve symbolic links on Posix systems

This commit is contained in:
Jason Perkins 2014-05-23 18:37:15 -04:00
parent f3395f6cad
commit 7c8acae626
5 changed files with 51 additions and 10 deletions

View File

@ -11,9 +11,6 @@
#if PLATFORM_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef struct struct_MatchInfo
{
HANDLE handle;

37
src/host/os_realpath.c Normal file
View File

@ -0,0 +1,37 @@
/**
* \file os_realpath.c
* \brief Return the canonical absolute version of a given path.
* \author Copyright (c) 2014 Jason Perkins and the Premake project
*/
#include "premake.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int os_realpath(lua_State* L)
{
char result[PATH_MAX];
int ok;
const char* path = luaL_checkstring(L, 1);
#if PLATFORM_POSIX
ok = (realpath(path, result) != NULL);
#elif PLATFORM_WINDOWS
ok = (_fullpath(result, path, PATH_MAX) != NULL);
#else
do_getabsolute(result, path, NULL);
ok = 1;
#endif
if (!ok) {
lua_pushnil(L);
lua_pushfstring(L, "unable to fetch real path of '%s', errno %d : %s", path, errno, strerror(errno));
return 2;
}
lua_pushstring(L, result);
return 1;
}

View File

@ -8,7 +8,7 @@
#include <string.h>
static void getabsolute(char* result, const char* value, const char* relative_to)
void do_getabsolute(char* result, const char* value, const char* relative_to)
{
int i;
char* ch;
@ -85,7 +85,7 @@ int path_getabsolute(lua_State* L)
lua_pushnil(L);
while (lua_next(L, 1)) {
const char* value = luaL_checkstring(L, -1);
getabsolute(buffer, value, relative_to);
do_getabsolute(buffer, value, relative_to);
lua_pop(L, 1);
lua_pushnumber(L, ++i);
@ -96,7 +96,7 @@ int path_getabsolute(lua_State* L)
}
else {
const char* value = luaL_checkstring(L, 1);
getabsolute(buffer, value, relative_to);
do_getabsolute(buffer, value, relative_to);
lua_pushstring(L, buffer);
return 1;
}

View File

@ -73,6 +73,7 @@ static const luaL_Reg os_functions[] = {
{ "matchstart", os_matchstart },
{ "mkdir", os_mkdir },
{ "pathsearch", os_pathsearch },
{ "realpath", os_realpath },
{ "rmdir", os_rmdir },
{ "stat", os_stat },
{ "uuid", os_uuid },
@ -148,10 +149,6 @@ int premake_execute(lua_State* L, int argc, const char** argv)
*/
int premake_locate(lua_State* L, const char* argv0)
{
#if !defined(PATH_MAX)
#define PATH_MAX (4096)
#endif
char buffer[PATH_MAX];
const char* path = NULL;

View File

@ -38,6 +38,8 @@
#define PLATFORM_STRING "windows"
#endif
#define PLATFORM_POSIX (PLATFORM_LINUX || PLATFORM_BSD || PLATFORM_MACOSX || PLATFORM_SOLARIS)
/* Pull in platform-specific headers required by built-in functions */
#if PLATFORM_WINDOWS
@ -48,12 +50,19 @@
#endif
/* Fill in any missing bits */
#ifndef PATH_MAX
#define PATH_MAX (4096)
#endif
/* A success return code */
#define OKAY (0)
/* Bootstrapping helper functions */
unsigned long do_hash(const char* str, int seed);
void do_getabsolute(char* result, const char* value, const char* relative_to);
int do_getcwd(char* buffer, size_t size);
int do_isabsolute(const char* path);
int do_isfile(const char* filename);
@ -87,6 +96,7 @@ int os_matchnext(lua_State* L);
int os_matchstart(lua_State* L);
int os_mkdir(lua_State* L);
int os_pathsearch(lua_State* L);
int os_realpath(lua_State* L);
int os_rmdir(lua_State* L);
int os_stat(lua_State* L);
int os_uuid(lua_State* L);