aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Wing <ewing@anscamobile.com>2012-05-01 14:38:02 -0700
committerEric Wing <ewing@anscamobile.com>2012-05-01 14:38:02 -0700
commit37c2309a40a08491887ce948c563f60a52b70c6d (patch)
tree68710e3f832ed77583f56f5118e5ca0ab4b0b5b7
parent84f1af5eef6af267109eaa84c18c5a70edaf65ea (diff)
downloadluafilesystem-37c2309a40a08491887ce948c563f60a52b70c6d.tar.gz
luafilesystem-37c2309a40a08491887ce948c563f60a52b70c6d.tar.bz2
luafilesystem-37c2309a40a08491887ce948c563f60a52b70c6d.zip
Due to Android (and apparently Sun) not supporting getcwd(NULL, 0), I've changed to call to getcwd to specify a buffer and explicit size which successfully works around the problem. One minor performance advantage is that one less malloc/free is needed since the buffer is now created on the stack. Apparently, this was already changed in a prior commit I didn't see initially, but I think the use of an invented/arbitrary PATH_MAX is incorrect. Platforms should generally provide a constant for this and PATH_MAX itself is already defined on some systems like Linux which can cause collisions. This commit improves on those changes by leveraging the constants provided by the compiler/system.
To help keep the code consistent but still correct, new platform specific code needed to be introduced for the max length. On POSIX I am assuming it is that <sys/param.h> provides MAXPATHLEN. This is what the Mac/BSD man page says to use and verified this is also defined on Ubuntu Linux and Android. On Windows, MAX_PATH is used. MAX_PATH seems to still be 260 which seems kind of small; is there a different constant we are supposed to use? In both cases, the respective constants are mapped to a new #define for LFS_MAXPATHLEN to allow the code to refer to one constant and avoid any potential name collisions in case MAXPATHLEN is defined already by something else on Windows (e.g. Cygwin).
-rw-r--r--src/lfs.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/lfs.c b/src/lfs.c
index a784d77..f3a6ce8 100644
--- a/src/lfs.c
+++ b/src/lfs.c
@@ -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
167static int get_dir (lua_State *L) { 169static 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;