File: test.lua

package info (click to toggle)
lua-posix 5.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 104 kB
  • ctags: 150
  • sloc: ansic: 1,144; makefile: 68; sh: 9
file content (213 lines) | stat: -rw-r--r-- 5,689 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
-- test posix library

require 'posix'

function testing(s)
 print""
 print("-------------------------------------------------------",s)
end

function myassert(w,c,s)
 if not c then print(w,s) end
 return c
end

function myprint(s,...)
 for i=1,table.getn(arg) do
  io.write(arg[i],s)
 end
 io.write"\n"
end

ox=posix

------------------------------------------------------------------------------
print(ox.version)

------------------------------------------------------------------------------
testing"uname"
function f(x) print(ox.uname(x)) end
f()
f("Machine %n is a %m running %s %r")

------------------------------------------------------------------------------
testing"terminal"
print(ox.getlogin(),ox.ttyname(),ox.ctermid(),ox.getenv"TERM")
print(ox.getlogin(),ox.ttyname(1),ox.ctermid(),ox.getenv"TERM")
print(ox.getlogin(),ox.ttyname(2),ox.ctermid(),ox.getenv"TERM")

------------------------------------------------------------------------------
testing"getenv"
function f(v) print(v,ox.getenv(v)) end
f"USER"
f"HOME"
f"SHELL"
f"absent"
for k,v in pairs(ox.getenv()) do print(k,'=',v) end io.write"\n"

------------------------------------------------------------------------------
testing"setenv"
function f(n,v) print("setenv",n,'=',v) ox.setenv(n,v) end
function g(n) print("now",n,'=',ox.getenv(n)) end
f("MYVAR","123");	g"MYVAR"
f("MYVAR",nil);	    g"MYVAR"
--f"MYVAR"	g"MYVAR"

------------------------------------------------------------------------------
testing"getcwd, chdir, mkdir, rmdir"
function f(d) myassert("chdir",ox.chdir(d)) g() end
function g() local d=ox.getcwd() print("now at",d) return d end
myassert("rmdir",ox.rmdir"x")
d=g()
f".."
f"xxx"
f"/etc/uucp"
f"/var/spool/mqueue"
f(d)
assert(ox.mkdir"x")
f"x"
f"../x"
assert("rmdir",ox.rmdir"../x")
myassert("mkdir",ox.mkdir".")
myassert("rmdir",ox.rmdir".")
g()
f(d)
myassert("rmdir",ox.rmdir"x")

------------------------------------------------------------------------------
testing"fork, exec"
io.flush()
pid=assert(ox.fork())
if pid==0 then
	pid=ox.getpid"pid"
	ppid=ox.getpid"ppid"
	io.write("in child process ",pid," from ",ppid,".\nnow executing date... ")
	io.flush()
	assert(ox.exec("date","+[%c]"))
	print"should not get here"
else
	io.write("process ",ox.getpid"pid"," forked child process ",pid,". waiting...\n")
	ox.wait(pid)
	io.write("child process ",pid," done\n")
end

------------------------------------------------------------------------------
testing"dir, stat"
function g() local d=ox.getcwd() print("now at",d) return d end
g()
for f in ox.files"." do
  local T=assert(ox.stat(f))
  local p=assert(ox.getpasswd(T.uid))
  local g=assert(ox.getgroup(T.gid))
  print(T.mode,p.name.."/"..g.name,T.size,os.date("%b %d %H:%M",T.mtime),f,T.type)
end

------------------------------------------------------------------------------
testing"glob"
function g() local d=ox.getcwd() print("now at",d) return d end
g()
for _,f in pairs(ox.glob "*.lua") do
  local T=assert(ox.stat(f))
  local p=assert(ox.getpasswd(T.uid))
  local g=assert(ox.getgroup(T.gid))
  print(T.mode,p.name.."/"..g.name,T.size,os.date("%b %d %H:%M",T.mtime),f,T.type)
end

------------------------------------------------------------------------------
testing"umask"
-- assert(not ox.access("xxx"),"`xxx' already exists")
ox.unlink"xxx"
print(ox.umask())
--print(ox.umask("a-r,o+w"))
print(ox.umask("a=r"))
print(ox.umask("o+w"))
io.close(io.open("xxx","w"))
os.execute"ls -l xxx"
ox.unlink"xxx"

------------------------------------------------------------------------------
testing"chmod, access"
ox.unlink"xxx"
print(ox.access("xxx"))
io.close(io.open("xxx","w"))
print(ox.access("xxx"))
os.execute"ls -l xxx"
print(ox.access("xxx","r"))
assert(ox.chmod("xxx","a-rwx,o+x"))
print(ox.access("xxx","r"))
os.execute"ls -l xxx"
ox.unlink"xxx"

------------------------------------------------------------------------------
testing"utime"
io.close(io.open("xxx","w"))
io.flush()
--os.execute"ls -l xxx test.lua"
os.execute( string.format("ls -l xxx %s", arg[0]))
--a=ox.stat"test.lua"
a=ox.stat( arg[0] )
ox.utime("xxx",a.mtime)
os.execute( string.format("ls -l xxx %s", arg[0]))
ox.unlink"xxx"
io.flush()

------------------------------------------------------------------------------
testing"links"
ox.unlink"xxx"
io.close(io.open("xxx","w"))
print(ox.link("xxx","yyy"))       --> hardlink
print(ox.link("xxx","zzz", true)) --> symlink
os.execute"ls -l xxx yyy zzz"
print("zzz ->",ox.readlink"zzz")
print("zzz ->",ox.readlink"xxx")
ox.unlink"xxx"
ox.unlink"yyy"
ox.unlink"zzz"

------------------------------------------------------------------------------
testing"getpasswd"
function f(x)
 local a
 if x==nil then a=ox.getpasswd() else a=ox.getpasswd(x) end
 if a==nil then
   print(x,"no such user")
  else
   myprint(":",a.name,a.passwd,a.uid,a.gid,a.gecos,a.dir,a.shell)
 end
end

f()
f(ox.getenv"USER")
f(ox.getenv"LOGNAME")
f"root"
f(0)
f(1234567)
f"xxx"
function f(x) print(ox.getpasswd(x,"name"),ox.getpasswd(x,"gecos")) end
f()
f(nil)
--ox.putenv"USER=root"
ox.setenv("USER","root")
f(ox.getenv"USER")

------------------------------------------------------------------------------
testing"sysconf"
a=ox.sysconf() table.foreach(a,print)
testing"pathconf"
a=ox.pathconf(".") table.foreach(a,print)

------------------------------------------------------------------------------
testing"times"
a=ox.times()
for k,v in pairs(a) do print(k,v) end
print"sleeping 4 seconds..."
ox.sleep(4)
b=ox.times()
for k,v in pairs(b) do print(k,v) end
print""
print("elapsed",b.elapsed-a.elapsed)
print("clock",os.clock())

------------------------------------------------------------------------------
print""
print(ox.version)