diff options
author | Fabio Mascarenhas <mascarenhas@gmail.com> | 2012-09-26 06:49:35 -0700 |
---|---|---|
committer | Fabio Mascarenhas <mascarenhas@gmail.com> | 2012-09-26 06:49:35 -0700 |
commit | 4a299c53b68cc9a9bf728f46c915e5b43280d896 (patch) | |
tree | ed8f0f051c319e96cc0726207b9f53e35fb183c0 | |
parent | 420db54f1e0c2d11df57581b94584741119ae717 (diff) | |
parent | 37c2309a40a08491887ce948c563f60a52b70c6d (diff) | |
download | luafilesystem-4a299c53b68cc9a9bf728f46c915e5b43280d896.tar.gz luafilesystem-4a299c53b68cc9a9bf728f46c915e5b43280d896.tar.bz2 luafilesystem-4a299c53b68cc9a9bf728f46c915e5b43280d896.zip |
Merge pull request #15 from ewmailing/master
Improvements to use of getcwd() for using the correct max path length
-rw-r--r-- | src/lfs.c | 16 |
1 files changed, 10 insertions, 6 deletions
@@ -82,6 +82,12 @@ | |||
82 | #define getcwd_error "Function 'getcwd' not provided by system" | 82 | #define getcwd_error "Function 'getcwd' not provided by system" |
83 | #else | 83 | #else |
84 | #define getcwd_error strerror(errno) | 84 | #define getcwd_error strerror(errno) |
85 | #ifdef _WIN32 | ||
86 | #define LFS_MAXPATHLEN MAX_PATH // MAX_PATH seems to be 260. Seems kind of small. Is there a better one? | ||
87 | #else | ||
88 | #include <sys/param.h> // for MAXPATHLEN | ||
89 | #define LFS_MAXPATHLEN MAXPATHLEN | ||
90 | #endif | ||
85 | #endif | 91 | #endif |
86 | 92 | ||
87 | #define DIR_METATABLE "directory metatable" | 93 | #define DIR_METATABLE "directory metatable" |
@@ -160,13 +166,11 @@ static int change_dir (lua_State *L) { | |||
160 | ** If unable to get the current directory, it returns nil | 166 | ** If unable to get the current directory, it returns nil |
161 | ** and a string describing the error | 167 | ** and a string describing the error |
162 | */ | 168 | */ |
163 | #ifndef PATH_MAX | ||
164 | #define PATH_MAX 4096 | ||
165 | #endif | ||
166 | |||
167 | static int get_dir (lua_State *L) { | 169 | static int get_dir (lua_State *L) { |
168 | char path[PATH_MAX]; | 170 | char *path; |
169 | if (getcwd((char *)path, PATH_MAX) == NULL) { | 171 | // Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. |
172 | char buf[LFS_MAXPATHLEN]; | ||
173 | if ((path = getcwd(buf, LFS_MAXPATHLEN)) == NULL) { | ||
170 | lua_pushnil(L); | 174 | lua_pushnil(L); |
171 | lua_pushstring(L, getcwd_error); | 175 | lua_pushstring(L, getcwd_error); |
172 | return 2; | 176 | return 2; |