diff options
| author | mascarenhas <mascarenhas> | 2007-12-22 17:19:45 +0000 |
|---|---|---|
| committer | mascarenhas <mascarenhas> | 2007-12-22 17:19:45 +0000 |
| commit | 50f17597b39ff746b5dcc45b7b4962e1483b308f (patch) | |
| tree | 5b56e6f138e39b1c4fe643f3f03f6d87de4f630a /src | |
| parent | 24fdbc0a66c1828410c7949addcc86c66beb648c (diff) | |
| download | luafilesystem-50f17597b39ff746b5dcc45b7b4962e1483b308f.tar.gz luafilesystem-50f17597b39ff746b5dcc45b7b4962e1483b308f.tar.bz2 luafilesystem-50f17597b39ff746b5dcc45b7b4962e1483b308f.zip | |
Added lfs.setmode for changing file's mode (only for Windows)
Diffstat (limited to 'src')
| -rw-r--r-- | src/lfs.c | 43 |
1 files changed, 42 insertions, 1 deletions
| @@ -15,7 +15,7 @@ | |||
| 15 | ** lfs.touch (filepath [, atime [, mtime]]) | 15 | ** lfs.touch (filepath [, atime [, mtime]]) |
| 16 | ** lfs.unlock (fh) | 16 | ** lfs.unlock (fh) |
| 17 | ** | 17 | ** |
| 18 | ** $Id: lfs.c,v 1.42 2007/10/26 21:01:07 carregal Exp $ | 18 | ** $Id: lfs.c,v 1.43 2007/12/22 17:19:45 mascarenhas Exp $ |
| 19 | */ | 19 | */ |
| 20 | 20 | ||
| 21 | #include <errno.h> | 21 | #include <errno.h> |
| @@ -29,6 +29,7 @@ | |||
| 29 | #include <io.h> | 29 | #include <io.h> |
| 30 | #include <sys/locking.h> | 30 | #include <sys/locking.h> |
| 31 | #include <sys/utime.h> | 31 | #include <sys/utime.h> |
| 32 | #include <fcntl.h> | ||
| 32 | #else | 33 | #else |
| 33 | #include <unistd.h> | 34 | #include <unistd.h> |
| 34 | #include <dirent.h> | 35 | #include <dirent.h> |
| @@ -68,6 +69,15 @@ typedef struct dir_data { | |||
| 68 | } dir_data; | 69 | } dir_data; |
| 69 | 70 | ||
| 70 | 71 | ||
| 72 | #ifdef _WIN32 | ||
| 73 | #define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m)) | ||
| 74 | #else | ||
| 75 | #define _O_TEXT 0 | ||
| 76 | #define _O_BINARY 0 | ||
| 77 | #define lfs_setmode(L,file,m) ((void)((void)file,m), \ | ||
| 78 | luaL_error(L, LUA_QL("setmode") " not supported"), -1) | ||
| 79 | #endif | ||
| 80 | |||
| 71 | /* | 81 | /* |
| 72 | ** This function changes the working (current) directory | 82 | ** This function changes the working (current) directory |
| 73 | */ | 83 | */ |
| @@ -165,6 +175,36 @@ static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long star | |||
| 165 | } | 175 | } |
| 166 | 176 | ||
| 167 | 177 | ||
| 178 | static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | ||
| 179 | static const int mode[] = {_O_TEXT, _O_BINARY}; | ||
| 180 | static const char *const modenames[] = {"text", "binary", NULL}; | ||
| 181 | int op = luaL_checkoption(L, arg, NULL, modenames); | ||
| 182 | int res = lfs_setmode(L, f, mode[op]); | ||
| 183 | if (res != -1) { | ||
| 184 | int i; | ||
| 185 | lua_pushboolean(L, 1); | ||
| 186 | for (i = 0; modenames[i] != NULL; i++) { | ||
| 187 | if (mode[i] == res) { | ||
| 188 | lua_pushstring(L, modenames[i]); | ||
| 189 | goto exit; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | lua_pushnil(L); | ||
| 193 | exit: | ||
| 194 | return 2; | ||
| 195 | } else { | ||
| 196 | int en = errno; | ||
| 197 | lua_pushnil(L); | ||
| 198 | lua_pushfstring(L, "%s", strerror(en)); | ||
| 199 | lua_pushinteger(L, en); | ||
| 200 | return 3; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | static int lfs_f_setmode(lua_State *L) { | ||
| 205 | return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); | ||
| 206 | } | ||
| 207 | |||
| 168 | /* | 208 | /* |
| 169 | ** Locks a file. | 209 | ** Locks a file. |
| 170 | ** @param #1 File handle. | 210 | ** @param #1 File handle. |
| @@ -592,6 +632,7 @@ static const struct luaL_reg fslib[] = { | |||
| 592 | {"currentdir", get_dir}, | 632 | {"currentdir", get_dir}, |
| 593 | {"dir", dir_iter_factory}, | 633 | {"dir", dir_iter_factory}, |
| 594 | {"lock", file_lock}, | 634 | {"lock", file_lock}, |
| 635 | {"setmode", lfs_f_setmode}, | ||
| 595 | {"mkdir", make_dir}, | 636 | {"mkdir", make_dir}, |
| 596 | {"rmdir", remove_dir}, | 637 | {"rmdir", remove_dir}, |
| 597 | #ifndef _WIN32 | 638 | #ifndef _WIN32 |
