Blame ext/posix/dirent.c

Packit 437b5e
/*
Packit 437b5e
 * POSIX library for Lua 5.1, 5.2 & 5.3.
Packit 437b5e
 * (c) Gary V. Vaughan <gary@vaughan.pe>, 2013-2015
Packit 437b5e
 * (c) Reuben Thomas <rrt@sc3d.org> 2010-2013
Packit 437b5e
 * (c) Natanael Copa <natanael.copa@gmail.com> 2008-2010
Packit 437b5e
 * Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11
Packit 437b5e
 * Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> 07 Apr 2006 23:17:49
Packit 437b5e
 * Based on original by Claudio Terra for Lua 3.x.
Packit 437b5e
 * With contributions by Roberto Ierusalimschy.
Packit 437b5e
 * With documentation from Steve Donovan 2012
Packit 437b5e
 */
Packit 437b5e
/***
Packit 437b5e
 Directory Iterators.
Packit 437b5e
Packit 437b5e
@module posix.dirent
Packit 437b5e
*/
Packit 437b5e
Packit 437b5e
#include <config.h>
Packit 437b5e
Packit 437b5e
#include <dirent.h>
Packit 437b5e
Packit 437b5e
#include "_helpers.c"
Packit 437b5e
Packit 437b5e
Packit 437b5e
/***
Packit 437b5e
Contents of directory.
Packit 437b5e
@function dir
Packit 437b5e
@string[opt="."] path directory to act on
Packit 437b5e
@treturn table contents of *path*
Packit 437b5e
@see dir.lua
Packit 437b5e
*/
Packit 437b5e
static int
Packit 437b5e
Pdir(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	const char *path = optstring(L, 1, ".");
Packit 437b5e
	DIR *d;
Packit 437b5e
	checknargs(L, 1);
Packit 437b5e
	d = opendir(path);
Packit 437b5e
	/* Throw an argument error for consistency with eg. io.lines */
Packit 437b5e
	if (d == NULL)
Packit 437b5e
	{
Packit 437b5e
		const char *msg = strerror (errno);
Packit 437b5e
		msg = lua_pushfstring(L, "%s: %s", path, msg);
Packit 437b5e
		return luaL_argerror(L, 1, msg);
Packit 437b5e
	}
Packit 437b5e
	else
Packit 437b5e
	{
Packit 437b5e
		int i;
Packit 437b5e
		struct dirent *entry;
Packit 437b5e
		lua_newtable(L);
Packit 437b5e
		for (i=1; (entry = readdir(d)) != NULL; i++)
Packit 437b5e
		{
Packit 437b5e
			lua_pushstring(L, entry->d_name);
Packit 437b5e
			lua_rawseti(L, -2, i);
Packit 437b5e
		}
Packit 437b5e
		closedir(d);
Packit 437b5e
		lua_pushinteger(L, i-1);
Packit 437b5e
		return 2;
Packit 437b5e
	}
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
static int
Packit 437b5e
aux_files(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	DIR **p = (DIR **)lua_touserdata(L, lua_upvalueindex(1));
Packit 437b5e
	DIR *d = *p;
Packit 437b5e
	struct dirent *entry;
Packit 437b5e
	if (d == NULL)
Packit 437b5e
		return 0;
Packit 437b5e
	entry = readdir(d);
Packit 437b5e
	if (entry == NULL)
Packit 437b5e
	{
Packit 437b5e
		closedir(d);
Packit 437b5e
		*p=NULL;
Packit 437b5e
		return 0;
Packit 437b5e
	}
Packit 437b5e
	return pushstringresult(entry->d_name);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
static int
Packit 437b5e
dir_gc (lua_State *L)
Packit 437b5e
{
Packit 437b5e
	DIR *d = *(DIR **)lua_touserdata(L, 1);
Packit 437b5e
	if (d!=NULL)
Packit 437b5e
		closedir(d);
Packit 437b5e
	return 0;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
/***
Packit 437b5e
Iterator over all files in named directory.
Packit 437b5e
@function files
Packit 437b5e
@string[opt="."] path directory to act on
Packit 437b5e
@return an iterator
Packit 437b5e
*/
Packit 437b5e
static int
Packit 437b5e
Pfiles(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	const char *path = optstring(L, 1, ".");
Packit 437b5e
	DIR **d;
Packit 437b5e
	checknargs(L, 1);
Packit 437b5e
	d = (DIR **)lua_newuserdata(L, sizeof(DIR *));
Packit 437b5e
	*d = opendir(path);
Packit 437b5e
	/* Throw an argument error for consistency with eg. io.lines */
Packit 437b5e
	if (*d == NULL)
Packit 437b5e
	{
Packit 437b5e
		const char *msg = strerror (errno);
Packit 437b5e
		msg = lua_pushfstring(L, "%s: %s", path, msg);
Packit 437b5e
		return luaL_argerror(L, 1, msg);
Packit 437b5e
	}
Packit 437b5e
	if (luaL_newmetatable(L, PACKAGE_NAME " dir handle"))
Packit 437b5e
	{
Packit 437b5e
		lua_pushcfunction(L, dir_gc);
Packit 437b5e
		lua_setfield(L, -2, "__gc");
Packit 437b5e
	}
Packit 437b5e
	lua_setmetatable(L, -2);
Packit 437b5e
	lua_pushcclosure(L, aux_files, 1);
Packit 437b5e
	return 1;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
static const luaL_Reg posix_dirent_fns[] =
Packit 437b5e
{
Packit 437b5e
	LPOSIX_FUNC( Pdir		),
Packit 437b5e
	LPOSIX_FUNC( Pfiles		),
Packit 437b5e
	{NULL, NULL}
Packit 437b5e
};
Packit 437b5e
Packit 437b5e
Packit 437b5e
LUALIB_API int
Packit 437b5e
luaopen_posix_dirent(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	luaL_register(L, "posix.dirent", posix_dirent_fns);
Packit 437b5e
	lua_pushliteral(L, "posix.dirent for " LUA_VERSION " / " PACKAGE_STRING);
Packit 437b5e
	lua_setfield(L, -2, "version");
Packit 437b5e
Packit 437b5e
	return 1;
Packit 437b5e
}