扩展 API |
|
虽然完整的 POSIX 绑定确实可以让 Lua 程序在许多平台上编写,但至少一个主要平台(Windows)会被排除在外。在 Windows 之上嫁接类似 POSIX 的 API 既不容易也不干净。
以下是已知库、API 和系统的简要列表和描述
--
以下是 扩展提案 API 与 [lposix] 和 [LuaFileSystem] 的比较。
-- get environment variable os.getenv posix.getenv -- set/unset environment variable os.setenv posix.putenv posix.setenv posix.unsetenv -- sleep (pause without consuming CPU) os.sleep posix.sleep --[[ os.sleep specifies that the implementation provide sub-second resolution if available. posix.sleep provides only second resolution. --]] -- system information posix.ctermid posix.errno posix.pathconf posix.sysconf posix.ttyname posix.uname
-- get/set current directory os.currentdir posix.getcwd lfs.currentdir os.chdir posix.chdir lfs.chdir -- create/delete directories os.mkdir posix.mkdir lfs.mkdir os.remove posix.rmdir lfs.rmdir --[[ In both "ex" and POSIX systems, os.remove() will remove any directory entry: both files (non-directories) and empty subdirectories. The Lua 5.1 reference says that the standard os.remove() function will remove a directory, but in fact the Microsoft MSVCRT implementation of the C remove() function (on which os.remove() is based) will not remove directories. --]] -- POSIX directory routines posix.link posix.unlink posix.mkfifo posix.readlink posix.symlink
-- list directory os.dir posix.files lfs.dir posix.dir -- get file attributes os.dirent posix.stat lfs.attributes --[[ The "ex" os.dir() iterator returns a table of directory entry attributes including the name, while the posix.files() and lfs.dir() iterators return entry names only. The best comparison of these three functions is via example: --]] require"ex" for e in os.dir() do print(e.name, e.size) end require"lfs" for name in lfs.dir() do if name~="." and name~=".." then print(name, lfs.attributes(name).size) end end require"posix" for name in posix.files() do if name~="." and name~=".." then print(name, posix.stat(name, "size")) end end --[[ os.dir() elides any "." and ".." names while lfs.dir(), posix.dir() and posix.files() include them. posix.dir() is not an iterator; it returns a table with all entry names. --]] -- set file attributes posix.utime lfs.touch posix.chmod posix.chown --[[ The "ex" API says only that the os.dirent() table can be extended with OS-specific attribute values. It would not be unreasonable to add file date information to the standard fields: --]] local age = os.difftime(os.time(), e.modified) e.modified = os.time() -- touch a file -- Check permissions posix.access
-- file locking file:lock lfs.lock file:unlock lfs.unlock -- create anonymous pipe io.pipe
-- spawn process os.spawn posix.fork proc:wait posix.exec posix.wait --[[ os.spawn supports redirection for standard I/O streams (in, out, err) while lposix does not provide full integration with POSIX-style descriptors and therefore does not provide a bind for the POSIX dup() interface. --]] -- signal a process posix.kill -- process information posix.getprocessid posix.getgroup posix.getlogin posix.getpasswd posix.setgid posix.setuid posix.times posix.umask
请注意,“ex” API 部分基于 LuaFileSystem,其实现使用了一些来自它的代码。
> py = require "python" > os = py.import("os") > =os.listdir('.') ['lua.exe', 'lua51.dll', 'luac.exe']