How to get the pathname of the current executable?

bethge at wums.wustl.edu bethge at wums.wustl.edu
Thu Feb 8 06:54:43 AEST 1990


I am a 10+ year veteran of VMS programming who is trying to learn to
like Unix.  (Really!)  It would help if I could find out how to port
some of my favorite VMS tricks.

I like to write programs that users can use without having  to  know
details  of  their  inner  workings.   Suppose  a program needs some
standard data which the user doesn't need to be concerned with,  and
which  for  various reasons needs to be read from a file rather than
compiled in.  The question is, how does the program find the file?

My VMS solution is to keep the file in the  same  directory  as  the
program  executable,  and  use  the system service which returns the
full pathname of the cuurrently  running  executable,  and  get  the
disk,  directory, etc.  from that.  But I don't know of a comparable
system routine in Unix.

I have looked at Unix programs which deal  with  this  problem,  and
found  that  the  pathname  for the data file is hard-coded into the
program.  This of course means that the program has to be edited and
recompiled if it becomes necessary to move the file.

Environment variables are a better solution, but  they  require  the
user  to define the environment variable before running the program.
I could define the program as  a  shell  script  which  defines  the
environment  variable  and  then fires up the executable, but that's
one more file to maintain.

Is there a better (more transparent) way?

____________________________________________________________________
Paul H. Bethge                                 bethge at wums.wustl.edu
Washington University, St. Louis                  bethge at wums.bitnet



More information about the Comp.unix.questions mailing list