Esempio n. 1
0
/*
 *  parse_args()
 *  Determine the program name, parse the command line options and assign
 *  the input and output filenames and time travel parameter. May exit on
 *  error.
 */
void parse_args(int argc, char **argv, char **out_file, char **err_file, int *time_travel) {

    int flag;
    int errors = 0;
    char *p;
    extern char *optarg;
    extern int optind, optopt;

    if(NULL == (g_PROGRAM_NAME = getexecname())) {
        fprintf(stderr, "failed to get program name; aborting");
        exit(2);
    }

    while((flag = getopt(argc, argv, "dho:e:t:v")) != -1) {

        switch(flag) {
        case 'd':
            g_DEBUG = 1;
            break;
        case 'h':
            usage();
            exit(0);
            break;
        case 'o':
            *out_file = optarg;
            break;
        case 'e':
            *err_file = optarg;
            break;
        case 't':
            for(p = optarg; *p; p++) {
                if(*p < '0' || *p > '9') {
                    error("argument to -t must be an integer\n");
                    exit(2);
                }
            }
            *time_travel = atoi(optarg);
            break;
        case 'v':
            version();
            exit(0);
            break;
        default:
            errors++;
            break;
        }

    }

    if(optind != argc) {
        error("invalid arguments\n");
        errors++;
    }

    if(errors) {
        usage();
        exit(2);
    }

}
static void locate_haproxy(char *buffer, size_t buffer_size)
{
	strncpy(buffer, getexecname(), buffer_size);
	char *end = strrchr(buffer, '/');
	end[1] = '\0';
	strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1));
	buffer[buffer_size - 1] = '\0';
}
Esempio n. 3
0
void GetExePath(int argc, char *argv[], boost::filesystem::path & path, boost::system::error_code & ec)
{
    char buffer[PATH_MAX];
    if (::realpath(getexecname(), buffer) == NULL)
        ec.assign(errno, boost::system::generic_category());
    else
        path.assign(buffer);
}
Esempio n. 4
0
File: out.c Progetto: mpusz/nvml
/*
 * out_init -- initialize the log
 *
 * This is called from the library initialization code.
 */
void
out_init(const char *log_prefix, const char *log_level_var,
		const char *log_file_var, int major_version,
		int minor_version)
{
	static int once;

	/* only need to initialize the out module once */
	if (once)
		return;
	once++;

	Log_prefix = log_prefix;

#ifdef	DEBUG
	char *log_level;
	char *log_file;

	if ((log_level = getenv(log_level_var)) != NULL) {
		Log_level = atoi(log_level);
		if (Log_level < 0) {
			Log_level = 0;
		}
	}

	if ((log_file = getenv(log_file_var)) != NULL) {
		int cc = strlen(log_file);

		/* reserve more than enough space for a PID + '\0' */
		char log_file_pid[cc + 30];

		if (cc > 0 && log_file[cc - 1] == '-') {
			snprintf(log_file_pid, cc + 30, "%s%d",
				log_file, getpid());
			log_file = log_file_pid;
		}
		if ((Out_fp = fopen(log_file, "w")) == NULL) {
			fprintf(stderr, "Error (%s): %s=%s: %s\n",
					log_prefix, log_file_var,
					log_file, strerror(errno));
			exit(1);
		}
	}
#endif	/* DEBUG */

	if (Out_fp == NULL)
		Out_fp = stderr;
	else
		setlinebuf(Out_fp);

	LOG(1, "pid %d: program: %s", getpid(), getexecname());
	LOG(1, "%s version %d.%d", log_prefix, major_version, minor_version);
	LOG(1, "src version %s", nvml_src_version);
#ifdef USE_VALGRIND
	LOG(1, "compiled with support for Valgrind");
#endif /* USE_VALGRIND */
}
Esempio n. 5
0
bool getCurrentExecPath(char *buf, size_t len)
{
	const char *exec = getexecname();
	if (exec == NULL)
		return false;

	if (strlcpy(buf, exec, len) >= len)
		return false;

	return true;
}
Esempio n. 6
0
char *
smb_getprogname()
{
	char *p;

	if (__progname == NULL) {
		__progname = (char *)getexecname();
		if ((p = strrchr(__progname, '/')) != 0)
			__progname = p + 1;
	}
	return (__progname);
}
Esempio n. 7
0
const char *acl_process_path()
{
	const char *myname = "acl_process_path";
	const char *ptr;

	ptr = getexecname();
	if (ptr == NULL) {
		acl_msg_error("%s(%d): readlink error(%s)",
				myname, __LINE__, acl_last_serror());
		return (NULL);
	}
	return (ptr);
}
Esempio n. 8
0
static void
save_execname()
{
	const char *name = getexecname();

	saved_execname[0] = 0;

	if (name[0] != '/') { /* started w/ relative path */
		(void) getcwd(saved_execname, MAXPATHLEN);
		(void) strlcat(saved_execname, "/", MAXPATHLEN);
	}
	(void) strlcat(saved_execname, name, MAXPATHLEN);
}
Esempio n. 9
0
char *
get_bin_path(void)
{
	/* getting the path to the executable's binary, isn't all that
	 * simple, unfortunately */
#ifdef NATIVE_WIN32
	if (GetModuleFileName(NULL, _bin_path,
			      (DWORD) sizeof(_bin_path)) != 0)
		return _bin_path;
#elif defined(HAVE__NSGETEXECUTABLEPATH)  /* Darwin/OSX */
	char buf[PATH_MAX];
	uint32_t size = PATH_MAX;
	if (_NSGetExecutablePath(buf, &size) == 0 &&
			realpath(buf, _bin_path) != NULL)
	return _bin_path;
#elif defined(HAVE_SYS_SYSCTL_H) && defined(KERN_PROC_PATHNAME)  /* BSD */
	int mib[4];
	size_t cb = sizeof(_bin_path);
	mib[0] = CTL_KERN;
	mib[1] = KERN_PROC;
	mib[2] = KERN_PROC_PATHNAME;
	mib[3] = -1;
	if (sysctl(mib, 4, _bin_path, &cb, NULL, 0) == 0)
		return _bin_path;
#elif defined(HAVE_GETEXECNAME)  /* Solaris */
	char buf[PATH_MAX];
	const char *execn = getexecname();
	/* getexecname doesn't always return an absolute path, the only
	 * thing it seems to do is strip leading ./ from the invocation
	 * string. */
	if (*execn != '/') {
		if (getcwd(buf, PATH_MAX) != NULL) {
			snprintf(buf + strlen(buf), PATH_MAX - strlen(buf), "/%s", execn);
			if (realpath(buf, _bin_path) != NULL)
				return(_bin_path);
		}
	} else {
		if (realpath(execn, _bin_path) != NULL)
			return(_bin_path);
	}
#else  /* try Linux approach, also works on Cygwin */
	if (readlink("/proc/self/exe",
				_bin_path, sizeof(_bin_path)) != -1)
			return _bin_path;
#endif
	/* could use argv[0] (passed) to deduce location based on PATH, but
	 * that's a lot of work and unreliable */
	return NULL;
}
Esempio n. 10
0
const char *thisprogname(void)
{
#if defined(__FreeBSD__)
	return getprogname();
#elif defined(__APPLE__)
	return getprogname();
#elif defined(__sun__)
	return getexecname();
#elif defined(__linux__)
	if (readlink("/proc/self/exe", proggy, MAXPATHLEN) != -1)
		return proggy;
	return "";
#else
#error "unsupported OS"
#endif
}
int main(int argc, char *argv[], char *envv[])
{
    int rc = 0;
    const char *pszExec = getexecname();

    if (!pszExec)
    {
        fprintf(stderr, "Failed to get executable name.\n");
        return -1;
    }

    rc = isaexec(pszExec, argv, envv);
    if (rc == -1)
        fprintf(stderr, "Failed to find/execute ISA specific executable for %s\n", pszExec);

    return rc;
}
Esempio n. 12
0
std::string executable_path(const char *argv0)
{
    std::string ret = getexecname();
    if (ret.empty())
    {
        return executable_path_fallback(argv0);
    }
    boost::filesystem::path p(ret);
    if (!p.has_root_directory())
    {
        boost::system::error_code ec;
        p = boost::filesystem::canonical(
            p, boost::filesystem::current_path(), ec);
        ret = p.make_preferred().string();
    }
    return ret;
}
Esempio n. 13
0
boost::filesystem::path Util::bin_dir()
{
    boost::filesystem::path path;

#if defined(_WIN32)
#include <windows.h>
    char buf[1024];
    DWORD ret = GetModuleFileName(NULL, buf, sizeof(buf));
    if (ret != 0 && ret != sizeof(buf))
        path = buf;
#elif defined(__APPLE__)  // Not tested
#include <mach-o/dyld.h>
    char buf[1024];
    uint32_t size = sizeof(buf);
    int ret = _NSGetExecutablePath(buf, &size);
    if (ret == 0)
        path = buf;
#elif defined(sun) || defined(__sun) // Not tested
#include <stdlib.h>
    path = getexecname();
#elif defined(__FreeBSD__)
#include <sys/sysctl.h>
    int mib[4];
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PATHNAME;
    mib[3] = -1;
    char buf[1024];
    size_t size = sizeof(buf);
    sysctl(mib, 4, buf, &size, NULL, 0);
    if (size != 0 && size != sizeof(buf))
        path = std::string(buf, size);
#elif defined(__linux__)
#include <unistd.h>
    char buf[1024];
    ssize_t ret = readlink("/proc/self/exe", buf, sizeof(buf));
    if (ret != 0 && ret != sizeof(buf))
        path = std::string(buf, ret);
#else
    message("Unsupported operating system; unable to determine the \
binary path. Trying with current working directory ...");
#endif

    return path.branch_path();
}
Esempio n. 14
0
/*ARGSUSED*/
int
main(int argc, char **argv, char **envp)
{
	const char *execname;
	const char *fname;

#if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
#define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
#endif
	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	/*
	 * Get the exec name.
	 */
	if ((execname = getexecname()) == NULL) {
		(void) fprintf(stderr,
				gettext("%s: getexecname() failed\n"),
				argv[0]);
		return (1);
	}

	/*
	 * Get the base name of the executable.
	 */
	fname = strrchr(execname, '/');
	fname = (fname != NULL) ? (fname+1) : execname;

	if (isaexec(execname, argv, envp) == -1) {
		if (errno == ENOENT) {
			(void) fprintf(stderr,
					gettext("%s: cannot find/execute \"%s\""
						" in ISA subdirectories\n"),
					argv[0], fname);
			return (1);
		}
	}

	(void) fprintf(stderr,
			gettext("%s: isaexec(\"%s\") failed\n"),
			argv[0], fname);
	return (1);
}
Esempio n. 15
0
const char *
getpname(void)
{
	const char *p, *q;

	if (pname != NULL)
		return (pname);

	if ((p = getexecname()) != NULL)
		q = strrchr(p, '/');
	else
		q = NULL;

	if (q == NULL)
		pname = p;
	else
		pname = q + 1;

	return (pname);
}
Esempio n. 16
0
int main (void)
{
	char *cwd;
	const char *path;

	if ((path = getexecname ()) == NULL)
		err_msg ("getexecname failed");

	if (*path != '/') {
		if ((cwd = getcwd (NULL, PATH_MAX)) == NULL)
			err_msg ("getcwd failed");

		printf ("exec name = %s/%s\n", cwd, path);
		free (cwd);
	}
	else
		printf ("exec name = %s\n", path);

	return (0);
}
Esempio n. 17
0
const char *
getprogname (void)
{
	const char *name;

#if defined (HAVE_GETEXECNAME)
	const char *p;
	name = getexecname();
	p = strrchr (name ? name : "", '/');
	if (p != NULL)
		name = p + 1;
#elif defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME)
	name = program_invocation_short_name;
#elif defined (HAVE___PROGNAME)
	name = __progname;
#else
	#error No way to retrieve short program name
#endif

	return name;
}
Esempio n. 18
0
PLATAPI const char* plat_execpath(void) {
	char cur_workdir[PATHMAXLEN];
	char* execname;

	if(have_execpath)
		return cur_execpath;

	if((execname = getexecname()) == NULL ||
	    (getcwd(cur_workdir, PATHMAXLEN) == NULL))
			return NULL;

	if(execname[0] != '/') {
		path_join(cur_execpath, PATHMAXLEN,
				  cur_workdir, execname, NULL);
	}
	else {
		strncpy(cur_execpath, execname, PATHMAXLEN);
	}

	have_execpath = B_TRUE;
	return cur_execpath;
}
Esempio n. 19
0
const char *
uu_setpname(char *arg0)
{
	/*
	 * Having a NULL argv[0], while uncommon, is possible.  It
	 * makes more sense to handle this event in uu_setpname rather
	 * than in each of its consumers.
	 */
	if (arg0 == NULL) {
		pname = getexecname();
		if (pname == NULL)
			pname = "unknown_command";
		return (pname);
	}

	/*
	 * Guard against '/' at end of command invocation.
	 */
	for (;;) {
		char *p = strrchr(arg0, '/');
		if (p == NULL) {
			pname = arg0;
			break;
		} else {
			if (*(p + 1) == '\0') {
				*p = '\0';
				continue;
			}

			pname = p + 1;
			break;
		}
	}

	return (pname);
}
Esempio n. 20
0
/**
 * get the executable filename.
 */
char* get_executable() {

// dlai modified 12/29/2012
#ifdef sun
  return( (char*) getexecname());
#else
  char buffer[PATH_MAX];
  // dlai blocked snprintf(buffer, PATH_MAX, "/proc/%u/exe", getpid());
  snprintf(buffer, PATH_MAX, "/proc/%lu/exe", getpid());
  char *filename = malloc(PATH_MAX);
  ssize_t len = readlink(buffer, filename, PATH_MAX);
  if (len == -1) {
    fprintf(stderr, "Can't get executable name from %s - %s\n", buffer,
            strerror(errno));
    exit(-1);
  } else if (len >= PATH_MAX) {
    fprintf(LOGFILE, "Executable name %.*s is longer than %d characters.\n",
            PATH_MAX, filename, PATH_MAX);
    exit(-1);
  }
  filename[len] = '\0';
  return filename;
#endif 
}
Esempio n. 21
0
wxString CodeBlocksApp::GetAppPath() const
{
    wxString base;
#ifdef __WXMSW__
    wxChar name[MAX_PATH] = {0};
    GetModuleFileName(0L, name, MAX_PATH);
    wxFileName fname(name);
    base = fname.GetPath(wxPATH_GET_VOLUME);
#else
    if (!m_Prefix.IsEmpty())
        return m_Prefix;

#ifdef SELFPATH
    // SELFPATH is a macro from prefix.h (binreloc)
    // it returns the absolute filename of us
    // similar to win32 GetModuleFileName()...
    base = wxString(SELFPATH,wxConvUTF8);
    base = wxFileName(base).GetPath();
#endif
#if defined(sun) || defined(__sun)
    base = wxString(getexecname(),wxConvCurrent);
    base = wxFileName(base).GetPath();
#endif
#if defined(__APPLE__) && defined(__MACH__)
    char path[MAXPATHLEN+1];
    uint32_t path_len = MAXPATHLEN;
    // SPI first appeared in Mac OS X 10.2
    _NSGetExecutablePath(path, &path_len);
    base = wxString(path, wxConvUTF8);
    base = wxFileName(base).GetPath();
#endif
    if (base.IsEmpty())
        base = _T(".");
#endif
    return base;
}
Esempio n. 22
0
int get_exec_path(char *exec_path, int len)
{
    int ret;
#ifdef LINUX
    ret = readlink("/proc/self/exe", exec_path, len);
    if (ret < 0) {
        fprintf(stderr, "readlink() failed. %d %s\n",
                errno, strerror(errno));
        return -1;
    }
    exec_path[len - 1] = '\0';
    return 0;
#elif SUNOS
    const char *cmdp;
    char cwd[BUFSIZ], *cwdp;
    char *homep = getenv("HOME");

    cmdp = getexecname();
    if (cmdp != NULL) {
        if (homep != NULL) {
            if (strstr(cmdp, homep) == NULL) {
                cwdp = getcwd(cwd, sizeof(cwd));
                if (cwdp == NULL) {
                    fprintf(stderr,
                            "getcwd() failed. %d %s\n",
                            errno, strerror(errno));
                    return -1;
                }
                cwd[sizeof(cwd) - 1] = '\0';
                snprintf(exec_path, len,
                         "%s/%s", cwd, cmdp);
            } else {
                strncpy(exec_path, cmdp, len);
                exec_path[len - 1] = '\0';
            }
        } else {
            fprintf(stderr, "warning: couldn't get $HOME\n");
            snprintf(exec_path, len,
                     "./%s\n", cmdp);
        }
    } else {
        fprintf(stderr, "getexecname() failed. %d %s\n",
                errno, strerror(errno));
        return -1;
    }
    return 0;
#elif HP_UX
    struct shl_descriptor desc;

    ret = shl_get_r(0, &desc);
    if (ret < 0) {
        fprintf(stderr, "shl_get_r() failed. %d %s\n",
                errno, strerror(errno));
        return -1;
    }
    strncpy(exec_path, desc.filename, len);
    exec_path[len - 1] = '\0';
    return 0;
#else
    return -1;
#endif
}
Esempio n. 23
0
/*
 * out_init -- initialize the log
 *
 * This is called from the library initialization code.
 */
void
out_init(const char *log_prefix, const char *log_level_var,
		const char *log_file_var, int major_version,
		int minor_version)
{
	static int once;

	/* only need to initialize the out module once */
	if (once)
		return;
	once++;

	Log_prefix = log_prefix;

#ifdef	DEBUG
	char *log_level;
	char *log_file;

	if ((log_level = getenv(log_level_var)) != NULL) {
		Log_level = atoi(log_level);
		if (Log_level < 0) {
			Log_level = 0;
		}
	}

	if ((log_file = getenv(log_file_var)) != NULL) {
		size_t cc = strlen(log_file);

		/* reserve more than enough space for a PID + '\0' */
		char *log_file_pid = alloca(cc + 30);

		if (cc > 0 && log_file[cc - 1] == '-') {
			snprintf(log_file_pid, cc + 30, "%s%d",
				log_file, getpid());
			log_file = log_file_pid;
		}
		if ((Out_fp = fopen(log_file, "w")) == NULL) {
			fprintf(stderr, "Error (%s): %s=%s: %s\n",
					log_prefix, log_file_var,
					log_file, strerror(errno));
			abort();
		}
	}
#endif	/* DEBUG */

	char *log_alignment = getenv("NVML_LOG_ALIGN");
	if (log_alignment) {
		int align = atoi(log_alignment);
		if (align > 0)
			Log_alignment = (unsigned)align;
	}

	if (Out_fp == NULL)
		Out_fp = stderr;
	else
		setlinebuf(Out_fp);

#ifdef	DEBUG
	LOG(1, "pid %d: program: %s", getpid(), getexecname());
#endif
	LOG(1, "%s version %d.%d", log_prefix, major_version, minor_version);
	LOG(1, "src version %s", nvml_src_version);
#ifdef USE_VG_PMEMCHECK
	/*
	 * Attribute "used" to prevent compiler from optimizing out the variable
	 * when LOG expands to no code (!DEBUG)
	 */
	static __attribute__((used)) const char *pmemcheck_msg =
			"compiled with support for Valgrind pmemcheck";
	LOG(1, "%s", pmemcheck_msg);
#endif /* USE_VG_PMEMCHECK */
#ifdef USE_VG_HELGRIND
	static __attribute__((used)) const char *helgrind_msg =
			"compiled with support for Valgrind helgrind";
	LOG(1, "%s", helgrind_msg);
#endif /* USE_VG_HELGRIND */
#ifdef USE_VG_MEMCHECK
	static __attribute__((used)) const char *memcheck_msg =
			"compiled with support for Valgrind memcheck";
	LOG(1, "%s", memcheck_msg);
#endif /* USE_VG_MEMCHECK */

	Last_errormsg_key_alloc();
}
Esempio n. 24
0
std::string getExecutablePath() {
	
#if ARX_PLATFORM == ARX_PLATFORM_MACOSX
	
	uint32_t bufsize = 0;
	
	// Obtain required size
	_NSGetExecutablePath(NULL, &bufsize);
	
	std::vector<char> exepath(bufsize);
	
	if(_NSGetExecutablePath(&exepath.front(), &bufsize) == 0) {
		char exerealpath[MAXPATHLEN];
		if(realpath(&exepath.front(), exerealpath)) {
			return exerealpath;
		}
	}
	
#elif defined(ARX_HAVE_WINAPI)
	
	std::vector<char> buffer;
	buffer.resize(MAX_PATH);
	if(GetModuleFileNameA(NULL, buffer.data(), buffer.size()) > 0) {
		return std::string(buffer.data(), buffer.size());
	}
	
#else
	
	// Try to get the path from OS-specific procfs entries
	#ifdef ARX_HAVE_READLINK
	std::vector<char> buffer(1024);
	// Linux
	if(try_readlink(buffer, "/proc/self/exe")) {
		return std::string(buffer.begin(), buffer.end());
	}
	// BSD
	if(try_readlink(buffer, "/proc/curproc/file")) {
		return std::string(buffer.begin(), buffer.end());
	}
	// Solaris
	if(try_readlink(buffer, "/proc/self/path/a.out")) {
		return std::string(buffer.begin(), buffer.end());
	}
	#endif
	
	// FreeBSD
	#if defined(ARX_HAVE_SYSCTL) && defined(CTL_KERN) && defined(KERN_PROC) \
	    && defined(KERN_PROC_PATHNAME) && ARX_PLATFORM == ARX_PLATFORM_BSD \
	    && defined(PATH_MAX)
	int mib[4];
	mib[0] = CTL_KERN;
	mib[1] = KERN_PROC;
	mib[2] = KERN_PROC_PATHNAME;
	mib[3] = -1;
	char pathname[PATH_MAX];
	size_t size = sizeof(pathname);
	int error = sysctl(mib, 4, pathname, &size, NULL, 0);
	if(error != -1 && size > 0 && size < sizeof(pathname)) {
		return util::loadString(pathname, size);
	}
	#endif
	
	// Solaris
	#ifdef ARX_HAVE_GETEXECNAME
	const char * execname = getexecname();
	if(execname != NULL) {
		return execname;
	}
	#endif
	
	// Fall back to argv[0] if possible
	if(executablePath != NULL) {
		std::string path(executablePath);
		if(path.find('/') != std::string::npos) {
			return path;
		}
	}
	
#endif
	
	// Give up - we couldn't determine the exe path.
	return std::string();
}
Esempio n. 25
0
std::string GetProcessPath() {
#if defined( PLATFORM_OSX )
	char exe_file[PATH_MAX + 1];
	CFBundleRef mainBundle = CFBundleGetMainBundle();
	if (mainBundle) {
		CFURLRef mainURL = CFBundleCopyBundleURL(mainBundle);

		if (mainURL) {
			int ok = CFURLGetFileSystemRepresentation ( mainURL, (Boolean) true, (UInt8*)exe_file, PATH_MAX );

			if (ok) {
				return std::string(exe_file) + "/";
			}
		}
	}

	return "./";
#elif defined( PLATFORM_LINUX )
	char exe_file[PATH_MAX + 1];
	int size;
	size = readlink("/proc/self/exe", exe_file, PATH_MAX);
	if (size < 0) {
		return "./";
	} else {
		exe_file[size] = '\0';
		return std::string(dirname(exe_file)) + "/";
	}
#elif defined( PLATFORM_WIN32 )
	// Get path to executable:
	TCHAR szDllName[_MAX_PATH];
	TCHAR szDrive[_MAX_DRIVE];
	TCHAR szDir[_MAX_DIR];
	TCHAR szFilename[_MAX_DIR];
	TCHAR szExt[_MAX_DIR];
	GetModuleFileName(0, szDllName, _MAX_PATH);

	_splitpath(szDllName, szDrive, szDir, szFilename, szExt);

	return std::string(szDrive) + std::string(szDir);
#elif defined( PLATFORM_BSD )
	int mib[4];
	mib[0] = CTL_KERN;
	mib[1] = KERN_PROC;
	mib[2] = KERN_PROC_PATHNAME;
	mib[3] = -1;
	char buf[1024];
	size_t cb = sizeof(buf);
	sysctl(mib, 4, buf, &cb, NULL, 0);

	return FileRemoveFileName( std::string( buf ) );
#elif defined( PLATFORM_SOLARIS )
	return FileRemoveFileName( std::string( getexecname() ) );
#elif defined( PLATFORM_HAIKU )
	image_info info;
	int32 cookie = 0;

	while ( B_OK == get_next_image_info( 0, &cookie, &info ) ) {
		if ( info.type == B_APP_IMAGE )
			break;
	}

	return FileRemoveFileName( std::string( info.name ) );
#else
	#warning GetProcessPath() not implemented on this platform. ( will return "./" )
	return "./";
#endif
}
Esempio n. 26
0
char *executable_path(const char *argv0) {
    return strdup(getexecname());
}
Esempio n. 27
0
FcChar8 *FcGetPrgname(void) {
    FcChar8 *prgname;
retry:
    prgname = fc_atomic_ptr_get(&default_prgname);
    if (!prgname) {
#ifdef _WIN32
        char buf[MAX_PATH + 1];

        /* TODO This is ASCII-only; fix it. */
        if (GetModuleFileNameA(GetModuleHandle(NULL), buf,
                               sizeof(buf) / sizeof(buf[0])) > 0) {
            char *p;
            unsigned int len;

            p = strrchr(buf, '\\');
            if (p)
                p++;
            else
                p = buf;

            len = strlen(p);

            if (len > 4 && 0 == strcmp(p + len - 4, ".exe")) {
                len -= 4;
                buf[len] = '\0';
            }

            prgname = FcStrdup(p);
        }
#elif defined(HAVE_GETPROGNAME)
        const char *q = getprogname();
        if (q)
            prgname = FcStrdup(q);
        else
            prgname = FcStrdup("");
#else
#if defined(HAVE_GETEXECNAME)
        const char *p = getexecname();
#elif defined(HAVE_READLINK)
        char buf[PATH_MAX + 1];
        int len;
        char *p = NULL;

        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
        if (len != -1) {
            buf[len] = '\0';
            p = buf;
        }
#else
        char *p = NULL;
#endif
        if (p) {
            char *r = strrchr(p, '/');
            if (r)
                r++;
            else
                r = p;

            prgname = FcStrdup(r);
        }

        if (!prgname) prgname = FcStrdup("");
#endif

        if (!fc_atomic_ptr_cmpexch(&default_prgname, NULL, prgname)) {
            free(prgname);
            goto retry;
        }
    }

    if (prgname && !prgname[0]) return NULL;

    return prgname;
}
Esempio n. 28
0
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
    char *retval = NULL;
    const char *envr = NULL;

    /* Try to avoid using argv0 unless forced to. Try system-specific stuff. */
    
    #if PHYSFS_PLATFORM_FREEBSD
    {
        char fullpath[PATH_MAX];
        size_t buflen = sizeof (fullpath);
        int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
        if (sysctl(mib, 4, fullpath, &buflen, NULL, 0) != -1)
            retval = __PHYSFS_strdup(fullpath);
    }
    #elif PHYSFS_PLATFORM_SOLARIS
    {
        const char *path = getexecname();
        if ((path != NULL) && (path[0] == '/'))  /* must be absolute path... */
            retval = __PHYSFS_strdup(path);
    }
    #endif

    if (retval)
        return retval;   /* already got it. */

    /* If there's a Linux-like /proc filesystem, you can get the full path to
     *  the current process from a symlink in there.
     */

    if (access("/proc", F_OK) == 0)
    {
        retval = readSymLink("/proc/self/exe");
        if (!retval) retval = readSymLink("/proc/curproc/file");
        if (!retval) retval = readSymLink("/proc/curproc/exe");
        if (retval == NULL)
        {
            /* older kernels don't have /proc/self ... try PID version... */
            const unsigned long long pid = (unsigned long long) getpid();
            char path[64];
            const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
            if ( (rc > 0) && (rc < sizeof(path)) )
                retval = readSymLink(path);
        } /* if */
    } /* if */

    if (retval != NULL)  /* chop off filename. */
    {
        char *ptr = strrchr(retval, '/');
        if (ptr != NULL)
            *(ptr+1) = '\0';
        else  /* shouldn't happen, but just in case... */
        {
            physfs_alloc.Free(retval);
            retval = NULL;
        } /* else */
    } /* if */

    /* No /proc/self/exe, etc, but we have an argv[0] we can parse? */
    if ((retval == NULL) && (argv0 != NULL))
    {
        /* fast path: default behaviour can handle this. */
        if (strchr(argv0, '/') != NULL)
            return NULL;  /* higher level parses out real path from argv0. */

        /* If there's no dirsep on argv0, then look through $PATH for it. */
        envr = getenv("PATH");
        if (envr != NULL)
        {
            char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1);
            BAIL_IF_MACRO(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
            strcpy(path, envr);
            retval = findBinaryInPath(argv0, path);
            __PHYSFS_smallFree(path);
        } /* if */
    } /* if */

    if (retval != NULL)
    {
        /* try to shrink buffer... */
        char *ptr = (char *) physfs_alloc.Realloc(retval, strlen(retval) + 1);
        if (ptr != NULL)
            retval = ptr;  /* oh well if it failed. */
    } /* if */

    return retval;
} /* __PHYSFS_platformCalcBaseDir */
Esempio n. 29
0
    inline boost::filesystem::path program_location_impl(boost::system::error_code& ec) {
        ec.clear();

        return boost::filesystem::path(getexecname());
    }
/* Specification.  */
#include "getprogname.h"

#include <errno.h> /* get program_invocation_name declaration */
#include <stdlib.h> /* get __argv declaration */

#ifdef _AIX
# include <unistd.h>
# include <procinfo.h>
# include <string.h>
#endif

#ifdef __MVS__
# ifndef _OPEN_SYS
#  define _OPEN_SYS
# endif
# include <string.h>
# include <sys/ps.h>
#endif

#ifdef __hpux
# include <unistd.h>
# include <sys/param.h>
# include <sys/pstat.h>
# include <string.h>
#endif

#include "dirname.h"

#ifndef HAVE_GETPROGNAME             /* not Mac OS X, FreeBSD, NetBSD, OpenBSD >= 5.4, Cygwin */
char const *
getprogname (void)
{
# if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME                /* glibc, BeOS */
  /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
  return program_invocation_short_name;
# elif HAVE_DECL_PROGRAM_INVOCATION_NAME                    /* glibc, BeOS */
  /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
  return last_component (program_invocation_name);
# elif HAVE_GETEXECNAME                                     /* Solaris */
  /* http://docs.oracle.com/cd/E19253-01/816-5168/6mbb3hrb1/index.html */
  const char *p = getexecname ();
  if (!p)
    p = "?";
  return last_component (p);
# elif HAVE_DECL___ARGV                                     /* mingw, MSVC */
  /* https://msdn.microsoft.com/en-us/library/dn727674.aspx */
  const char *p = __argv && __argv[0] ? __argv[0] : "?";
  return last_component (p);
# elif HAVE_VAR___PROGNAME                                  /* OpenBSD, QNX */
  /* http://man.openbsd.org/style.9 */
  /* http://www.qnx.de/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_lib_ref%2Fp%2F__progname.html */
  /* Be careful to declare this only when we absolutely need it
     (OpenBSD 5.1), rather than when it's available.  Otherwise,
     its mere declaration makes program_invocation_short_name
     malfunction (have zero length) with Fedora 25's glibc.  */
  extern char *__progname;
  const char *p = __progname;
  return p && p[0] ? p : "?";
# elif _AIX                                                 /* AIX */
  /* Idea by Bastien ROUCARIÈS,
     http://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00095.html
     Reference: http://
   ibm.biz/knowctr#ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/getprocs.htm
  */
  static char *p;
  static int first = 1;
  if (first)
    {
      first = 0;
      pid_t pid = getpid ();
      struct procentry64 procs;
      p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1)
           ? strdup (procs.pi_comm)
           : NULL);
      if (!p)
        p = "?";
    }
  return p;
# elif defined __hpux
  static char *p;
  static int first = 1;
  if (first)
    {
      first = 0;
      pid_t pid = getpid ();
      struct pst_status status;
      p = (0 < pstat_getproc (&status, sizeof status, 0, pid)
           ? strdup (status.pst_ucomm)
           : NULL);
      if (!p)
        p = "?";
    }
  return p;
# elif __MVS__                                              /* z/OS */
  /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */
  static char *p = "?";
  static int first = 1;
  if (first)
    {
      pid_t pid = getpid ();
      int token;
      W_PSPROC buf;
      first = 0;
      memset (&buf, 0, sizeof(buf));
      buf.ps_cmdptr    = (char *) malloc (buf.ps_cmdlen    = PS_CMDBLEN_LONG);
      buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN);
      buf.ps_pathptr   = (char *) malloc (buf.ps_pathlen   = PS_PATHBLEN);
      if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr)
        {
          for (token = 0; token >= 0;
               token = w_getpsent (token, &buf, sizeof(buf)))
            {
              if (token > 0 && buf.ps_pid == pid)
                {
                  char *s = strdup (last_component (buf.ps_pathptr));
                  if (s)
                    p = s;
                  break;
                }
            }
        }
      free (buf.ps_cmdptr);
      free (buf.ps_conttyptr);
      free (buf.ps_pathptr);
    }
  return p;
# else
#  error "getprogname module not ported to this OS"
# endif
}