Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 0.89 KB | Hits: 59 | Expires: Never
Copy text to clipboard
  1. local alien = require 'alien'
  2. local structs = require 'structs'
  3. local struct = require 'alien.struct'
  4.  
  5. local function listdir(path)
  6.   local result = {}
  7.   local libc = alien.default
  8.   libc.strerror:types("string", "int")
  9.   libc.opendir:types("pointer", "string")
  10.   libc.readdir:types("pointer", "pointer")
  11.   libc.closedir:types("int", "pointer")
  12.  
  13.   local dirh = libc.opendir(path)
  14.   if (dirh == nil) then
  15.     err = alien.errno()
  16.     merr = libc.strerror(err)
  17.     log.crit("opendir('%s') returned: errno=(%d) %s", tostring(path), err, merr)
  18.     error("Failed to open: ".. path)
  19.   end
  20.  
  21.   while true do
  22.     dp = libc.readdir(dirh)
  23.     if dp == nil then
  24.       break
  25.     end
  26.    
  27.     local s = struct.unpack("s", dp, structs.dirent.__size, structs.dirent.d_name + 1)
  28.     log.crit("str: %d: %s", structs.dirent.d_name, s)
  29.     table.insert(result, s)
  30.   end
  31.  
  32.   return result
  33. end