Skip to content

Commit

Permalink
use PATH_MAX buffer with realpath on Mac OS <= v10.6 (#780)
Browse files Browse the repository at this point in the history
Modern systems support a NULL second argument to `realpath`, but some
older ones don't. Use whether `PATH_MAX` is defined as a way of
triggering a non-NULL second argument, and arrange for `PATH_MAX` that
to be defined for old Mac OS versions.
  • Loading branch information
mflatt authored Dec 11, 2023
1 parent 9b274bb commit b09dc5e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions c/self-exe.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ static char *copy_string(const char *s) {

#if defined(__APPLE__) && defined(__MACH__)
#include <mach-o/dyld.h>
#include <AvailabilityMacros.h>
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1070
/* `PATH_MAX` mode for `realpath` is needed for 10.6 and earlier: */
# include <sys/param.h>
#endif
#define HAVE_GET_SELF_PATH_PLATFORM
static char *get_self_path_platform() {
uint32_t bufsize = 256;
Expand Down Expand Up @@ -260,7 +265,17 @@ static char *get_process_executable_path(const char *exec_file) {
}
char *rr = NULL;
if (r != NULL) {
/* `PATH_MAX` is a problem in various ways, but if `realpath` doesn't
accept a NULL second argument, then make sure `PATH_MAX` is defined.
Otherwise, avoid having `PATH_MAX` defined. */
#ifdef PATH_MAX
char buffer[PATH_MAX];
rr = realpath(r, buffer);
if (rr != NULL)
rr = copy_string(rr);
#else
rr = realpath(r, NULL);
#endif
}
free(r);
return rr;
Expand Down

0 comments on commit b09dc5e

Please sign in to comment.