improve fs docs and minor blunders
This commit is contained in:
parent
661b141a1f
commit
2feddd9abc
1 changed files with 41 additions and 40 deletions
81
lfs.c
81
lfs.c
|
@ -23,12 +23,15 @@
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Returns the last component of the given pathname,
|
* Returns the last component of the given path,
|
||||||
* removing any trailing '/' characters. If the given
|
* removing any trailing '/' characters. If the given
|
||||||
* path consists entirely of '/' characters, the string
|
* path consists entirely of '/' characters, the string
|
||||||
* `"/"` is returned. If *path* is an empty string,
|
* `"/"` is returned. If *path* is an empty string,
|
||||||
* the string `"."` is returned.
|
* the string `"."` is returned.
|
||||||
*
|
*
|
||||||
|
* This is purely a string manipulation function and
|
||||||
|
* depends on no outside state.
|
||||||
|
*
|
||||||
* @function basename
|
* @function basename
|
||||||
* @usage
|
* @usage
|
||||||
print("name of script file is " .. fs.basename(arg[0]))
|
print("name of script file is " .. fs.basename(arg[0]))
|
||||||
|
@ -70,7 +73,8 @@ static int
|
||||||
fs_copy(lua_State *L)
|
fs_copy(lua_State *L)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
const char *source, *target;
|
const char *source; /* parameter 1 (string) */
|
||||||
|
const char *target; /* parameter 2 (string) */
|
||||||
char readbuf[512];
|
char readbuf[512];
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
int sfd, tfd;
|
int sfd, tfd;
|
||||||
|
@ -118,13 +122,16 @@ finish:
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Returns the parent directory of the pathname
|
* Returns the parent directory of the pathn
|
||||||
* given. Any trailing '/' characters are not
|
* given. Any trailing '/' characters are not
|
||||||
* counted as part of the directory name.
|
* counted as part of the directory name.
|
||||||
* If the given path is an empty string or contains
|
* If the given path is an empty string or contains
|
||||||
* no '/' characters, the string `"."` is returned,
|
* no '/' characters, the string `"."` is returned,
|
||||||
* signifying the current directory.
|
* signifying the current directory.
|
||||||
*
|
*
|
||||||
|
* This is purely a string manipulation function and
|
||||||
|
* depends on no outside state.
|
||||||
|
*
|
||||||
* @function dirname
|
* @function dirname
|
||||||
* @usage assert(fs.dirname("/usr/local/bin") == "/usr/local")
|
* @usage assert(fs.dirname("/usr/local/bin") == "/usr/local")
|
||||||
* @tparam string path The path to process.
|
* @tparam string path The path to process.
|
||||||
|
@ -149,7 +156,7 @@ fs_dirname(lua_State *L)
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Returns true if the given pathname exists
|
* Returns true if the given pathname exists
|
||||||
* in the file system, or false if it does not.
|
* in the file system.
|
||||||
*
|
*
|
||||||
* @function exists
|
* @function exists
|
||||||
* @usage
|
* @usage
|
||||||
|
@ -161,7 +168,7 @@ end
|
||||||
static int
|
static int
|
||||||
fs_exists(lua_State *L)
|
fs_exists(lua_State *L)
|
||||||
{
|
{
|
||||||
const char *path;
|
const char *path; /* parameter 1 (string) */
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
path = luaL_checkstring(L, 1);
|
path = luaL_checkstring(L, 1);
|
||||||
|
@ -178,7 +185,7 @@ static int
|
||||||
ismode(lua_State *L, mode_t mode)
|
ismode(lua_State *L, mode_t mode)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
const char *path;
|
const char *path; /* parameter 1 (string) */
|
||||||
|
|
||||||
path = luaL_checkstring(L, 1);
|
path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
@ -199,10 +206,9 @@ ismode(lua_State *L, mode_t mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Returns true if the given path specifies a
|
* Returns true if the given path specifies a directory.
|
||||||
* directory, or false if it does not.
|
* Will return false if either the given path does not
|
||||||
* Will return false if the given path does not
|
* specify a directory or the path does not exist at all.
|
||||||
* specify an existing directory entry at all.
|
|
||||||
*
|
*
|
||||||
* On error returns nil, an error message and a
|
* On error returns nil, an error message and a
|
||||||
* platform-dependent error code.
|
* platform-dependent error code.
|
||||||
|
@ -221,10 +227,10 @@ fs_isdirectory(lua_State *L)
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Returns true if the given path specifies a file,
|
* Returns true if the given path specifies a file.
|
||||||
* or false if it does not.
|
* Will return false if either the given path
|
||||||
* Will return false if the given path does not
|
* does not specify a directory or the path
|
||||||
* specify an existing directory entry at all.
|
* does not exist at all.
|
||||||
*
|
*
|
||||||
* On error returns nil, an error message and a
|
* On error returns nil, an error message and a
|
||||||
* platform-dependent error code.
|
* platform-dependent error code.
|
||||||
|
@ -296,7 +302,8 @@ mkpath(char *path, mode_t mode, mode_t dir_mode)
|
||||||
* Creates a new directory.
|
* Creates a new directory.
|
||||||
*
|
*
|
||||||
* If *recursive* is true, creates intermediate directories
|
* If *recursive* is true, creates intermediate directories
|
||||||
* as required; behaves as POSIX `mkdir -p` would.
|
* (parent directories) as required; behaves as POSIX
|
||||||
|
* `mkdir -p` would.
|
||||||
*
|
*
|
||||||
* On success, returns true. Otherwise returns nil, an error
|
* On success, returns true. Otherwise returns nil, an error
|
||||||
* message and a platform-dependent error code.
|
* message and a platform-dependent error code.
|
||||||
|
@ -304,8 +311,7 @@ mkpath(char *path, mode_t mode, mode_t dir_mode)
|
||||||
* @function mkdir
|
* @function mkdir
|
||||||
* @usage fs.mkdir("/usr/local/bin")
|
* @usage fs.mkdir("/usr/local/bin")
|
||||||
* @tparam string dir The path of the directory to create.
|
* @tparam string dir The path of the directory to create.
|
||||||
* @tparam[opt] boolean recursive Whether to create intermediate
|
* @tparam[opt] boolean recursive Whether to create intermediate directories.
|
||||||
* directories as required.
|
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
fs_mkdir(lua_State *L)
|
fs_mkdir(lua_State *L)
|
||||||
|
@ -334,11 +340,13 @@ fs_mkdir(lua_State *L)
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Moves the file at the path *src* to the path *dest*,
|
* Moves the item at path *src* to path *dest*.
|
||||||
* moving it between directories if required. If *dest*
|
* If *dest* exists, it is overwritten. Both *src* and *dest*
|
||||||
* exists, it is first removed. Both *src* and *dest*
|
* must be of the same type (that is, both directories or both
|
||||||
* must be of the same type (that is, both directories or
|
* non-directories) and must reside on the same file system.
|
||||||
* both non-directories) and must reside on the same file system.
|
*
|
||||||
|
* (If you need to move files across different file systems,
|
||||||
|
* consider using `fs.copy` instead.)
|
||||||
*
|
*
|
||||||
* On success, returns true. Otherwise returns nil, an error
|
* On success, returns true. Otherwise returns nil, an error
|
||||||
* message and a platform-dependent error code.
|
* message and a platform-dependent error code.
|
||||||
|
@ -365,8 +373,6 @@ fs_move(lua_State *L)
|
||||||
}
|
}
|
||||||
|
|
||||||
return lfail(L);
|
return lfail(L);
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
|
@ -382,7 +388,7 @@ fs_move(lua_State *L)
|
||||||
static int
|
static int
|
||||||
fs_rmdir(lua_State *L)
|
fs_rmdir(lua_State *L)
|
||||||
{
|
{
|
||||||
const char *dir;
|
const char *dir; /* parameter 1 (string) */
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
dir = luaL_checkstring(L, 1);
|
dir = luaL_checkstring(L, 1);
|
||||||
|
@ -394,8 +400,6 @@ fs_rmdir(lua_State *L)
|
||||||
}
|
}
|
||||||
|
|
||||||
return lfail(L);
|
return lfail(L);
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
|
@ -421,31 +425,28 @@ static int
|
||||||
fs_workdir(lua_State *L)
|
fs_workdir(lua_State *L)
|
||||||
{
|
{
|
||||||
const char *workdir; /* parameter 1 (string) */
|
const char *workdir; /* parameter 1 (string) */
|
||||||
char *buffer; /* buffer used by getcwd() */
|
char buffer[512]; /* buffer used by getcwd() */
|
||||||
char *ret;
|
char *ret;
|
||||||
|
|
||||||
if (lua_isnoneornil(L, 1)) { /* if first argument was not given... */
|
if (lua_isnoneornil(L, 1)) { /* if first argument was not given... */
|
||||||
buffer = malloc(sizeof(char *) * 512);
|
|
||||||
ret = getcwd(buffer, 512);
|
ret = getcwd(buffer, 512);
|
||||||
|
|
||||||
if (ret != NULL) {
|
if (ret != NULL) {
|
||||||
lua_pushstring(L, buffer);
|
lua_pushstring(L, buffer);
|
||||||
free(buffer);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(buffer);
|
|
||||||
return lfail(L);
|
|
||||||
} else {
|
|
||||||
workdir = luaL_checkstring(L, 1);
|
|
||||||
|
|
||||||
if (chdir(workdir) == 0) {
|
|
||||||
lua_pushboolean(L, 1);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return lfail(L);
|
return lfail(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workdir = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
if (chdir(workdir) == 0) {
|
||||||
|
lua_pushboolean(L, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lfail(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const luaL_Reg fslib[] = {
|
static const luaL_Reg fslib[] = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue