Author Topic: Is there a /proc/self/exe for OS/2?  (Read 1150 times)

Johann 'Myrkraverk' Oskarsson

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Is there a /proc/self/exe for OS/2?
« 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, 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.

Lars

  • Hero Member
  • *****
  • Thank You
  • -Given: 1
  • -Receive: 75
  • Posts: 786
  • Karma: +22/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #1 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).
« Last Edit: December 11, 2020, 08:28:30 am by Lars »

Lars

  • Hero Member
  • *****
  • Thank You
  • -Given: 1
  • -Receive: 75
  • Posts: 786
  • Karma: +22/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #2 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.

Andreas Schnellbacher

  • Hero Member
  • *****
  • Thank You
  • -Given: 1
  • -Receive: 35
  • Posts: 686
  • Karma: +6/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #3 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

Lars

  • Hero Member
  • *****
  • Thank You
  • -Given: 1
  • -Receive: 75
  • Posts: 786
  • Karma: +22/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #4 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.

Andreas Schnellbacher

  • Hero Member
  • *****
  • Thank You
  • -Given: 1
  • -Receive: 35
  • Posts: 686
  • Karma: +6/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #5 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 (_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.

Rich Walsh

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 14
  • Posts: 138
  • Karma: +2/-0
  • ONU! (OS/2 is NOT Unix!)
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #6 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++;


Lars

  • Hero Member
  • *****
  • Thank You
  • -Given: 1
  • -Receive: 75
  • Posts: 786
  • Karma: +22/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #7 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.

Andreas Schnellbacher

  • Hero Member
  • *****
  • Thank You
  • -Given: 1
  • -Receive: 35
  • Posts: 686
  • Karma: +6/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #8 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.

Dave Yeo

  • Hero Member
  • *****
  • Thank You
  • -Given: 13
  • -Receive: 208
  • Posts: 2995
  • Karma: +38/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #9 on: December 13, 2020, 11:32:59 pm »
Open32 seems to support GetModuleFileName()  according to the inf file.

Rich Walsh

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 14
  • Posts: 138
  • Karma: +2/-0
  • ONU! (OS/2 is NOT Unix!)
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #10 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.)


Andreas Schnellbacher

  • Hero Member
  • *****
  • Thank You
  • -Given: 1
  • -Receive: 35
  • Posts: 686
  • Karma: +6/-0
    • View Profile
Re: Is there a /proc/self/exe for OS/2?
« Reply #11 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.)
« Last Edit: December 14, 2020, 06:36:46 pm by Andreas Schnellbacher »