From 54d079387e8b55fad6a961837c33e8f677221247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 19 Mar 2022 11:27:59 +0100 Subject: [PATCH] cli: in linux set proper exename value when cppcheck is executed from PATH and argv[0] does not have path information --- cli/cmdlineparser.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 8e6b8cc60..04187e96d 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -46,6 +46,10 @@ #include #endif +#ifdef __linux__ +#include +#endif + static void addFilesToList(const std::string& fileList, std::vector& pathNames) { // To keep things initially simple, if the file can't be opened, just be silent and move on. @@ -122,6 +126,14 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) bool maxconfigs = false; mSettings->exename = argv[0]; +#ifdef __linux__ + // Executing cppcheck in PATH. argv[0] does not contain the path. + if (mSettings->exename.find_first_of("/\\") == std::string::npos) { + char buf[PATH_MAX] = {0}; + if (FileLister::fileExists("/proc/self/exe") && readlink("/proc/self/exe", buf, sizeof(buf)-1) > 0) + mSettings->exename = buf; + } +#endif for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') {