<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: C/C++: how to retrieve executable name and path in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445388#M851728</link>
    <description>From &lt;A href="http://www.faqs.org/faqs/unix-faq/programmer/faq/" target="_blank"&gt;http://www.faqs.org/faqs/unix-faq/programmer/faq/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;1.14 How can I find a process' executable file?&lt;BR /&gt;===============================================&lt;BR /&gt;&lt;BR /&gt;This would be a good candidate for a list of `Frequently Unanswered&lt;BR /&gt;Questions', because the fact of asking the question usually means that the&lt;BR /&gt;design of the program is flawed. :-)&lt;BR /&gt;&lt;BR /&gt;You can make a `best guess' by looking at the value of `argv[0]'.  If this&lt;BR /&gt;contains a `/', then it is probably the absolute or relative (to the&lt;BR /&gt;current directory at program start) path of the executable.  If it does&lt;BR /&gt;not, then you can mimic the shell's search of the `PATH' variable, looking&lt;BR /&gt;for the program.  However, success is not guaranteed, since it is possible&lt;BR /&gt;to invoke programs with arbitrary values of `argv[0]', and in any case the&lt;BR /&gt;executable may have been renamed or deleted since it was started.&lt;BR /&gt;&lt;BR /&gt;If all you want is to be able to print an appropriate invocation name with&lt;BR /&gt;error messages, then the best approach is to have `main()' save the value&lt;BR /&gt;of `argv[0]' in a global variable for use by the entire program.  While&lt;BR /&gt;there is no guarantee whatsoever that the value in `argv[0]' will be&lt;BR /&gt;meaningful, it is the best option available in most circumstances.&lt;BR /&gt;&lt;BR /&gt;The most common reason people ask this question is in order to locate&lt;BR /&gt;configuration files with their program. This is considered to be bad form;&lt;BR /&gt;directories containing executables should contain *nothing* except&lt;BR /&gt;executables, and administrative requirements often make it desirable for&lt;BR /&gt;configuration files to be located on different filesystems to executables.&lt;BR /&gt;&lt;BR /&gt;A less common, but more legitimate, reason to do this is to allow the&lt;BR /&gt;program to call `exec()' *on itself*; this is a method used (e.g. by some&lt;BR /&gt;versions of `sendmail') to completely reinitialise the process (e.g. if a&lt;BR /&gt;daemon receives a `SIGHUP').</description>
    <pubDate>Fri, 17 Dec 2004 00:29:39 GMT</pubDate>
    <dc:creator>Ermin Borovac</dc:creator>
    <dc:date>2004-12-17T00:29:39Z</dc:date>
    <item>
      <title>C/C++: how to retrieve executable name and path</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445386#M851726</link>
      <description>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?&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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.</description>
      <pubDate>Thu, 16 Dec 2004 16:00:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445386#M851726</guid>
      <dc:creator>Peter Hug</dc:creator>
      <dc:date>2004-12-16T16:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: C/C++: how to retrieve executable name and path</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445387#M851727</link>
      <description>i dont understand what you are trying to say???????</description>
      <pubDate>Fri, 17 Dec 2004 00:17:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445387#M851727</guid>
      <dc:creator>Dan_334</dc:creator>
      <dc:date>2004-12-17T00:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: C/C++: how to retrieve executable name and path</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445388#M851728</link>
      <description>From &lt;A href="http://www.faqs.org/faqs/unix-faq/programmer/faq/" target="_blank"&gt;http://www.faqs.org/faqs/unix-faq/programmer/faq/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;1.14 How can I find a process' executable file?&lt;BR /&gt;===============================================&lt;BR /&gt;&lt;BR /&gt;This would be a good candidate for a list of `Frequently Unanswered&lt;BR /&gt;Questions', because the fact of asking the question usually means that the&lt;BR /&gt;design of the program is flawed. :-)&lt;BR /&gt;&lt;BR /&gt;You can make a `best guess' by looking at the value of `argv[0]'.  If this&lt;BR /&gt;contains a `/', then it is probably the absolute or relative (to the&lt;BR /&gt;current directory at program start) path of the executable.  If it does&lt;BR /&gt;not, then you can mimic the shell's search of the `PATH' variable, looking&lt;BR /&gt;for the program.  However, success is not guaranteed, since it is possible&lt;BR /&gt;to invoke programs with arbitrary values of `argv[0]', and in any case the&lt;BR /&gt;executable may have been renamed or deleted since it was started.&lt;BR /&gt;&lt;BR /&gt;If all you want is to be able to print an appropriate invocation name with&lt;BR /&gt;error messages, then the best approach is to have `main()' save the value&lt;BR /&gt;of `argv[0]' in a global variable for use by the entire program.  While&lt;BR /&gt;there is no guarantee whatsoever that the value in `argv[0]' will be&lt;BR /&gt;meaningful, it is the best option available in most circumstances.&lt;BR /&gt;&lt;BR /&gt;The most common reason people ask this question is in order to locate&lt;BR /&gt;configuration files with their program. This is considered to be bad form;&lt;BR /&gt;directories containing executables should contain *nothing* except&lt;BR /&gt;executables, and administrative requirements often make it desirable for&lt;BR /&gt;configuration files to be located on different filesystems to executables.&lt;BR /&gt;&lt;BR /&gt;A less common, but more legitimate, reason to do this is to allow the&lt;BR /&gt;program to call `exec()' *on itself*; this is a method used (e.g. by some&lt;BR /&gt;versions of `sendmail') to completely reinitialise the process (e.g. if a&lt;BR /&gt;daemon receives a `SIGHUP').</description>
      <pubDate>Fri, 17 Dec 2004 00:29:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445388#M851728</guid>
      <dc:creator>Ermin Borovac</dc:creator>
      <dc:date>2004-12-17T00:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: C/C++: how to retrieve executable name and path</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445389#M851729</link>
      <description>&amp;gt; This would be a good candidate for a list of&lt;BR /&gt;&amp;gt; `Frequently Unanswered Questions', because&lt;BR /&gt;&amp;gt; the fact of asking the question usually&lt;BR /&gt;&amp;gt; means that the design of the program is&lt;BR /&gt;&amp;gt; flawed. :-)&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;In the meantime I found the answer:&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDLIB.H&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;LIMITS.H&gt;&lt;BR /&gt;&lt;BR /&gt;int main ()&lt;BR /&gt;{&lt;BR /&gt;  char *szExecPath;&lt;BR /&gt;  char szFullPath[PATH_MAX+1];&lt;BR /&gt;  &lt;BR /&gt;  if ((szExecPath = getenv("_")) == NULL)&lt;BR /&gt;  {&lt;BR /&gt;    fprintf(stderr, "Could not read path from environment.\n");&lt;BR /&gt;    exit(EXIT_FAILURE);&lt;BR /&gt;  }&lt;BR /&gt;&lt;BR /&gt;  if (realpath(szExecPath, szFullPath) == NULL)&lt;BR /&gt;  {&lt;BR /&gt;    fprintf(stderr, "Error resolving full path.\n");&lt;BR /&gt;    exit(EXIT_FAILURE);&lt;BR /&gt;  }&lt;BR /&gt;  &lt;BR /&gt;  printf("Path is: %s\n", szExecPath);&lt;BR /&gt;  printf("Full path is: %s\n", szFullPath);&lt;BR /&gt;&lt;BR /&gt;  return 0;&lt;BR /&gt;}&lt;/LIMITS.H&gt;&lt;/STDIO.H&gt;&lt;/STDLIB.H&gt;</description>
      <pubDate>Fri, 17 Dec 2004 02:28:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445389#M851729</guid>
      <dc:creator>Peter Hug</dc:creator>
      <dc:date>2004-12-17T02:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: C/C++: how to retrieve executable name and path</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445390#M851730</link>
      <description>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. &lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 17 Dec 2004 08:26:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-c-how-to-retrieve-executable-name-and-path/m-p/3445390#M851730</guid>
      <dc:creator>Thomas Bianco</dc:creator>
      <dc:date>2004-12-17T08:26:50Z</dc:date>
    </item>
  </channel>
</rss>

