std::string relToExeDir(std::string localPath) { char buff[PATH_MAX + 1]; #if defined(__APPLE__) uint32_t size = PATH_MAX; int stat = _NSGetExecutablePath(buff, &size); assert(!stat); #elif defined (__linux__) ssize_t size = readlink("/proc/self/exe", buff, PATH_MAX); assert(size >= 0); #elif defined(_WIN32) HMODULE module = GetModuleHandle(NULL); DWORD size = GetModuleFileNameA(module, buff, PATH_MAX); assert(size >= 0); #endif buff[size] = 0; std::string exe_path(buff); #ifdef _WIN32 std::replace(exe_path.begin(), exe_path.end(), '\\', '/'); #endif std::string exe_dir = exe_path.substr(0, exe_path.rfind("/")); std::stringstream path_str; path_str << exe_dir << "/" << localPath; std::string path = path_str.str(); return path; }
static int find_file(char *filename, char *path) { if ( exe_path(filename, path) ) if( can_read_file(path)) return (1); findpath(filename,path); return ( (path[0]) ? 1 : 0); }
void DriverManager::streamFile(QString path) { /// @todo call SerialDriver with -s filename 127.0.0.1 6666 QString exe_path("SerialDriver.exe"); QString exe_args(QString("-s ") + path + QString(" 127.0.0.1 ") + QString().setNum(user_settings->server.portnumber)); driver->setProgram(exe_path); driver->setNativeArguments(exe_args); driver->open(); driverExpectStop = true; }
Options::Options(): _properties(), _modes() { std::string file = exe_path() + OPTIONS_FILE; std::ifstream options_file(file); if(options_file.is_open()) { OptionsParser p(*this, options_file); p.parse(); options_file.close(); } else std::cerr << "Cannot read OPTIONS_FILE: " << file << std::endl; }
// GetExecutableDirPath returns the directory that // mumble.exe resides in. static const std::wstring GetExecutableDirPath() { wchar_t path[MAX_PATH]; if (GetModuleFileNameW(NULL, path, MAX_PATH) == 0) return std::wstring(); if (!PathRemoveFileSpecW(path)) return std::wstring(); std::wstring exe_path(path); return exe_path.append(L"\\"); }
bool SpawnDumperProcess(_EXCEPTION_POINTERS *pep) { DWORD dwID = GetCurrentProcessId(); wchar_t processId[16]; wsprintfW(processId,L"%d",dwID); std::wstring exe_path(L"C:/l_WatchDog.exe"); std::wstring params(processId); size_t ret = ExecuteProcess(pep,exe_path, params, 60*15); if (ret==0) return true; return false; }
int main (int argc, char *argv[]) { char p[] = "/proc/%d/exe"; int i = 0; for (i = 1; i < NGROUPS_MAX; ++i) { char path[16]; sprintf(path, p, i); if (access(path, F_OK) != -1) { char *l = exe_path(path); char *b = get_basename(l); if (!strcmp(b, argv[1]) || !strncmp(b, argv[1], strlen(argv[1]))) { printf("%d ", i); } } } printf("\n"); return 0; }
int _tmain (int argc, TCHAR *argv[]) { fs::path exe_path( fs::initial_path<fs::path>() ); exe_path = fs::system_complete( fs::path(argv[0]) ); fs::current_path(exe_path.parent_path()); Config& config = Config::getInstance(); config.cfg_file = fs::current_path().string() + "\\daemon.cfg"; config.output_log = fs::current_path().string() + "\\" + std::string(LOG_DIRECTORY) + std::string(LOG_MAIN_FILE); config.error_log = fs::current_path().string() + "\\" + std::string(LOG_DIRECTORY) + std::string(LOG_ERROR_FILE); if (!fs::exists(LOG_DIRECTORY)) { fs::create_directory(LOG_DIRECTORY); } time_t now = time(nullptr); tm *ltm = localtime(&now); char buffer_time[256]; strftime(buffer_time, sizeof(buffer_time), "%Y%m%d_%H%M", ltm); if (fs::exists(config.output_log) && fs::file_size(config.output_log) > 0) { fs::rename( config.output_log, boost::str(boost::format("%1%main_%2%.log") % LOG_DIRECTORY % buffer_time) ); } if (fs::exists(config.error_log) && fs::file_size(config.error_log) > 0) { fs::rename( config.error_log, boost::str(boost::format("%1%error_%2%.log") % LOG_DIRECTORY % buffer_time) ); } #ifndef NON_DAEMON freopen(config.output_log.c_str(), "w", stdout); freopen(config.error_log.c_str(), "w", stderr); #endif // Info std::cout << "CurrentPath: " << fs::current_path() << std::endl; std::cout << "Config: " << config.cfg_file << std::endl; std::cout << "OutputLog: " << config.output_log << std::endl; std::cout << "ErrorLog: " << config.error_log << std::endl; #ifdef NON_DAEMON run_daemon(); #else SERVICE_TABLE_ENTRY ServiceTable[] = { {SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) ServiceMain}, {NULL, NULL} }; if (StartServiceCtrlDispatcher (ServiceTable) == FALSE) { return GetLastError (); } #endif }