Comparison

util-src/pposix.c @ 11656:c368b4f6ee04

util.pposix: Bind isatty(3) Useful for disabling certain behavior, ANSI colors etc when not connected to a terminal.
author Kim Alvefur <zash@zash.se>
date Sun, 04 Jul 2021 15:11:07 +0200
parent 10921:6eb5d2bb11af
child 12154:760dd1fc3dc1
comparison
equal deleted inserted replaced
11655:bbf50525faa5 11656:c368b4f6ee04
810 lua_pushstring(L, strerror(err)); 810 lua_pushstring(L, strerror(err));
811 lua_pushinteger(L, err); 811 lua_pushinteger(L, err);
812 return 3; 812 return 3;
813 } 813 }
814 814
815 static int lc_isatty(lua_State *L) {
816 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
817 const int fd = fileno(f);
818 lua_pushboolean(L, isatty(fd));
819 return 1;
820 }
821
815 /* Register functions */ 822 /* Register functions */
816 823
817 int luaopen_util_pposix(lua_State *L) { 824 int luaopen_util_pposix(lua_State *L) {
818 #if (LUA_VERSION_NUM > 501) 825 #if (LUA_VERSION_NUM > 501)
819 luaL_checkversion(L); 826 luaL_checkversion(L);
851 { "meminfo", lc_meminfo }, 858 { "meminfo", lc_meminfo },
852 #endif 859 #endif
853 860
854 { "atomic_append", lc_atomic_append }, 861 { "atomic_append", lc_atomic_append },
855 862
863 { "isatty", lc_isatty },
864
856 { NULL, NULL } 865 { NULL, NULL }
857 }; 866 };
858 867
859 lua_newtable(L); 868 lua_newtable(L);
860 luaL_setfuncs(L, exports, 0); 869 luaL_setfuncs(L, exports, 0);