dirname

function dirname( path ) --> parent

Description

Returns the parent directory’s path of the file or directory that is defined with path.

Parameters

path

A unix like path.

Return Values

parent

Directory name of the path.

Code

--ZFUNC-dirname-v1
local function dirname( path ) --> parent

   local i = #path
   local c = string.sub( path, i, i )
   if c == "/" and #path >= 1 then
      i = i - 1
      c = string.sub( path, i, i )
   end

   while i > 0 and c ~= "/" do
      i = i - 1
      c = string.sub( path, i, i )
   end

   if i == 0 then
      return path
   elseif i == 1 then -- root case
      return string.sub( path, 1, 1 )
   else
      return string.sub( path, 1, i-1 )
   end
end

return dirname

Examples

local t = require( "taptest" )
local dirname = require( "dirname" )

path = "/foo/bar/baz/asdf/quux.html"
t( dirname( path ), "/foo/bar/baz/asdf" )

t( dirname( "/etc/passwd" ), "/etc" )
t( dirname( "/etc/" ), "/" )
t( dirname( "/" ), "/" )
t( dirname( "." ), "." )

t()