mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-04 10:18:31 +00:00
Make more fixes and improvements
This commit is contained in:
parent
01b25e267b
commit
1599b818d9
24 changed files with 858 additions and 538 deletions
80
tool/net/demo/unix-info.lua
Normal file
80
tool/net/demo/unix-info.lua
Normal file
|
@ -0,0 +1,80 @@
|
|||
local unix = require 'unix'
|
||||
|
||||
Write('<!doctype html>\r\n')
|
||||
Write('<title>redbean</title>\r\n')
|
||||
Write('<h3>UNIX Information Demo</h3>\r\n')
|
||||
|
||||
Write('<dl>\r\n')
|
||||
Write('<dt>getuid()\r\n')
|
||||
Write(string.format('<dd>%d\r\n', unix.getuid()))
|
||||
Write('<dt>getgid()\r\n')
|
||||
Write(string.format('<dd>%d\r\n', unix.getgid()))
|
||||
Write('<dt>getpid()\r\n')
|
||||
Write(string.format('<dd>%d\r\n', unix.getpid()))
|
||||
Write('<dt>getppid()\r\n')
|
||||
Write(string.format('<dd>%d\r\n', unix.getppid()))
|
||||
Write('<dt>getpgrp()\r\n')
|
||||
Write(string.format('<dd>%d\r\n', unix.getpgrp()))
|
||||
|
||||
Write('<dt>getsid(0)\r\n')
|
||||
sid, errno = unix.getsid(0)
|
||||
if sid then
|
||||
Write(string.format('<dd>%d\r\n', sid))
|
||||
else
|
||||
Write(string.format('<dd>%s\r\n', EscapeHtml(unix.strerrno(errno))))
|
||||
end
|
||||
|
||||
Write('<dt>gethostname()\r\n')
|
||||
Write(string.format('<dd>%s\r\n', EscapeHtml(unix.gethostname())))
|
||||
Write('<dt>getcwd()\r\n')
|
||||
Write(string.format('<dd>%s\r\n', EscapeHtml(unix.getcwd())))
|
||||
|
||||
function PrintResourceLimit(name, id)
|
||||
soft, hard, errno = unix.getrlimit(id)
|
||||
Write(string.format('<dt>getrlimit(%s)\r\n', name))
|
||||
if soft then
|
||||
Write('<dd>')
|
||||
Write('soft ')
|
||||
if soft == -1 then
|
||||
Write('∞')
|
||||
else
|
||||
Write(string.format('%d', soft))
|
||||
end
|
||||
Write('<br>\r\n')
|
||||
Write('hard ')
|
||||
if hard == -1 then
|
||||
Write('∞')
|
||||
else
|
||||
Write(string.format('%d', hard))
|
||||
end
|
||||
Write('\r\n')
|
||||
else
|
||||
Write(string.format('<dd>%s\r\n', EscapeHtml(unix.strerrno(errno))))
|
||||
end
|
||||
end
|
||||
PrintResourceLimit('RLIMIT_AS', unix.RLIMIT_AS)
|
||||
PrintResourceLimit('RLIMIT_RSS', unix.RLIMIT_RSS)
|
||||
PrintResourceLimit('RLIMIT_CPU', unix.RLIMIT_CPU)
|
||||
PrintResourceLimit('RLIMIT_FSIZE', unix.RLIMIT_FSIZE)
|
||||
PrintResourceLimit('RLIMIT_NPROC', unix.RLIMIT_NPROC)
|
||||
PrintResourceLimit('RLIMIT_NOFILE', unix.RLIMIT_NOFILE)
|
||||
|
||||
Write('<dt>siocgifconf()\r\n')
|
||||
Write('<dd>\r\n')
|
||||
ifs, errno = unix.siocgifconf()
|
||||
if ifs then
|
||||
for i = 1,#ifs do
|
||||
if ifs[i].netmask ~= 0 then
|
||||
cidr = 32 - Bsf(ifs[i].netmask)
|
||||
else
|
||||
cidr = 0
|
||||
end
|
||||
Write(string.format('%s %s/%d<br>\r\n',
|
||||
EscapeHtml(ifs[i].name),
|
||||
FormatIp(ifs[i].ip),
|
||||
cidr))
|
||||
end
|
||||
else
|
||||
Write(string.format('%s\r\n', EscapeHtml(unix.strerrno(errno))))
|
||||
end
|
||||
Write('</dl>\r\n')
|
44
tool/net/demo/unix-subprocess.lua
Normal file
44
tool/net/demo/unix-subprocess.lua
Normal file
|
@ -0,0 +1,44 @@
|
|||
-- example of how to run the ls command
|
||||
-- and pipe its output to the http user
|
||||
local unix = require "unix"
|
||||
function main()
|
||||
syscall = 'commandv'
|
||||
ls, errno = unix.commandv("ls")
|
||||
if ls then
|
||||
syscall = 'pipe'
|
||||
reader, writer, errno = unix.pipe()
|
||||
if reader then
|
||||
syscall = 'fork'
|
||||
child, errno = unix.fork()
|
||||
if child then
|
||||
if child == 0 then
|
||||
unix.close(1)
|
||||
unix.dup(writer)
|
||||
unix.close(writer)
|
||||
unix.close(reader)
|
||||
unix.execve(ls, {ls, "-Shal"})
|
||||
unix.exit(127)
|
||||
else
|
||||
unix.close(writer)
|
||||
SetStatus(200)
|
||||
SetHeader('Content-Type', 'text/plain')
|
||||
while true do
|
||||
data = unix.read(reader)
|
||||
if data ~= "" then
|
||||
Write(data)
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
unix.close(reader)
|
||||
unix.wait(-1)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
SetStatus(200)
|
||||
SetHeader('Content-Type', 'text/plain')
|
||||
Write(string.format('error %s calling %s()', unix.strerrno(errno), syscall))
|
||||
end
|
||||
main()
|
|
@ -1,26 +0,0 @@
|
|||
-- example of how to run the ls command
|
||||
-- and pipe its output to the http user
|
||||
local unix = require "unix"
|
||||
ls = unix.commandv("ls")
|
||||
reader, writer = unix.pipe()
|
||||
if unix.fork() == 0 then
|
||||
unix.close(1)
|
||||
unix.dup(writer)
|
||||
unix.close(writer)
|
||||
unix.close(reader)
|
||||
unix.execve(ls, {ls, "-Shal"})
|
||||
unix.exit(127)
|
||||
else
|
||||
unix.close(writer)
|
||||
SetHeader('Content-Type', 'text/plain')
|
||||
while true do
|
||||
data = unix.read(reader)
|
||||
if data ~= "" then
|
||||
Write(data)
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
unix.close(reader)
|
||||
unix.wait(-1)
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue