summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lauxlib.c')
-rw-r--r--apps/plugins/lua/lauxlib.c48
1 files changed, 27 insertions, 21 deletions
diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c
index fd71e07217..3f6d8e6f1b 100644
--- a/apps/plugins/lua/lauxlib.c
+++ b/apps/plugins/lua/lauxlib.c
@@ -560,24 +560,35 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
560 return LUA_ERRFILE; 560 return LUA_ERRFILE;
561} 561}
562 562
563static void make_path(char* dest, size_t dest_size, char* curfile, char* newfile) 563bool get_cur_path(lua_State *L, char* dest, size_t dest_size)
564{ 564{
565 char* pos = rb->strrchr(curfile, '/'); 565 lua_Debug ar;
566 if(pos != NULL) 566 if(lua_getstack(L, 1, &ar))
567 { 567 {
568 unsigned int len = (unsigned int)(pos - curfile); 568 /* Try determining the base path of the current Lua chunk
569 len = len + 1 > dest_size ? dest_size - 1 : len; 569 and write it to dest. */
570 570 lua_getinfo(L, "S", &ar);
571 if(len > 0) 571
572 memcpy(dest, curfile, len); 572 char* curfile = (char*) &ar.source[1];
573 573 char* pos = rb->strrchr(curfile, '/');
574 dest[len] = '/'; 574 if(pos != NULL)
575 dest[len+1] = '\0'; 575 {
576 unsigned int len = (unsigned int)(pos - curfile);
577 len = len + 1 > dest_size ? dest_size - 1 : len;
578
579 if(len > 0)
580 memcpy(dest, curfile, len);
581
582 dest[len] = '/';
583 dest[len+1] = '\0';
584
585 return true;
586 }
587 else
588 return false;
576 } 589 }
577 else 590 else
578 dest[0] = '\0'; 591 return false;
579
580 strncat(dest, newfile, dest_size - strlen(dest));
581} 592}
582 593
583LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { 594LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
@@ -590,13 +601,8 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
590 if(lf.f < 0) { 601 if(lf.f < 0) {
591 /* Fallback */ 602 /* Fallback */
592 603
593 lua_Debug ar; 604 if(get_cur_path(L, buffer, sizeof(buffer))) {
594 if(lua_getstack(L, 1, &ar)) { 605 strncat(buffer, filename, sizeof(buffer) - strlen(buffer));
595 lua_getinfo(L, "S", &ar);
596
597 /* Try determining the base path of the current Lua chunk
598 and prepend it to filename in buffer. */
599 make_path(buffer, sizeof(buffer), (char*)&ar.source[1], (char*)filename);
600 lf.f = rb->open(buffer, O_RDONLY); 606 lf.f = rb->open(buffer, O_RDONLY);
601 } 607 }
602 608