summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Ipsum <richardipsum@fastmail.co.uk>2018-10-12 18:10:52 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2018-10-21 13:39:31 +0100
commit4e95fbccfbbec3c1ebc10dc156a154a2953200dd (patch)
tree825e257955beae7640e280a02098a423fbc3bacf
parent6cdd1f9a9f10ae7fb63441341727a367ca4c5475 (diff)
downloadluxio-4e95fbccfbbec3c1ebc10dc156a154a2953200dd.tar.gz
Bind termident functions
-rw-r--r--luxio.c36
-rw-r--r--luxio_constants.inc.in3
-rw-r--r--tests/test-termident.lua17
3 files changed, 52 insertions, 4 deletions
diff --git a/luxio.c b/luxio.c
index 7016385..6507a00 100644
--- a/luxio.c
+++ b/luxio.c
@@ -980,8 +980,34 @@ luxio_unsetenv(lua_State *L) /* POSIX.1-2001 */
/* 4.7 Terminal identification ***********************************************/
-/* TODO: ctermid() 4.7.1 */
-/* TODO: ttyname(), ttyname_r(), isatty() 4.7.2 */
+static int
+luxio_ctermid(lua_State *L)
+{
+ lua_pushstring(L, ctermid(NULL));
+ return 1;
+}
+
+static int
+luxio_ttyname(lua_State *L)
+{
+ int fd = luaL_checkinteger(L, 1);
+
+ lua_pushstring(L, ttyname(fd));
+ lua_pushinteger(L, errno);
+
+ return 2;
+}
+
+static int
+luxio_isatty(lua_State *L)
+{
+ int fd = luaL_checkinteger(L, 1);
+
+ lua_pushboolean(L, isatty(fd));
+ lua_pushinteger(L, errno);
+
+ return 2;
+}
/* 4.8 Configurable system variables *****************************************/
@@ -4557,11 +4583,15 @@ luxio_functions[] = {
{ "iconv_open", luxio_iconv_open },
{ "iconv_close", luxio_iconv_close },
{ "iconv", luxio_iconv },
-
+
#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L
{ "posix_fadvise", luxio_posix_fadvise },
#endif
+ { "ctermid", luxio_ctermid },
+ { "ttyname", luxio_ttyname },
+ { "isatty", luxio_isatty },
+
{ NULL, NULL }
};
diff --git a/luxio_constants.inc.in b/luxio_constants.inc.in
index d96ee67..ca045da 100644
--- a/luxio_constants.inc.in
+++ b/luxio_constants.inc.in
@@ -17,7 +17,7 @@ static const struct {
E(EEXIST),
E(EBADF),
E(EFAULT),
- E(EFBIG),
+ E(EFBIG),
E(EINTR),
E(EIO),
E(EISDIR),
@@ -34,6 +34,7 @@ static const struct {
E(ENOTCONN),
E(ENOTDIR),
E(ENOTSOCK),
+ E(ENOTTY),
E(ENXIO),
E(EOVERFLOW),
E(EPERM),
diff --git a/tests/test-termident.lua b/tests/test-termident.lua
new file mode 100644
index 0000000..5cde143
--- /dev/null
+++ b/tests/test-termident.lua
@@ -0,0 +1,17 @@
+l = require "luxio"
+
+print("ctermid()", l.ctermid())
+print("ttyname(STDIN_FILENO)", l.ttyname(l.STDIN_FILENO))
+print("ttyname(STDOUT_FILENO)", l.ttyname(l.STDOUT_FILENO))
+
+local tfd, tpath = l.mkstemp(nil)
+
+print("isatty(STDIN_FILENO)", l.isatty(l.STDIN_FILENO))
+
+istty, what = l.isatty(tfd)
+
+print(("isatty(%s)"):format(tpath), istty, l.strerror(what))
+
+print(l.strerror(l.ENOTTY))
+
+l.unlink(tpath)