realpath.c: passing NULL is POSIX-2008 compliant

also default MAX_PATH to 4096 which is more realistic than 32768
(4096 is the value in linux)
This commit is contained in:
Thomas Bernard 2018-12-10 13:34:02 +01:00
parent ec4ad8adb2
commit c9c4891b46
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -25,9 +25,9 @@
// the path. So this implementation is limited, it's really better to // the path. So this implementation is limited, it's really better to
// use realpath() if your platform has it. // use realpath() if your platform has it.
#if defined(__GP2X__) || defined(__WIZ__) || defined(__CAANOO__) || defined(__amigaos__) #if !defined(PATH_MAX)
// This is a random default value ... // This is a random default value ...
#define PATH_MAX 32768 #define PATH_MAX 4096
#endif #endif
static char *sep(char *path) static char *sep(char *path)
@ -123,18 +123,22 @@
return _fullpath(resolved_path,_path,260); return _fullpath(resolved_path,_path,260);
} }
#else #else
#include <limits.h>
// Use the stdlib function. // Use the stdlib function.
char *Realpath(const char *_path, char *resolved_path) char *Realpath(const char *_path, char *resolved_path)
{ {
// While linux version of realpath handles the resolved_path being a /// POSIX 2004 states :
// null pointer, this is not the case for other platforms (Haiku), nor /// If resolved_name is a null pointer, the behavior of realpath()
// specified by the open group in POSIX. So, be safe and allocate /// is implementation-defined.
// ourselves. ///
if(resolved_path==NULL) // if we called realpath with null as a 2nd arg /// but POSIX 2008 :
resolved_path = (char*) malloc( PATH_MAX ); /// If resolved_name is a null pointer, the generated pathname shall
return realpath(_path, resolved_path); /// be stored as a null-terminated string in a buffer allocated as if
/// by a call to malloc().
///
/// So we assume all platforms now support passing NULL.
/// If you find a platform where this is not the case,
/// please add a new implementation with #ifdef's.
return realpath(_path, resolved_path);
} }
#endif #endif