[comp.sys.ibm.pc] Where oh where does my program reside??

pa1256@sdcc13.ucsd.edu (Jennifer's Lover) (10/24/89)

I have a question regarding how to pull some information out of DOS.

I want to know, regardless of the current directory, what directory
my program resides in.

For Example:

If I am in the directory C:\bogus and I run the program foo which
resides in the directory C:\goodstuf, (which, incidentally I can do
because C:\goodstuf is in my path statement) I want to call a function
which will return the string "C:\goodstuf\foo.exe".

The reason I want foo.exe to know where it was called from is so
that I can look in the same directory for other files which it
needs.  I know this can be done other ways, but I want to do it this
way.

This is what Word-Perfect does when it displays the message:
The system is using C:\WP50.

Can anyone give me a hand??

By the way, I am using Turbo C 2.0 and DOS 3.xx.

Thanks,
-Don Woodward

pa1256@sdcc13.ucsd.edu

jfbruno@rodan.acs.syr.edu (John F. Bruno) (10/25/89)

In article <1407@sdcc13.ucsd.edu> pa1256@sdcc13.ucsd.edu (Jennifer's Lover) writes:
>I have a question regarding how to pull some information out of DOS.
>
>I want to know, regardless of the current directory, what directory
>my program resides in.
>
>  [deleted stuff...]
>
>By the way, I am using Turbo C 2.0 and DOS 3.xx.
>
>Thanks,
>-Don Woodward
>

Try this:

#include <stdio.h>
 
void main (int argc,char *argv[],char *env[]) {
  printf("My name is %s\n.",argv[0]);
}

- John Bruno (jfbruno@sunrise.acs.syr.edu)

fredex@cg-atla.UUCP (Fred Smith) (10/25/89)

In article <1407@sdcc13.ucsd.edu> pa1256@sdcc13.ucsd.edu (Jennifer's Lover) writes:
>I have a question regarding how to pull some information out of DOS.
>
>I want to know, regardless of the current directory, what directory
>my program resides in.
>
>This is what Word-Perfect does when it displays the message:
>The system is using C:\WP50.
>
>Can anyone give me a hand??
>
>By the way, I am using Turbo C 2.0 and DOS 3.xx.




At MS(PC)-dos beginning at 3.00, the segment which contains the environment of the
current program also cotains the full pathname of the program
being executed. This is passed thru as argv[0] by Microsoft C. Perhaps Turbo
C does the same.

NOTE that this feature is available only at DOS 3.0 and later. In previous
versions the name or path of the executing program is NOT AVAILABLE.

I have always been told that this information lives immediately following the
environment in your psp. I just looked at it, however, in Microsoft C/Codeview
and in my test case, at least, it actually precedes it. MS C passes a third
argument to main, envp, which points to a pointer to the environment string.
In the case I just checked, the next word following the pointer to the environment
is a pointer to the command head, i.e., the full path of the program executing.

Hope this helps you!

Fred

vgopal@cbnewsc.ATT.COM (venu.p.gopal) (10/25/89)

In article <1407@sdcc13.ucsd.edu> pa1256@sdcc13.ucsd.edu (Jennifer's Lover) writes:
>I have a question regarding how to pull some information out of DOS.
>I want to know, regardless of the current directory, what directory
>my program resides in.
>For Example:
>If I am in the directory C:\bogus and I run the program foo which
>resides in the directory C:\goodstuf, (which, incidentally I can do
>because C:\goodstuf is in my path statement) I want to call a function
>which will return the string "C:\goodstuf\foo.exe".

If you are running DOS 3.0 or greater, you can get this information
directly from the PSP area.  On DOS 2.x this is not possible.  One
method is to read the path, then search it in the same order as DOS
to find your program.  Much easier in DOS 3.0.

Venu P. Gopal   UUCP: ..!att!ihuxy!vgopal   Internet: vgopal@ihuxy.att.com
BITNET: com%"vgopal@ihuxy.att.com"   or   com%"vgopal%ihuxy@research.att.com"
Silence those silent letters, save the world 500 million keystrokes a day.

jbeard@ntvax.uucp (Jeff Beardsley) (10/26/89)

In article <1407@sdcc13.ucsd.edu> pa1256@sdcc13.ucsd.edu (Jennifer's Lover) writes:
>I have a question regarding how to pull some information out of DOS.
>
>I want to know, regardless of the current directory, what directory
>my program resides in.
>

Well, since you are using C, this is no problem.
The following code fragment will print out your program's pathname:

main(argc,argv)
int argc; char **argv;
{
	printf(argv[0]);
}

This will be a Full pathname, in the normal form:
"C:\USR\JBEARD\TEST.EXE", etc, 

Since you are in DOS 3.x this is usable, previous versions would print
"C:TEST.EXE".

-- 
-------- Jeff Beardsley at UNT ----------- <jbeard@dept.csci.unt.edu> --------

bobmon@iuvax.cs.indiana.edu (RAMontante) (10/26/89)

Assuming Turbo C and DOS >= 3.0, the functions fnsplit() and fnmerge()
are convenient to use with argv[0] --- they let you slice and dice the
path into pieces, and recombine, as desired.