Added lfs.setmode for changing file's mode (only for Windows)

This commit is contained in:
mascarenhas
2007-12-22 17:19:45 +00:00
parent 24fdbc0a66
commit 50f17597b3

View File

@@ -15,7 +15,7 @@
** lfs.touch (filepath [, atime [, mtime]]) ** lfs.touch (filepath [, atime [, mtime]])
** lfs.unlock (fh) ** lfs.unlock (fh)
** **
** $Id: lfs.c,v 1.42 2007/10/26 21:01:07 carregal Exp $ ** $Id: lfs.c,v 1.43 2007/12/22 17:19:45 mascarenhas Exp $
*/ */
#include <errno.h> #include <errno.h>
@@ -29,6 +29,7 @@
#include <io.h> #include <io.h>
#include <sys/locking.h> #include <sys/locking.h>
#include <sys/utime.h> #include <sys/utime.h>
#include <fcntl.h>
#else #else
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
@@ -68,6 +69,15 @@ typedef struct dir_data {
} dir_data; } dir_data;
#ifdef _WIN32
#define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m))
#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"), -1)
#endif
/* /*
** This function changes the working (current) directory ** This function changes the working (current) directory
*/ */
@@ -165,6 +175,36 @@ static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long star
} }
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};
int op = luaL_checkoption(L, arg, NULL, modenames);
int res = lfs_setmode(L, f, mode[op]);
if (res != -1) {
int i;
lua_pushboolean(L, 1);
for (i = 0; modenames[i] != NULL; i++) {
if (mode[i] == res) {
lua_pushstring(L, modenames[i]);
goto exit;
}
}
lua_pushnil(L);
exit:
return 2;
} else {
int en = errno;
lua_pushnil(L);
lua_pushfstring(L, "%s", strerror(en));
lua_pushinteger(L, en);
return 3;
}
}
static int lfs_f_setmode(lua_State *L) {
return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);
}
/* /*
** Locks a file. ** Locks a file.
** @param #1 File handle. ** @param #1 File handle.
@@ -592,6 +632,7 @@ static const struct luaL_reg fslib[] = {
{"currentdir", get_dir}, {"currentdir", get_dir},
{"dir", dir_iter_factory}, {"dir", dir_iter_factory},
{"lock", file_lock}, {"lock", file_lock},
{"setmode", lfs_f_setmode},
{"mkdir", make_dir}, {"mkdir", make_dir},
{"rmdir", remove_dir}, {"rmdir", remove_dir},
#ifndef _WIN32 #ifndef _WIN32