home / software

pozix.c


Download

Download ztact-lua.zip.
View pozix.c

Overview

The pozix.c module provides direct (and in a few cases extended) access to useful POSIX functionality.

Here are examples of the various ways to use those few pozix functions that have been extended beyond the raw POSIX/C API.



-- public domain 20080407 lua@ztact.com


require 'pozix'


-- opendir ------------------------------------------------------------ opendir

        -- two ways to use opendir, readdir and closedir

local dir = pozix.opendir ('.')
print (dir)    -- dir is userdata
repeat
  local name = pozix.readdir (dir)
  if not name then  break  end
  print (name)
  until not name
pozix.closedir (dir)

dir = pozix.opendir ('.')
for name in dir do
  print (name)
  end
dir:close ()

        -- or even more concisely
for name in pozix.opendir ('.') do  print (name)  end


-- stat ------------------------------------------------------------------ stat

        -- two ways to use stat

local stat = pozix.stat ('.')
print (stat)    -- stat is userdata
print (stat.mtime, stat.is_dir)

local mtime, is_dir = pozix.stat ('.', 'mtime', 'is_dir')
print (mtime, is_dir)

Additional information

An important question to consider when deciding how to get access to POSIX functionality in Lua is whether you want high-level, "Lua-style" access or low-level, "C-style" access.

I initally started writing pozix.c to provide raw, literal, direct access to POSIX functions. Indeed, most pozix function do provide straight access to the underlying POSIX functions. In select cases, however, the pozix provided functions have been extended to be more flexible and Lua-friendly.

pozix.c is spelled with a "z" to avoid conflict with lhf's lposix library.

For a higher-level API, consider using pzp.lua, a reimplementation of popular PHP functions.

For even higher level programming, consider writing shell scripts in Luashell.

Functionality will be added to pozix.c, pzp.lua and Luashell on an "as-needed-by-me or as-submitted-by-others" basis.