diff options
author | Victor Westerhuis <victor@westerhu.is> | 2021-12-07 22:41:16 +0100 |
---|---|---|
committer | Victor Westerhuis <victor@westerhu.is> | 2021-12-07 22:41:16 +0100 |
commit | ab722e3fd51dbdd6cf9c8041f9ef0b4d789996ca (patch) | |
tree | 4c87d9d576a2a306b317f7d84a0b18075d0063bf /src/main.cc | |
parent | cb9103f5fdb1331f4c92cdabc2cb3e10a8b8523d (diff) | |
download | colm-default-build-dir.tar.gz |
Use /proc/self/exe to detect colm build directorydefault-build-dir
The canonical identity of a file on Unix is the combination of device
and inode numbers. This code checks both possible paths for the
colm executable: directly in the build directory (when colm is linked
against libcolm statically) or in libtool's `objdir` subdirectory (when
colm is linked against libcolm dynamically).
This fails in two situations I can see: when /proc is not mounted or
doesn't exist, or when colm is installed using a hard link.
Diffstat (limited to 'src/main.cc')
-rw-r--r-- | src/main.cc | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/main.cc b/src/main.cc index 482fb4a4..a54a6dd8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -688,14 +688,23 @@ void defaultBuildDir() if ( buildDir != 0 ) return; - const char *ldlp = getenv("LD_LIBRARY_PATH"); - if ( ldlp != 0 ) { - size_t len_atbd = strlen(ABS_TOP_BUILDDIR); + struct stat self; + if ( stat( "/proc/self/exe", &self ) == 0 ) + { + struct stat colm; + + if ( stat ( ABS_BUILDDIR "/" LT_OBJDIR "colm", &colm ) == 0 && + self.st_dev == colm.st_dev && self.st_ino == colm.st_ino ) + { + buildDir = ABS_TOP_BUILDDIR; + return; + } - if ( strlen(ldlp) > len_atbd && - memcmp( ldlp, ABS_TOP_BUILDDIR "/", len_atbd+1) == 0 ) + if ( stat ( ABS_BUILDDIR "/colm", &colm ) == 0 && + self.st_dev == colm.st_dev && self.st_ino == colm.st_ino ) { buildDir = ABS_TOP_BUILDDIR; + return; } } } |