Add trivial implementation of setmode on non-Windows platforms.

This commit is contained in:
Reuben Thomas
2011-06-09 14:23:44 +01:00
parent 3140ca4db8
commit 361cede4d0
3 changed files with 12 additions and 23 deletions

View File

@@ -221,8 +221,8 @@ LuaFileSystem offers the following functions:
<dt><a name="setmode"></a><strong><code>lfs.setmode (file, mode)</code></strong></dt>
<dd>Sets the writing mode for a file. The mode string can be either <code>binary</code> or <code>text</code>.
Returns the previous mode string for the file. This function is only available in Windows, so you may want to make sure that
<code>lfs.setmode</code> exists before using it.
Returns the previous mode string for the file. On non-Windows platforms, where the two modes are identical,
setting the mode has no effect, and the mode is always returned as <code>binary</code>.
</dd>
<dt><a name="symlinkattributes"></a><strong><code>lfs.symlinkattributes (filepath [, aname])</code></strong></dt>

View File

@@ -100,8 +100,7 @@ typedef struct dir_data {
#else
#define _O_TEXT 0
#define _O_BINARY 0
#define lfs_setmode(L,file,m) ((void)((void)file,m), \
luaL_error(L, LUA_QL("setmode") " not supported on this platform"), -1)
#define lfs_setmode(L,file,m) 0
#define STAT_STRUCT struct stat
#define STAT_FUNC stat
#define LSTAT_FUNC lstat
@@ -281,10 +280,9 @@ static int lfs_unlock_dir(lua_State *L) {
}
#endif
#ifdef _WIN32
static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
static const int mode[] = {_O_TEXT, _O_BINARY};
static const char *const modenames[] = {"text", "binary", NULL};
static const int mode[] = {_O_BINARY, _O_TEXT};
static const char *const modenames[] = {"binary", "text", NULL};
int op = luaL_checkoption(L, arg, NULL, modenames);
int res = lfs_setmode(L, f, mode[op]);
if (res != -1) {
@@ -307,13 +305,6 @@ static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
return 3;
}
}
#else
static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
lua_pushboolean(L, 0);
lua_pushliteral(L, "setmode not supported on this platform");
return 2;
}
#endif
static int lfs_f_setmode(lua_State *L) {
return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);

View File

@@ -76,15 +76,13 @@ if (os.execute ("ln -s "..tmpfile.." _a_link_for_test_")) then
assert (os.remove"_a_link_for_test_")
end
if lfs.setmode then
-- Checking text/binary modes (works only in Windows)
local f = io.open(tmpfile, "w")
local result, mode = lfs.setmode(f, "binary")
assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform"))
result, mode = lfs.setmode(f, "text")
assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform"))
f:close()
end
-- Checking text/binary modes (only has an effect in Windows)
local f = io.open(tmpfile, "w")
local result, mode = lfs.setmode(f, "binary")
assert(result) -- on non-Windows platforms, mode is always returned as "binary"
result, mode = lfs.setmode(f, "text")
assert(result and mode == "binary")
f:close()
-- Restore access time to current value
assert (lfs.touch (tmpfile, attrib.access, attrib.modification))