-- public domain 20080404 lua@ztact.com require 'pozix' -- stat do print ("stat ('Makefile')"); print ('', pozix.stat ('Makefile', 'atime', 'ctime', 'mode')); local stat = pozix.stat ('Makefile'); print ('', 'stat', stat); print ('', 'atime', stat.atime); print ('', 'ctime', stat.ctime); print ('', 'dev', stat.dev); print ('', 'gid', stat.gid); print ('', 'ino', stat.ino); print ('', 'mode', stat.mode); print ('', 'mtime', stat.mtime); print ('', 'nlink', stat.nlink); print ('', 'size', stat.size); print ('', 'uid', stat.uid); print ('', 'is_block', stat.is_block); print ('', 'is_char', stat.is_char); print ('', 'is_dir', stat.is_dir); print ('', 'is_fifo', stat.is_fifo); print ('', 'is_link', stat.is_link); print ('', 'is_reg', stat.is_reg); print ('', 'is_sgid', stat.is_sgid); print ('', 'is_socket', stat.is_socket); print ('', 'is_sticky', stat.is_sticky); print ('', 'is_suid', stat.is_suid); end os.exit (0); print 'pid, ppid' pid = pozix.getpid () ppid = pozix.getppid (); print ('', pid, 'getpid', pid); print ('', pid, 'getppid', ppid); print '' -- opendir, readdir, closedir do print 'opendir, readdir, closedir' local dir = pozix.opendir ('.') print ('', 'opendir', dir) repeat local name = dir:read () print ('', 'readdir', name) until not name dir:close () dir:close () dir:close () end -- stat do print 'stat' stat = pozix.stat ('nonexistent') print ('', pid, 'stat', stat) stat = pozix.stat ('.', pozix.st_mode) print ('', pid, 'stat', stat) for i,v in pairs { 'S_ISBLK', 'S_ISCHR', 'S_ISDIR', 'S_ISFIFO', 'S_ISLNK', 'S_ISREG', 'S_ISSOCK' } do print ('', pid, v, pozix[v] ()) end mode = pozix.stat ('test.lua', 'mode') print ('', pid, 'mode', mode) print '' end print 'getenv, setenv' getenv = pozix.getenv ('PATH'); print ('', pid, 'getenv', getenv); setenv = pozix.setenv ('FOO', 'BAR'); print ('', pid, 'setenv', setenv); getenv = pozix.getenv ('FOO'); print ('', pid, 'getenv', getenv); print '' print 'fork, wait, exec' fork = pozix.fork () print ('', pid, 'fork', fork) if fork == 0 then pid = pozix.getpid () ppid = pozix.getppid (); print ('', pid, 'getpid', pid); print ('', pid, 'getppid', ppid); print ('', pid, 'exit', 0) os.exit (0) end wait, status = pozix.wait () print ('', pid, 'wait', wait, status) t = {} for i,v in pairs { 'WIFEXITED', 'WEXITSTATUS', 'WIFSIGNALED', 'WTERMSIG', 'WIFSTOPPED', 'WSTOPSIG' } do t[v] = pozix[v] (status) end t = { { 'WIFEXITED', 'WEXITSTATUS' }, { 'WIFSIGNALED', 'WTERMSIG' }, { 'WIFSTOPPED', 'WSTOPSIG' }, } for i,tuple in ipairs (t) do local pri, sec = unpack (tuple) print ('', pid, pri, pozix[pri] (status)) if pozix[pri] (status) then print ('', pid, sec, pozix[sec] (status)) end end print '' print 'fork, exec /bin/ls, and wait' fork = pozix.fork () pid = pozix.getpid () print ('', pid, 'fork', fork) if fork == 0 then pozix.execv ('/bin/ls', {'ls'}) end wait, status = pozix.wait () print ('', pid, 'wait', wait, status) print 'fork, exec /bin/false, and wait' fork = pozix.fork () pid = pozix.getpid () print ('', pid, 'fork', fork) if fork == 0 then pozix.execv ('/bin/false', {'false'}) end wait, status = pozix.wait () print ('', pid, 'wait', wait, status)