/*** Name of a terminal device. @function ttyname @see ttyname(3) @int[opt=0] fd file descriptor to process @return string name */ static int Pttyname(lua_State *L) { int fd=optint(L, 1, 0); checknargs(L, 1); return pushstringresult(ttyname(fd)); }
/*** Name of controlling terminal. @function ctermid @treturn string controlling terminal for current process @see ctermid(3) */ static int Pctermid(lua_State *L) { char b[L_ctermid]; checknargs(L, 0); return pushstringresult(ctermid(b)); }
/*** Current working directory for this process. @function getcwd @treturn[1] string path of current working directory, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see getcwd(3) */ static int Pgetcwd(lua_State *L) { #ifdef __GNU__ char *b = get_current_dir_name(); checknargs(L, 0); if (b == NULL) /* we return the same error as below */ return pusherror(L, "."); return pushstringresult(b); #else long size = pathconf(".", _PC_PATH_MAX); void *ud; lua_Alloc lalloc; char *b, *r; checknargs(L, 0); lalloc = lua_getallocf(L, &ud); if (size == -1) size = _POSIX_PATH_MAX; /* FIXME: Retry if this is not long enough */ if ((b = lalloc(ud, NULL, 0, (size_t)size + 1)) == NULL) return pusherror(L, "lalloc"); r = getcwd(b, (size_t)size); if (r != NULL) lua_pushstring(L, b); lalloc(ud, b, (size_t)size + 1, 0); return (r == NULL) ? pusherror(L, ".") : 1; #endif }
/*** Fetch the label for a soft label key. @function slk_label @int labnum @treturn string current label for *labnum* @see slk_label(3x) */ static int Pslk_label(lua_State *L) { int labnum = checkint(L, 1); #if LCURSES_POSIX_COMPLIANT return pushstringresult(slk_label(labnum)); #else return binding_notimplemented(L, "slk_label", "curses"); #endif }
/*** Get the name of a slave pseudo-terminal @function ptsname @int fd descriptor returned by @{openpt} @return[1] path name of the slave terminal device, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see ptsname(3) @see grantpt @see unlockpt */ static int Pptsname(lua_State *L) { int fd=checkint(L, 1); const char* slave; checknargs(L, 1); slave = ptsname(fd); if (!slave) return pusherror(L, "getptsname"); return pushstringresult(slave); }
/*** Write a time out according to a format. @function strftime @string format specifier with `%` place-holders @tparam PosixTm tm broken-down local time @treturn string *format* with place-holders plugged with *tm* values @see strftime(3) */ static int Pstrftime(lua_State *L) { char tmp[256]; const char *fmt = luaL_checkstring(L, 1); struct tm t; totm(L, 2, &t); checknargs(L, 2); strftime(tmp, sizeof(tmp), fmt, &t); return pushstringresult(tmp); }
/*** Encrypt a password. Not recommended for general encryption purposes. @function crypt @string trypass string to hash @string salt two-character string from [a-zA-Z0-9./] @return encrypted string @see crypt(3) @usage local salt, hash = pwent:match ":$6$(.-)$([^:]+)" if P.crypt (trypass, salt) ~= hash then error "wrong password" end */ static int Pcrypt(lua_State *L) { const char *str, *salt; char *r; str = luaL_checkstring(L, 1); salt = luaL_checkstring(L, 2); if (strlen(salt) < 2) luaL_error(L, "not enough salt"); checknargs(L, 2); r = crypt(str, salt); return pushstringresult(r); }
static int aux_files(lua_State *L) { DIR **p = (DIR **)lua_touserdata(L, lua_upvalueindex(1)); DIR *d = *p; struct dirent *entry; if (d == NULL) return 0; entry = readdir(d); if (entry == NULL) { closedir(d); *p=NULL; return 0; } return pushstringresult(entry->d_name); }
/*** Current logged-in user. @treturn[1] string username, if successful @return[2] nil @see getlogin(3) */ static int Pgetlogin(lua_State *L) { checknargs(L, 0); return pushstringresult(getlogin()); }
/*** Fetch the verbose name of the terminal. @function longname @treturn string verbose description of the current terminal @see longname(3x) */ static int Plongname(lua_State *L) { return pushstringresult(longname()); }
/*** Fetch the name of the terminal. @function termname @treturn string terminal name @see termname(3x) */ static int Ptermname(lua_State *L) { return pushstringresult(termname()); }
/*** Return a printable representation of a key. @function keyname @int c a key @treturn string key name of *c* @see keyname(3x) @see unctrl */ static int Pkeyname(lua_State *L) { int c = checkint(L, 1); return pushstringresult(keyname(c)); }
/*** Return a printable representation of a character, ignoring attributes. @function unctrl @int c character to act on @treturn string printable representation of *c* @see unctrl(3x) @see keyname */ static int Punctrl(lua_State *L) { chtype c = checkch(L, 1); return pushstringresult(unctrl(c)); }