public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* where my executable is
@ 2001-09-24  0:35 Anni Hienola
  2001-09-28 17:21 ` Alexandre Oliva
  0 siblings, 1 reply; 3+ messages in thread
From: Anni Hienola @ 2001-09-24  0:35 UTC (permalink / raw)
  To: gcc-help

Is there a function that would give the absolute path to the directory
where my executable was executed from?

I've been searching the documentations and tutorials throughout the web
for this, without any success. It sounds so trivial that I wonder, why
it so hard to get.


Anni Hienola

^ permalink raw reply	[flat|nested] 3+ messages in thread
* Re: where my executable is
@ 2001-09-24  2:32 Michael Veksler
  0 siblings, 0 replies; 3+ messages in thread
From: Michael Veksler @ 2001-09-24  2:32 UTC (permalink / raw)
  To: gcc-help

> Is there a function that would give the absolute path to the directory
> where my executable was executed from?

If your main looks like: int main(int argc, char *argv[])
Then you normally can use argv[0] (no guarantee).

Here is pseudocode (I do something similar in ksh):

// Finds the full path version of "dir"
char* resolve_dir(char *dir)
{
   if (dir starts with '/'):
      return dir
   else:
     return getcwd()+'/'+dir
}

if (argv[0] contains '/'):
   return resolve_dir(the directory part of argv[0]);


// Search through $PATH;
name=argv[0]; // note that name contains no '/'
foreach element in $PATH:
   if exists executable file element+'/'+name:
      return resolve_dir(element+'/'+name);

// Nothing found
error "can't find the directory, the caller did not pass relevant info"



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-09-28 17:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-24  0:35 where my executable is Anni Hienola
2001-09-28 17:21 ` Alexandre Oliva
2001-09-24  2:32 Michael Veksler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).