mirror of
https://github.com/nmap/nmap.git
synced 2026-01-10 08:29:02 +00:00
Add includes from the original lfs.c code, fix formatting (spaces instead of tabs).
This commit is contained in:
248
nse_fs.cc
248
nse_fs.cc
@@ -36,6 +36,32 @@
|
||||
|
||||
#define _LARGEFILE64_SOURCE
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <sys/locking.h>
|
||||
#ifdef __BORLANDC__
|
||||
#include <utime.h>
|
||||
#else
|
||||
#include <sys/utime.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <utime.h>
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
@@ -47,20 +73,8 @@ extern "C" {
|
||||
#include "nmap_error.h"
|
||||
#include "NmapOps.h"
|
||||
|
||||
#include <utime.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user