summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2018-10-03 00:38:54 +0800
committerruki <[email protected]>2018-10-02 21:53:26 +0800
commit7ab9409d3cedff94974cfa65fc9be31e54733062 (patch)
treeefce6da2261d65bcde0e3d7d41a2c0a12769ad73 /core/src
parent4248b37344ac119834ed633fabe94673caeb46d0 (diff)
add os.readlink
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/machine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/os/readlink.c84
3 files changed, 87 insertions, 0 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index f54ab9df3..18fb95061 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -79,6 +79,7 @@ tb_int_t xm_os_rename(lua_State* lua);
tb_int_t xm_os_exists(lua_State* lua);
tb_int_t xm_os_setenv(lua_State* lua);
tb_int_t xm_os_getenv(lua_State* lua);
+tb_int_t xm_os_readlink(lua_State* lua);
tb_int_t xm_os_filesize(lua_State* lua);
tb_int_t xm_os_emptydir(lua_State* lua);
tb_int_t xm_os_strerror(lua_State* lua);
@@ -170,6 +171,7 @@ static luaL_Reg const g_os_functions[] =
, { "exists", xm_os_exists }
, { "setenv", xm_os_setenv }
, { "getenv", xm_os_getenv }
+, { "readlink", xm_os_readlink }
, { "emptydir", xm_os_emptydir }
, { "strerror", xm_os_strerror }
, { "filesize", xm_os_filesize }
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index cd05a8b6a..1471c75ef 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -35,6 +35,7 @@ xmake_C_FILES += \
os/exists \
os/setenv \
os/getenv \
+ os/readlink \
os/emptydir \
os/strerror \
os/filesize \
diff --git a/core/src/xmake/os/readlink.c b/core/src/xmake/os/readlink.c
new file mode 100644
index 000000000..6b1a6b8ae
--- /dev/null
+++ b/core/src/xmake/os/readlink.c
@@ -0,0 +1,84 @@
+/*!The Make-like Build Utility based on Lua
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015 - 2018, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file readlink.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "readlink"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+#ifndef TB_CONFIG_OS_WINDOWS
+# include <unistd.h>
+#endif
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_os_readlink(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the path
+ tb_char_t const* path = luaL_checkstring(lua, 1);
+ tb_check_return_val(path, 0);
+
+ // is link?
+#if defined(TB_CONFIG_OS_WINDOWS)
+ lua_pushnil(lua);
+#else
+ tb_char_t srcpath[TB_PATH_MAXN];
+ tb_long_t size = readlink(path, srcpath, TB_PATH_MAXN);
+ if (size == TB_PATH_MAXN)
+ {
+ tb_size_t maxn = TB_PATH_MAXN * 2;
+ tb_char_t* data = (tb_char_t*)tb_malloc(maxn);
+ if (data)
+ {
+ tb_long_t size = readlink(path, data, maxn);
+ if (size > 0 && size < maxn)
+ {
+ data[size] = '\0';
+ lua_pushstring(lua, data);
+ }
+ else lua_pushnil(lua);
+ tb_free(data);
+ }
+ }
+ else if (size >= 0 && size < TB_PATH_MAXN)
+ {
+ srcpath[size] = '\0';
+ lua_pushstring(lua, srcpath);
+ }
+ else lua_pushnil(lua);
+#endif
+
+ // ok
+ return 1;
+}