require"posix" fs = posix fs.cwd = posix.getcwd() -------------------------------------------------------------------------------------------------------------------------------------------------------- ---- Directory Functions ---- -------------------------------------------------------------------------------------------------------------------------------------------------------- -- Removes all traces of "~", and replace with full path to user's home directory. function fs.remhomedir(dir) if string.sub(dir,1,1) == "~" then dir = posix.getenv("HOME")..string.sub(dir,2) end -- If first chracter is "~" then replace it with the home directory return dir end -- Gets dir path with ~ if appropriate function fs.checkhomedir(dir) local dir = fs.relativetoexact -- Get exact directory local home = posix.getenv("HOME") -- Get current home directory if string.sub(dir,1,string.len(home)) == home then -- If Beginning of exact dir is equal to home directory: dir = "~"..string.sub(dir,string.len(home)) -- Dir is: ~/HOMEDIR end return dir end -- Changes directory. Accepts relative directory to change TO. -- Returns new directory's full path OR error & code. function fs.cd (dir) dir = fs.remhomedir(dir) -- Convert ~ to home directory local worked, error, code = posix.chdir(dir) if error then return error, code else fs.cwd = posix.getcwd() return fs.cwd end end -- Lists the contents of directory "dir" into a table. Accepts relative directory to list. --Returns the table, and the full path to the directory OR error & code. function fs.dir(dir) if dir == "." or nil then -- If no parameter passed, or current directory selected else dir = fs.remhomedir(dir) -- Convert ~ to home directory local olddir = fs.cwd -- Save current/Old Directory local i, j = fs.cd(dir) if j then return i, j end -- Change to directory to be listed. If you can't get into the directory, error. end local ttable = {} local i = 0 for file in posix.files() do if i > 0 then -- Skip First entry (".") local filestat = posix.stat(file) ttable[i] = {} if filestat.type == "directory" then ttable[i].dir = true else ttable[i].dir = false end -- Change directory to a boolean ttable[i].name = file ttable[i].size = filestat.size ttable[i].perms = filestat.mode ttable[i].mtime = filestat.mtime ttable[i].uid = posix.getpasswd(filestat.uid) ttable[i].gid = posix.getgroup(filestat.gid) end i = i + 1 end local i, j = fs.cd(olddir) if j then return i, j end -- Change back to original directory. If you can't get into the directory, error. return ttable, dir -- If all worked, return directory table and directory thats been listed. end -- Creates a directory. Accepts relative directory to list. --Returns the full path to the new directory OR error & code. function fs.mkdir(dir) dir = fs.remhomedir(dir) -- Convert ~ to home directory local worked, error, code = posix.mkdir(dir) if error then return error, code else return fs.cwd..dir end end fs.md = fs.mkdir -- Make an alias (md) -- Removes a directory. Accepts relative directory to list, and whether to be Recursive (boolean). --Returns the full path to the current directory OR error & code. function fs.rmdir(dir, recur) if recur == nil then recur = false end dir = fs.remhomedir(dir) -- Convert ~ to home directory local worked, error, code = posix.rmdir(dir) if error then return error, code else return fs.cwd end end fs.rd = fs.rmdir -- Make an alias (rd) -- Converts a relative path to an exact one (ugly hack) function fs.relativetoexact(dir) olddir = posix.getcwd() -- Save the old/current directory path local i, j = fs.cd(dir) if j then return i, j end -- Change to directory to be checked. If you can't get into the directory, error. newdir = posix.getcwd() -- Get new directory's exact path local i, j = fs.cd(olddir) if j then return i, j end -- Change back to old directory. If you can't get into the directory, error. return newdir -- Return exact path of specified directory end -- Other, imported functions from posix: -- posix.link(,) -- Creates a link -- posix.symlink(,) -- Creates a symlink