Operating System - HP-UX
1827842 Members
1333 Online
109969 Solutions
New Discussion

C/C++: how to retrieve executable name and path

 
Peter Hug
Advisor

C/C++: how to retrieve executable name and path

In Windows I can use the api GetModuleFileName() and in Linux I can make a call to readlink("/proc/self/exe", ...) and realpath(...) to get these details at runtime, but what can I use in HP-UX?

While I could work this out programmatically using argv[0] and the PATH environment variable, this is not suitable as I need the info during static initialisation. However, if there was a way to access the commandline arguments before entering main() this would be an option.

Of course, even better would be a portable way to doing this - but for now I'd be wrapped if I had a reliable way in HP-UX.
4 REPLIES 4
Dan_334
New Member

Re: C/C++: how to retrieve executable name and path

i dont understand what you are trying to say???????
Ermin Borovac
Honored Contributor

Re: C/C++: how to retrieve executable name and path

From http://www.faqs.org/faqs/unix-faq/programmer/faq/

1.14 How can I find a process' executable file?
===============================================

This would be a good candidate for a list of `Frequently Unanswered
Questions', because the fact of asking the question usually means that the
design of the program is flawed. :-)

You can make a `best guess' by looking at the value of `argv[0]'. If this
contains a `/', then it is probably the absolute or relative (to the
current directory at program start) path of the executable. If it does
not, then you can mimic the shell's search of the `PATH' variable, looking
for the program. However, success is not guaranteed, since it is possible
to invoke programs with arbitrary values of `argv[0]', and in any case the
executable may have been renamed or deleted since it was started.

If all you want is to be able to print an appropriate invocation name with
error messages, then the best approach is to have `main()' save the value
of `argv[0]' in a global variable for use by the entire program. While
there is no guarantee whatsoever that the value in `argv[0]' will be
meaningful, it is the best option available in most circumstances.

The most common reason people ask this question is in order to locate
configuration files with their program. This is considered to be bad form;
directories containing executables should contain *nothing* except
executables, and administrative requirements often make it desirable for
configuration files to be located on different filesystems to executables.

A less common, but more legitimate, reason to do this is to allow the
program to call `exec()' *on itself*; this is a method used (e.g. by some
versions of `sendmail') to completely reinitialise the process (e.g. if a
daemon receives a `SIGHUP').
Peter Hug
Advisor

Re: C/C++: how to retrieve executable name and path

> This would be a good candidate for a list of
> `Frequently Unanswered Questions', because
> the fact of asking the question usually
> means that the design of the program is
> flawed. :-)

What a lousy argument. Good software offers choices, regardless of whether or not someone likes a certain choice. I will argue that those who make such silly statements just deny the existence of any flaws in Unix.

In the meantime I found the answer:

#include
#include
#include

int main ()
{
char *szExecPath;
char szFullPath[PATH_MAX+1];

if ((szExecPath = getenv("_")) == NULL)
{
fprintf(stderr, "Could not read path from environment.\n");
exit(EXIT_FAILURE);
}

if (realpath(szExecPath, szFullPath) == NULL)
{
fprintf(stderr, "Error resolving full path.\n");
exit(EXIT_FAILURE);
}

printf("Path is: %s\n", szExecPath);
printf("Full path is: %s\n", szFullPath);

return 0;
}
Thomas Bianco
Honored Contributor

Re: C/C++: how to retrieve executable name and path

you might take a look at the Gnu programs that run differently based on how they are called. GNU Grep (aka eGrep fGrep etc...)and gzip (aka gunzip gzcat) have been ported to HPUX and exhibit this beheviour.

i can see that a program looking for it's config files based on how this was called could be bad planning, but there are legitimate reasons to know how this was exec'd.

from a totally academic perspective, any program should be able to run without knowing anything about itself, but that's not to say it's always practicable to do this.

There have been Innumerable people who have helped me. Of course, I've managed to piss most of them off.