-- /bin/ls - Lists files in a directory. -- -- Iterate over the 'children' map exposed via C++. local function format_permissions(perms) local rwx = { "-", "-", "-", "-", "-", "-", "-", "-", "-" } if(perms & 0x100) ~= 0 then rwx[1] = "r" end -- Owner read. if(perms & 0x080) ~= 0 then rwx[2] = "w" end -- Owner write. if(perms & 0x040) ~= 0 then rwx[3] = "x" end -- Owner execute. if(perms & 0x020) ~= 0 then rwx[4] = "r" end -- Group read. if(perms & 0x010) ~= 0 then rwx[5] = "w" end -- Group write. if(perms & 0x008) ~= 0 then rwx[6] = "x" end -- Group execute. if(perms & 0x004) ~= 0 then rwx[7] = "r" end -- Other read. if(perms & 0x002) ~= 0 then rwx[8] = "w" end -- Other write. if(perms & 0x001) ~= 0 then rwx[9] = "x" end -- Other execute. return table.concat(rwx) end local function get_file_size(node) if node.type == 0 then -- FILE_NODE. return #node.content else return 0 -- Dirs don't have content size in this context. end end local function ls_long_format(dir) local output = {} for name, node in pairs(dir.children) do local line_type = (node.type == 1) and "d" or "-" local perms = format_permissions(node.permissions) local owner = node.owner_id local group = node.group_id local size = get_file_size(node) table.insert(output, string.format("%s%s %d %d %5d %s", line_type, perms, owner, group, size, name)) end table.sort(output) return table.concat(output, "\n") end local function ls_short_format(dir) local output = {} for name, node in pairs(dir.children) do local display_name = name if node.type == 1 then -- DIR_NODE display_name = display_name .. "/" elseif node.type == 2 then --EXEC_NODE display_name = display_name .. "*" end table.insert(output, display_name) end table.sort(output) return table.concat(output, "\t") -- Tab separated short format. end local current_dir = bettola.get_current_dir(context); if arg[1] == "-l" then return ls_long_format(current_dir) else return ls_short_format(current_dir) end