diff --git a/Makefile b/Makefile index 0a59fb6..298b75d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ include config.mk OBJS = csto.o callisto.o lcl.o lenviron.o lextra.o lfs.o ljson.o\ - lpath.o lprocess.o util.o + lprocess.o util.o LIBS = liblua.a cjson.a all: csto libcallisto.a @@ -23,7 +23,6 @@ lextra.o: lextra.c callisto.h util.h lenviron.o: lenviron.c callisto.h lfs.o: lfs.c callisto.h util.h ljson.o: ljson.c callisto.h -lpath.o: lpath.c callisto.h util.h lprocess.o: lprocess.c callisto.h util.h util.o: util.c diff --git a/callisto.c b/callisto.c index f4cb28c..8521201 100644 --- a/callisto.c +++ b/callisto.c @@ -15,7 +15,6 @@ static const luaL_Reg loadedlibs[] = { {CALLISTO_JSONLIBNAME, luaopen_json}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_OSLIBNAME, luaopen_os}, - {CALLISTO_PATHLIBNAME, luaopen_path}, {CALLISTO_PROCLIBNAME, luaopen_process}, {NULL, NULL} }; diff --git a/callisto.h b/callisto.h index c29fc56..e5224c4 100644 --- a/callisto.h +++ b/callisto.h @@ -22,7 +22,6 @@ #define CALLISTO_EXTLIBNAME "_G" /* global table */ #define CALLISTO_FSYSLIBNAME "fs" #define CALLISTO_JSONLIBNAME "json" -#define CALLISTO_PATHLIBNAME "path" #define CALLISTO_PROCLIBNAME "process" #define CALLISTO_ENVIRON "environ" @@ -32,7 +31,6 @@ int luaopen_environ(lua_State *); int luaopen_extra(lua_State *); int luaopen_fs(lua_State *); int luaopen_json(lua_State *); -int luaopen_path(lua_State *); int luaopen_process(lua_State *); lua_State *callisto_newstate(void); diff --git a/lfs.c b/lfs.c index 1335b19..337d6f0 100644 --- a/lfs.c +++ b/lfs.c @@ -22,6 +22,37 @@ #include "callisto.h" #include "util.h" +/*** + * Returns the last component of the given pathname, + * removing any trailing '/' characters. If the given + * path consists entirely of '/' characters, the string + * `"/"` is returned. If *path* is an empty string, + * the string `"."` is returned. + * + * @function basename + * @usage +print("name of script file is " .. fs.basename(arg[0])) +assert(fs.basename("/etc/fstab") == "fstab") + * @tparam string path The path to process. + */ +static int +fs_basename(lua_State *L) +{ + const char *ret; + char *path; /* parameter 1 (string) */ + + path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1)); + ret = basename(path); + + if (ret == NULL) /* failed? */ + return lfail(L); + + lua_pushstring(L, ret); + + free(path); + return 1; +} + /*** * Copies the contents of the file *source* to * the file *target*. *target* will be overwritten @@ -86,6 +117,36 @@ finish: return 1; } +/*** + * Returns the parent directory of the pathname + * given. Any trailing '/' characters are not + * counted as part of the directory name. + * If the given path is an empty string or contains + * no '/' characters, the string `"."` is returned, + * signifying the current directory. + * + * @function dirname + * @usage assert(fs.dirname("/usr/local/bin") == "/usr/local") + * @tparam string path The path to process. + */ +static int +fs_dirname(lua_State *L) +{ + const char *ret; + char *path; /* parameter 1 (string) */ + + path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1)); + ret = dirname(path); + + if (ret == NULL) /* failed? */ + return lfail(L); + + lua_pushstring(L, ret); + + free(path); + return 1; +} + /*** * Returns true if the given pathname exists * in the file system, or false if it does not. @@ -388,7 +449,9 @@ fs_workdir(lua_State *L) } static const luaL_Reg fslib[] = { + {"basename", fs_basename}, {"copy", fs_copy}, + {"dirname", fs_dirname}, {"exists", fs_exists}, {"isdirectory", fs_isdirectory}, {"isfile", fs_isfile}, diff --git a/lpath.c b/lpath.c deleted file mode 100644 index e4e2065..0000000 --- a/lpath.c +++ /dev/null @@ -1,101 +0,0 @@ -/*** - * File system path manipulation. - * - * @module path - */ - -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "callisto.h" -#include "util.h" - -/*** - * Returns the last component of the given pathname, - * removing any trailing '/' characters. If the given - * path consists entirely of '/' characters, the string - * `"/"` is returned. If *path* is an empty string, - * the string `"."` is returned. - * - * This function may return nil if the given - * pathname exceeds the system's path length - * limit (on most Linux systems this will be 4096). - * - * @function basename - * @usage path.basename(arg[0]) - * @tparam string path The path to process. - */ -static int -path_basename(lua_State *L) -{ - const char *ret; - char *path; /* parameter 1 (string) */ - - path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1)); - ret = basename(path); - - if (ret == NULL) /* failed? */ - return lfail(L); - - lua_pushstring(L, ret); - - free(path); - return 1; -} - -/*** - * Returns the parent directory of the pathname - * given. Any trailing '/' characters are not - * counted as part of the directory name. - * If the given path is an empty string or contains - * no '/' characters, the string `"."` is returned, - * signifying the current directory. - * - * This function may return nil if the given - * pathname exceeds the system's path length - * limit (on most Linux systems this will be 4096). - * - * @function dirname - * @usage path.dirname(arg[0]) - * @tparam string path The path to process. - */ -static int -path_dirname(lua_State *L) -{ - const char *ret; - char *path; /* parameter 1 (string) */ - - path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1)); - ret = dirname(path); - - if (ret == NULL) /* failed? */ - return lfail(L); - - lua_pushstring(L, ret); - - free(path); - return 1; -} - -static const luaL_Reg pathlib[] = { - {"basename", path_basename}, - {"dirname", path_dirname}, - {NULL, NULL} -}; - -int -luaopen_path(lua_State *L) -{ - luaL_newlib(L, pathlib); - return 1; -}