* Changes the working directory to the path of the give executable. * For OSX application bundles '.app' is the required extension of the bundle, * so when we crop the path to there, when can remove the name of the bundle * in the same way we remove the name from the executable name. * @param exe the path to the executable */
| 673 | * @param exe the path to the executable |
| 674 | */ |
| 675 | static bool ChangeWorkingDirectoryToExecutable(std::string_view exe) |
| 676 | { |
| 677 | std::string path{exe}; |
| 678 | |
| 679 | #ifdef WITH_COCOA |
| 680 | for (size_t pos = path.find_first_of('.'); pos != std::string::npos; pos = path.find_first_of('.', pos + 1)) { |
| 681 | if (StrEqualsIgnoreCase(path.substr(pos, 4), ".app")) { |
| 682 | path.erase(pos); |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | #endif /* WITH_COCOA */ |
| 687 | |
| 688 | size_t pos = path.find_last_of(PATHSEPCHAR); |
| 689 | if (pos == std::string::npos) return false; |
| 690 | |
| 691 | path.erase(pos); |
| 692 | |
| 693 | if (chdir(path.c_str()) != 0) { |
| 694 | Debug(misc, 0, "Directory with the binary does not exist?"); |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | return true; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Whether we should scan the working directory. |
no test coverage detected