summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTitanSnow <[email protected]>2017-05-22 18:33:04 +0800
committerTitanSnow <[email protected]>2017-05-22 18:37:43 +0800
commitb8e4fb467d8de881749e3bc77493728a642ff145 (patch)
treee8a60ea6e8c14d15a8044e95b4ff566bf36d44ee
parentd4aab4ff42e4032f848e0c6f048aaa61cb7db18b (diff)
add os.getown
-rw-r--r--core/src/xmake/machine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/os/getown.c71
3 files changed, 74 insertions, 0 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index a1ae497ec..ccfd91254 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -84,6 +84,7 @@ tb_int_t xm_os_versioninfo(lua_State* lua);
#ifndef TB_CONFIG_OS_WINDOWS
tb_int_t xm_os_uid(lua_State* lua);
tb_int_t xm_os_gid(lua_State* lua);
+tb_int_t xm_os_getown(lua_State* lua);
#endif
// the path functions
@@ -148,6 +149,7 @@ static luaL_Reg const g_os_functions[] =
#ifndef TB_CONFIG_OS_WINDOWS
, { "uid", xm_os_uid }
, { "gid", xm_os_gid }
+, { "getown", xm_os_getown }
#endif
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index f2be6405d..c7b1ffc29 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -39,6 +39,7 @@ xmake_C_FILES += \
os/versioninfo \
os/uid \
os/gid \
+ os/getown \
path/relative \
path/absolute \
path/translate \
diff --git a/core/src/xmake/os/getown.c b/core/src/xmake/os/getown.c
new file mode 100644
index 000000000..aa4e2de66
--- /dev/null
+++ b/core/src/xmake/os/getown.c
@@ -0,0 +1,71 @@
+/*!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 - 2017, TBOOX Open Source Group.
+ *
+ * @author TitanSnow
+ * @file getown.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "getown"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+#ifndef TB_CONFIG_OS_WINDOWS
+# include <unistd.h>
+# include <sys/stat.h>
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+// get owner by a given path
+tb_int_t xm_os_getown(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the pathname
+ tb_char_t const* pathname = luaL_checkstring(lua, 1);
+ tb_check_return_val(pathname, 0);
+
+ // get stat
+ struct stat sts;
+ if(stat(pathname, &sts) != 0)
+ return 0;
+
+ // push
+ lua_newtable(lua);
+ lua_pushstring(lua, "uid");
+ lua_pushinteger(lua, sts.st_uid);
+ lua_settable(lua, -3);
+ lua_pushstring(lua, "gid");
+ lua_pushinteger(lua, sts.st_gid);
+ lua_settable(lua, -3);
+
+ // ok
+ return 1;
+}
+
+#endif