Next: Graphical Interfaces, Previous: User Interfaces, Up: Program Behavior [Contents][Index]
A program may need to find the executable file it was started with, so as to relaunch the same program. It may need to find associated files, either source files or files constructed by building, that it uses at run time.
The way to find them starts with looking at argv[0]
.
If that string contains a slash, it is by convention the file name of
the executable and its directory part is the directory that contained
the executable. This is the case when the program was not found
through PATH
, which normally means it was built but not
installed, and run from the build directory. The program can use the
argv[0]
file name to relaunch itself, and can look in its
directory part for associated files. If that file name is not
absolute, then it is relative to the working directory in which the
program started.
If argv[0]
does not contain a slash, it is a command name whose
executable was found via PATH
. The program should search for
that name in the directories in PATH
, interpreting . as
the working directory that was current when the program started.
If this procedure finds the executable, we call the directory it was found in the invocation directory. The program should check for the presence in that directory of the associated files it needs.
If the program’s executable is normally built in a subdirectory of the main build directory, and the main build directory contains associated files (perhaps including subdirectories), the program should look at the parent of the invocation directory, checking for the associated files and subdirectories the main build directory should contain.
If the invocation directory doesn’t contain what’s needed, but the executable file name is a symbolic link, the program should try using the link target’s containing directory as the invocation directory.
If this procedure doesn’t come up with an invocation directory that is
valid—normally the case for an installed program that was found via
PATH
—the program should look for the associated files in the
directories where the program’s makefile installs them.
See Directory Variables.
Providing valid information in argv[0]
is a convention, not
guaranteed. Well-behaved programs that launch other programs, such as
shells, follow the convention; your code should follow it too, when
launching other programs. But it is always possible to launch the
program and give a nonsensical value in argv[0]
.
Therefore, any program that needs to know the location of its executable, or that of of other associated files, should offer the user environment variables to specify those locations explicitly.
Don’t give special privilege, such as with the setuid
bit, to programs that will search heuristically for associated files
or for their own executables when invoked that way. Limit that
privilege to programs that find associated files in hard-coded
installed locations such as under /usr and /etc.
See Bourne Shell Variables in Bash Reference Manual,
for more information about PATH
.
Next: Graphical Interfaces, Previous: User Interfaces, Up: Program Behavior [Contents][Index]