From 1471b7eeadb60f2666a4bfc02913063a798ea1c2 Mon Sep 17 00:00:00 2001 From: perdo Date: Fri, 20 Jul 2012 11:15:55 +0000 Subject: [PATCH] Add includes from the original lfs.c code, fix formatting (spaces instead of tabs). --- nse_fs.cc | 248 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 131 insertions(+), 117 deletions(-) diff --git a/nse_fs.cc b/nse_fs.cc index b127fa5fd..c091dc8d1 100644 --- a/nse_fs.cc +++ b/nse_fs.cc @@ -36,6 +36,32 @@ #define _LARGEFILE64_SOURCE +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#include +#include +#include +#ifdef __BORLANDC__ + #include +#else + #include +#endif +#include +#else +#include +#include +#include +#include +#include +#endif + extern "C" { #include "lauxlib.h" #include "lua.h" @@ -47,20 +73,8 @@ extern "C" { #include "nmap_error.h" #include "NmapOps.h" -#include -#include -#include - #include -enum { - DIR_METATABLE = lua_upvalueindex(1), -}; - -#ifndef WIN32 -#include "dirent.h" -#endif - #ifndef MAX_PATH #define MAX_PATH 2048 #endif @@ -71,12 +85,12 @@ enum { #if (LUA_VERSION_NUM == 502) #undef luaL_register #define luaL_register(L,n,f) \ - { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); } + { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); } #endif /* Define 'strerror' for systems that do not implement it */ #ifdef NO_STRERROR -#define strerror(_) "System unable to describe the error" +#define strerror(_) "System unable to describe the error" #endif #define DIR_METATABLE "directory metatable" @@ -95,21 +109,21 @@ typedef struct dir_data { */ static int pusherror(lua_State *L, const char *info) { - lua_pushnil(L); - if (info==NULL) - lua_pushstring(L, strerror(errno)); - else - lua_pushfstring(L, "%s: %s", info, strerror(errno)); - lua_pushinteger(L, errno); - return 3; + lua_pushnil(L); + if (info==NULL) + lua_pushstring(L, strerror(errno)); + else + lua_pushfstring(L, "%s: %s", info, strerror(errno)); + lua_pushinteger(L, errno); + return 3; } static int pushresult(lua_State *L, int i, const char *info) { - if (i==-1) - return pusherror(L, info); - lua_pushboolean(L, true); - return 1; + if (i==-1) + return pusherror(L, info); + lua_pushboolean(L, true); + return 1; } /* @@ -121,12 +135,12 @@ static int pushresult(lua_State *L, int i, const char *info) static int make_link(lua_State *L) { #ifndef _WIN32 - const char *oldpath = luaL_checkstring(L, 1); - const char *newpath = luaL_checkstring(L, 2); - return pushresult(L, - (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); + const char *oldpath = luaL_checkstring(L, 1); + const char *newpath = luaL_checkstring(L, 2); + return pushresult(L, + (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); #else - pusherror(L, "make_link is not supported on Windows"); + return pusherror(L, "make_link is not supported on Windows"); #endif } @@ -135,21 +149,21 @@ static int make_link(lua_State *L) ** @param #1 Directory path. */ static int make_dir (lua_State *L) { - const char *path = luaL_checkstring (L, 1); - int fail; + const char *path = luaL_checkstring (L, 1); + int fail; #ifdef _WIN32 - fail = _mkdir (path); + fail = _mkdir (path); #else - fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | - S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH ); + fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | + S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH ); #endif - if (fail) { - lua_pushnil (L); + if (fail) { + lua_pushnil (L); lua_pushfstring (L, "%s", strerror(errno)); - return 2; - } - lua_pushboolean (L, 1); - return 1; + return 2; + } + lua_pushboolean (L, 1); + return 1; } /* @@ -180,40 +194,40 @@ static int dir_iter (lua_State *L) { #else struct dirent *entry; #endif - dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); - luaL_argcheck (L, d->closed == 0, 1, "closed directory"); + dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); + luaL_argcheck (L, d->closed == 0, 1, "closed directory"); #ifdef _WIN32 - if (d->hFile == 0L) { /* first entry */ - if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) { - lua_pushnil (L); - lua_pushstring (L, strerror (errno)); - d->closed = 1; - return 2; - } else { - lua_pushstring (L, c_file.name); - return 1; - } - } else { /* next entry */ - if (_findnext (d->hFile, &c_file) == -1L) { - /* no more entries => close directory */ - _findclose (d->hFile); - d->closed = 1; - return 0; - } else { - lua_pushstring (L, c_file.name); - return 1; - } - } + if (d->hFile == 0L) { /* first entry */ + if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) { + lua_pushnil (L); + lua_pushstring (L, strerror (errno)); + d->closed = 1; + return 2; + } else { + lua_pushstring (L, c_file.name); + return 1; + } + } else { /* next entry */ + if (_findnext (d->hFile, &c_file) == -1L) { + /* no more entries => close directory */ + _findclose (d->hFile); + d->closed = 1; + return 0; + } else { + lua_pushstring (L, c_file.name); + return 1; + } + } #else - if ((entry = readdir (d->dir)) != NULL) { - lua_pushstring (L, entry->d_name); - return 1; - } else { - /* no more entries => close directory */ - closedir (d->dir); - d->closed = 1; - return 0; - } + if ((entry = readdir (d->dir)) != NULL) { + lua_pushstring (L, entry->d_name); + return 1; + } else { + /* no more entries => close directory */ + closedir (d->dir); + d->closed = 1; + return 0; + } #endif } @@ -239,22 +253,22 @@ static int dir_close (lua_State *L) { ** Factory of directory iterators */ static int dir_iter_factory (lua_State *L) { - const char *path = luaL_checkstring (L, 1); - dir_data *d; - lua_pushcfunction (L, dir_iter); - d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); - luaL_getmetatable (L, DIR_METATABLE); - lua_setmetatable (L, -2); - d->closed = 0; + const char *path = luaL_checkstring (L, 1); + dir_data *d; + lua_pushcfunction (L, dir_iter); + d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); + luaL_getmetatable (L, DIR_METATABLE); + lua_setmetatable (L, -2); + d->closed = 0; #ifdef _WIN32 - d->hFile = 0L; - if (strlen(path) > MAX_PATH-2) - luaL_error (L, "path too long: %s", path); - else - sprintf (d->pattern, "%s/*", path); + d->hFile = 0L; + if (strlen(path) > MAX_PATH-2) + luaL_error (L, "path too long: %s", path); + else + sprintf (d->pattern, "%s/*", path); #else - d->dir = opendir (path); - if (d->dir == NULL) + d->dir = opendir (path); + if (d->dir == NULL) luaL_error (L, "cannot open %s: %s", path, strerror (errno)); #endif return 2; @@ -265,35 +279,35 @@ static int dir_iter_factory (lua_State *L) { ** Creates directory metatable. */ static int dir_create_meta (lua_State *L) { - luaL_newmetatable (L, DIR_METATABLE); + luaL_newmetatable (L, DIR_METATABLE); - /* Method table */ - lua_newtable(L); - lua_pushcfunction (L, dir_iter); - lua_setfield(L, -2, "next"); - lua_pushcfunction (L, dir_close); - lua_setfield(L, -2, "close"); + /* Method table */ + lua_newtable(L); + lua_pushcfunction (L, dir_iter); + lua_setfield(L, -2, "next"); + lua_pushcfunction (L, dir_close); + lua_setfield(L, -2, "close"); - /* Metamethods */ - lua_setfield(L, -2, "__index"); - lua_pushcfunction (L, dir_close); - lua_setfield (L, -2, "__gc"); - return 1; + /* Metamethods */ + lua_setfield(L, -2, "__index"); + lua_pushcfunction (L, dir_close); + lua_setfield (L, -2, "__gc"); + return 1; } /* ** Assumes the table is on top of the stack. */ static void set_info (lua_State *L) { - lua_pushliteral (L, "_COPYRIGHT"); - lua_pushliteral (L, "Copyright (C) 2003-2009 Kepler Project"); - lua_settable (L, -3); - lua_pushliteral (L, "_DESCRIPTION"); - lua_pushliteral (L, "LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution"); - lua_settable (L, -3); - lua_pushliteral (L, "_VERSION"); - lua_pushliteral (L, "LuaFileSystem 1.5.0"); - lua_settable (L, -3); + lua_pushliteral (L, "_COPYRIGHT"); + lua_pushliteral (L, "Copyright (C) 2003-2009 Kepler Project"); + lua_settable (L, -3); + lua_pushliteral (L, "_DESCRIPTION"); + lua_pushliteral (L, "LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution"); + lua_settable (L, -3); + lua_pushliteral (L, "_VERSION"); + lua_pushliteral (L, "LuaFileSystem 1.5.0"); + lua_settable (L, -3); } static int get_path_separator(lua_State *L){ @@ -306,18 +320,18 @@ static int get_path_separator(lua_State *L){ } static const struct luaL_Reg fslib[] = { - {"dir", dir_iter_factory}, + {"dir", dir_iter_factory}, {"link", make_link}, - {"mkdir", make_dir}, - {"rmdir", remove_dir}, - {"get_path_separator", get_path_separator}, - {NULL, NULL}, + {"mkdir", make_dir}, + {"rmdir", remove_dir}, + {"get_path_separator", get_path_separator}, + {NULL, NULL}, }; LUALIB_API int luaopen_lfs(lua_State *L) { - dir_create_meta (L); - luaL_register (L, "lfs", fslib); - set_info (L); - return 1; + dir_create_meta (L); + luaL_register (L, "lfs", fslib); + set_info (L); + return 1; }