OS2 World Community Forum

OS/2, eCS & ArcaOS - Technical => Programming => Topic started by: myrkraverk on December 11, 2020, 02:01:02 am

Title: Is there a /proc/self/exe for OS/2?
Post by: myrkraverk on December 11, 2020, 02:01:02 am
Dear OS/2 World,

As per Stack Overflow, https://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe (https://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe), there are supposedly reliable ways to get the current executable name in C.  Is there something similar in OS/2?

From what I gather, the Watcom _cmdname() function relies on argv[ 0 ] on DOS, OS/2 and Windows.  I am hoping there's something more reliable.   Or is argv[ 0 ] reliable on OS/2?  Because it's not on Unix systems.
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Lars on December 11, 2020, 08:26:14 am
It might be unreliable in the way that it might only return what you entered on the command line (actually I think this is where a compiler RTL will take the info from).
But you can try DosGetInfoSeg or you could even query the complete module table with DosQuerySysState where each module entry also points to the full name of the loaded module. But of course,the latter are not portable (are OS specific).
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Lars on December 11, 2020, 08:32:58 am
Actually you need a combination of DosGetInfoSeg and DosQuerySysState: use DosGetInfoSeg to query your own module table entry handle (hmte) and then search the module table chain returned by DosQuerySysState for this hmte.
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Andreas Schnellbacher on December 11, 2020, 03:42:43 pm
Or use DosQueryModeFromEIP for an executable and DosQueryModuleName for a DLL:
module.c by Christian for NEPMD, GPL 2 (http://svn.netlabs.org/repos/nepmd/trunk/src/gui/common/module.c)
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Lars on December 12, 2020, 04:20:30 pm
Are you sure that DosQueryModuleFromEIP and DosQueryModuleName return the full path information?
I'd say, not in general. If a DLL is loaded not by full path, only the 8 byte (at max) Module name is returned.
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Andreas Schnellbacher on December 12, 2020, 07:19:12 pm
Are you sure that DosQueryModeFromEIP and DosQueryModuleName return the full path information?
I'd say, not in general. If a DLL is loaded not by full path, only the 8 byte (at max) Module name is returned.
Your question makes me assume that I'm wrong. (Corrected the function name in your question.)

I had a look at some other code (http://svn.netlabs.org/repos/nepmd/trunk/src/gui/nepmdlib/nepmdlib.c) (_insertModuleStamp) and there first DosQueryModuleHandle is used to query the handle. With that, DosQueryModuleName is used to return the fullname. DLLs in EPM and NEPMD are loaded by their names, not fullnames.
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Rich Walsh on December 13, 2020, 04:03:39 pm
I'm surprised that no one mentioned the system-standard method for determining a process's executable. The string should be accessed during startup and saved elsewhere if needed because it might not persist.

Code: [Select]
PPIB    ppib;
char   *ptr;

// get the Process Information Block
DosGetInfoBlocks(0, &ppib);

// get a pointer to the environment
ptr = ppib->pib_pchenv;

// loop through the environment strings to find
// the null string that terminates it
while (*ptr)
    ptr = strchr(ptr, 0) + 1;

// the exe's fully-qualified filename starts at the next character
ptr++;

Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Lars on December 13, 2020, 04:29:37 pm
The system standard method might not be sufficient because for a call from command line, it will only return what was entered on the commandline. At least that is what I seem to remember.
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Andreas Schnellbacher on December 13, 2020, 10:17:10 pm
I'm surprised that no one mentioned the system-standard method for determining a process's executable.
BTW: In the cited _insertModuleStamp Christian has used DosGetInfoBlocks to get the handle of the executable (DosQueryModuleHandle for a DLL name) and then DosQueryModuleName to get the fullname. Both ways (this one and the one that I've mentioned before) get the fullname.
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Dave Yeo on December 13, 2020, 11:32:59 pm
Open32 seems to support GetModuleFileName()  according to the inf file.
Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Rich Walsh on December 14, 2020, 05:28:59 pm
The system standard method might not be sufficient because for a call from command line, it will only return what was entered on the commandline. At least that is what I seem to remember.

No. It will always provide the fully-qualified capitalized pathname. The next string is the filename as entered on the commandline, and the string after that is the commandline parameters. This never varies: if the info is available, it is presented as described. (Use Theseus4 to examine any process's environment segment - it's all right there.)

Title: Re: Is there a /proc/self/exe for OS/2?
Post by: Andreas Schnellbacher on December 14, 2020, 06:29:53 pm
It will always provide the fully-qualified capitalized pathname.
Yes, that's also that what I've seen. Thanks for pointing this out. That is really a bit easier than the other methods.

(Both methods I've mentioned give the capitalized pathname as well.)