擴充 API |
|
儘管完整的 POSIX 繫結確實能讓 Lua 程式撰寫成可以在許多平台上執行,但另一個重要的平台(Windows)將會排除在外。嘗試在 Windows 上嫁接類 POSIX API 不容易,也不乾淨。
以下是已知的函式庫、API 和系統的簡要清單和說明
--
以下是 ExtensionProposal 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']