delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/07/12:02:27

Newsgroups: comp.os.msdos.djgpp
From: fredex AT fcshome DOT stoneham DOT ma DOT us (Fred Smith)
Subject: Re: Determining the path of the executable at runtime
Organization: None!
Message-ID: <EFy4su.GBI@fcshome.stoneham.ma.us>
References: <927cd$11e31 DOT 3dd AT news DOT eznet DOT net>
Date: Wed, 3 Sep 1997 19:00:28 GMT
Lines: 33
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Karl Garrison (karlos AT eznet DOT net) wrote:
: Does anyone know how I can find out what directory a program was invoked
: from?  I would perfer an ANSI-C method, but, failing that, a
: DJGPP-specific method would be acceptable.  Specifically:

: I am making a HTML editor that will look for configuration files and the
: like in the same directory as the executable.  I don't want to force the
: user to install in a particular directory, or make them add enviroment
: variables to their autoexec.bat.

This question seems to come up something like monthly in one news group
or another. Unfortunately, the short answer is that there IS no portable
way to do this. In fact depending on your platform there may be no 
guaranteed correct way to do it at all even if you leave portability
out of the picture.

On MSDOS >= 3.x, argv[0] is generally the full pathname of the program
being run, which is either what you typed when you ran it, or based
on a PATH search if you did not specify the absolute location. On 
earlier systems argv[0] may be empty (I'm not sure). When run on
Windoze I've no idea what argv[0] is. Unix platforms generally do
not put the full pathname in argv[0], so you've got to make assumptions.
One thing you can do is to search the PATH environment, but that for
sure is not robust (what if you have a program named FOO in the path,
but you instead ran a different program, from a different directory,
of the same name??)

Fred
--
---- Fred Smith -- fredex AT fcshome DOT stoneham DOT ma DOT us -- fred AT computrition DOT com ----
                      The eyes of the Lord are everywhere, 
                    keeping watch on the wicked and the good.
----------------------------- Proverbs 15:3 (niv) -----------------------------

- Raw text -


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