delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/07/06:07:50

Date: Sun, 7 Sep 1997 13:03:07 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: "A. Sinan Unur" <asu1 AT cornell DOT edu>
cc: djgpp AT delorie DOT com
Subject: Re: Determining the path of the executable at runtime
In-Reply-To: <340C8E59.C8A019DF@cornell.edu>
Message-ID: <Pine.SUN.3.91.970907130243.2248C-100000@is>
MIME-Version: 1.0

On Tue, 2 Sep 1997, A. Sinan Unur wrote:

> int main(int argc, char* argv[])
> {
>   printf("%s\n", argv[0]);
>   return 0;
> }
> 
> i do not know how ansi it is to return the full path in addition to the name
> of the executable.

It's not.  ANSI leaves this very vague indeed.  It says argv[0] is the
name of the program, but doesn't specify how precise that name should
be.  It even allows the name to be an empty string "".

On a more practical note, some Unix shells only return the program
name as typed on the command line.  For example, if you typed "foo bar"
and the program `foo' was found along the PATH, you will only have
"foo" as argv[0].  Programs that need to know the exact file name
of their executable should also look along the PATH to handle such
shells (for example, Emacs does that at startup).

As another example, DOS programs that run on Windows 95, only get the
short version of their pathname as argv[0].  So if you need the long
variety, you will have to write some non-trivial code (this is left as
an exercise to an interested reader ;-).

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019