Advertisement
Guest User

02_os.lua

a guest
Mar 28th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. local computer = require("computer")
  2. local event = require("event")
  3. local fs = require("filesystem")
  4. local shell = require("shell")
  5. local unicode = require("unicode")
  6. local process = require("process")
  7.  
  8. local function env()
  9.   return process.info().data.vars
  10. end
  11.  
  12. os.execute = function(command)
  13.   if not command then
  14.     return type(shell) == "table"
  15.   end
  16.   return shell.execute(command)
  17. end
  18.  
  19. function os.exit(code)
  20.   error({reason="terminated", code=code}, 0)
  21. end
  22.  
  23. function os.getenv(varname)
  24.   local env = env()
  25.   if not varname then
  26.     return env
  27.   end
  28.   return env[varname]
  29. end
  30.  
  31. function os.setenv(varname, value)
  32.   checkArg(1, varname, "string", "number")
  33.   if value == nil then
  34.     env()[varname] = nil
  35.   else
  36.     local success, val = pcall(tostring, value)
  37.     if success then
  38.       env()[varname] = val
  39.       return val
  40.     else
  41.       return nil, val
  42.     end
  43.   end
  44. end
  45.  
  46. function os.remove(...)
  47.   return fs.remove(...)
  48. end
  49.  
  50. function os.rename(...)
  51.   return fs.rename(...)
  52. end
  53.  
  54. function os.sleep(timeout)
  55.   checkArg(1, timeout, "number", "nil")
  56.   local deadline = computer.uptime() + (timeout or 0)
  57.   repeat
  58.     event.pull(deadline - computer.uptime())
  59.   until computer.uptime() >= deadline
  60. end
  61.  
  62. function os.tmpname()
  63.   local path = os.getenv("TMPDIR") or "/tmp"
  64.   if fs.exists(path) then
  65.     for i = 1, 10 do
  66.       local name = fs.concat(path, tostring(math.random(1, 0x7FFFFFFF)))
  67.       if not fs.exists(name) then
  68.         return name
  69.       end
  70.     end
  71.   end
  72. end
  73.  
  74. os.setenv("PATH", "/bin:/usr/bin:/home/bin:.")
  75. os.setenv("TMP", "/tmp") -- Deprecated
  76. os.setenv("TMPDIR", "/tmp")
  77.  
  78. if computer.tmpAddress() then
  79.   fs.mount(computer.tmpAddress(), os.getenv("TMPDIR") or "/tmp")
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement