finding executable's own filename
finding executable's own filename
i'm looking for a way to obtain the executable's filename of the current process. particularly for various types of Unix (hints for Windows are also welcome).
this would be useful to locate files associated with a program.
is there a general Unix way to get the filename? or at least different ways for different Unixes? please share any that you know.
i know there's the /proc filesystem for Linux, but that isn't very portable. i guess it can be used if there are no better alternatives..
looking at argv [ 0 ] - even less clean, because it's likely incomplete, and has to be reconstructed - tricky and unreliable.
for Windows, GetModuleFileName() can be used, afaik.
this would be useful to locate files associated with a program.
is there a general Unix way to get the filename? or at least different ways for different Unixes? please share any that you know.
i know there's the /proc filesystem for Linux, but that isn't very portable. i guess it can be used if there are no better alternatives..
looking at argv [ 0 ] - even less clean, because it's likely incomplete, and has to be reconstructed - tricky and unreliable.
for Windows, GetModuleFileName() can be used, afaik.
Re:finding executable's own filename
argv[ 0 ].zloba wrote: is there a general Unix way to get the filename?
Inhowfar incomplete? Why would you have to reconstruct it? If you're looking for a fully qualified file name (instead of a relative one), you might want to use getenv( "PWD" ) to get the current directory - at least under Unixes.looking at argv [ 0 ] - even less clean, because it's likely incomplete, and has to be reconstructed - tricky and unreliable.
Every good solution is obvious once you've found it.
Re:finding executable's own filename
completely incomplete. when you invoke a program in the $PATH, using only the name - that's what argv [ 0 ] will be, without path, or it could be a relative path.argv [ 0 ] - Inhowfar incomplete?
i would have to reconstruct it using the same algorithm shell used to run the binary, and then what if it's a symlink?
exactly.If you're looking for a fully qualified file name (instead of a relative one),
that's current working directory, it has exactly nothing to do with the executable path, unless you invoke it as ./path/to/binaryyou might want to use getenv( "PWD" ) to get the current directory - at least under Unixes.
when you invoke "ls", do you cd /bin ?
quoting myself,Why would you have to reconstruct it?
such as resource files, bundled binaries, libraries, etc.this would be useful to locate files associated with a program.
Re:finding executable's own filename
i suppose there isn't a "general unix way", not a clean one anyway...
Re:finding executable's own filename
I believe the generic way to allocate resources for your file is to compile it with a prefix-dir (such as "/usr/", "/", "/usr/local/" or "c:\Program Files") and to append your program name and then to put your files there.zloba_guest wrote: i suppose there isn't a "general unix way", not a clean one anyway...
Re:finding executable's own filename
Ah, OK.zloba_guest wrote: completely incomplete. when you invoke a program in the $PATH, using only the name - that's what argv [ 0 ] will be, without path, or it could be a relative path.
Then you're approaching this wrongly, at least for the Unix world. Unix has a path for binaries, it has a path for libraries, and ressource files are ususally located somewhere in a compiled-in path where your program should be looking for them.such as resource files, bundled binaries, libraries, etc.this would be useful to locate files associated with a program.
Other than that, there's "which".
I know this all doesn't really sound helpful, but AFAIK Unix doesn't cater for something like [tt]PROGDIR:[/tt] under AmigaOS...
Every good solution is obvious once you've found it.
Re:finding executable's own filename
Then you're approaching this wrongly, at least for the Unix world. Unix has a path for binaries, it has a path for libraries, and ressource files are ususally located somewhere in a compiled-in path where your program should be looking for them.
that's what i'm trying to avoid. it's like global variables vs. objects..I believe the generic way to allocate resources for your file is to compile it with a prefix-dir
i'm thinking about wrapper binaries or scripts that would pass certain information to the main executable, via environment or arguments.but AFAIK Unix doesn't cater for something like PROGDIR: under AmigaOS...
<rant:>
it's not just about resources, but more importantly, config and data - so that there may be multiple instances of a program and multiple instances of data, independently of each other (executable path isn't good for data, unless there is a mapping {program-instance,user}=>data)
this is largely inspired by running various versions of things like Apache and MySQL simultaneously and with separate instances of data. it can be accomplished by pointing them to a specific config file, preferrably without having to do so by hand each time.
MySQL wants to be started from within the directory where it was unpacked - apparently that's how it finds itself.
funny how programs distributed as binaries manage to work well and be portable (location-independent and multi-platform), unlike pure Unix-way programs.
Re:finding executable's own filename
Btw, you could also take the content of argv[ 0 ] and give it to 'which'. This will always give you the absolute path of your executable, but it depends on 'which' to be installed....I can think of several programs that do so, however.
cheers Joe
cheers Joe
Re:finding executable's own filename
then i would have to fork a child, handle its input..Btw, you could also take the content of argv[ 0 ] and give it to 'which'. This will always give you the absolute path of your executable
and "which" doesn't handle symlinks, or relative filenames - only those in $PATH.
i think it will have to be wrappers - like Windows shortcuts with their options. perhaps using a special #!interpreter, so they can contain data instead of code.
Re:finding executable's own filename
It's a lot of work for a 'trivial' task, yes..zloba wrote: then i would have to fork a child, handle its input..
That's not true: 'which' handles relative filenames just fine, I tested this before posting. How should it 'handle' symlinks? It treats symlinks to directories as seperate directories, but isn't this the expected behaviour?zloba wrote: and "which" doesn't handle symlinks, or relative filenames - only those in $PATH.
cheers Joe
Re:finding executable's own filename
'handle' meaning dereferencing them, at least when it comes to the final executable.'which' handles relative filenames just fine (..) How should it 'handle' symlinks?
i was looking for a fully qualified (or at least relative) path to the final binary (its actual base directory).
Code: Select all
$ make hello
cc hello.c -o hello
$ mkdir subdir
$ cp hello subdir/hello
$ ln -s subdir/hello hello-link
$ which ./hello-link
./hello-link
# ^ desired: location of hello (the target executable)
# whether absolute or at least relative, so it can be associated with subdir where it is
i guess this is how it's executed - dereferencing is done by the OS routines when actually loading the file - but it's not very useful as such.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:finding executable's own filename
well, considering the possible complexity of a "generic" approach compared to reading link value of "/proc/self/exe", i'd suggest you detect at compile time whether "/proc/self/exe" works and fall back to "readlink `which $ARGV[0]`" when the former is not available.