Recent

Author Topic: How to get launched app name in case of symlink  (Read 2730 times)

zim

  • New Member
  • *
  • Posts: 17
How to get launched app name in case of symlink
« on: January 17, 2024, 01:02:09 pm »
Hi all !

If I run application via symlink, somehow ParamStr(0) returns a physical name - i.e. symlink target, not source
Is there any API to get a source name ?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to get launched app name in case of symlink
« Reply #1 on: January 17, 2024, 02:44:51 pm »
Since you forgot to tell what OS you run your app, here is a possible way:
unit LazFileUtils contain GetPhysicalFilename(), feed that with your symbolic link (ParamStr(0)) and it returns the real path/filename
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

zim

  • New Member
  • *
  • Posts: 17
Re: How to get launched app name in case of symlink
« Reply #2 on: January 18, 2024, 12:42:40 pm »
Since you forgot to tell what OS you run your app, here is a possible way:
unit LazFileUtils contain GetPhysicalFilename(), feed that with your symbolic link (ParamStr(0)) and it returns the real path/filename

I just thought that "Unix" group should be enough :)
The problem is that ParamStr(0) initially contains the symlink target name, not source, and Argv[0] also does
I finally decided to use hardlinks, that is no so good, but at least that works.

Possibly that is some Linux internal behaviour. But maybe some API is present to get source name ?

TRon

  • Hero Member
  • *****
  • Posts: 3170
Re: How to get launched app name in case of symlink
« Reply #3 on: January 18, 2024, 02:14:52 pm »
The problem is that ParamStr(0) initially contains the symlink target name, not source, and Argv[0] also does
They are the same and yes they do provide the actual executable (not the symlink). See PascalDragon's (correct) answer, directly below this post.

For getting the symlink you can try (baseunix.)fpReadLink on /proc/<pid>/cwd to get the working directory from which the symlink was executed and then concat the result from reading a line of text from /proc/<pid>/cmdline

Depending from which directory the symlink was started the concatted answer contains a relative path (so you might have to convert it to an absolute path).

You can retrieve the pid with (baseunix.)fpGetPid or you could use self.
« Last Edit: January 19, 2024, 04:38:46 am by TRon »
All software is open source (as long as you can read assembler)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5649
  • Compiler Developer
Re: How to get launched app name in case of symlink
« Reply #4 on: January 18, 2024, 09:59:41 pm »
If I run application via symlink, somehow ParamStr(0) returns a physical name - i.e. symlink target, not source
Is there any API to get a source name ?

On *nix you can directly use argv[0] for that (cause ParamStr(0) returns /proc/self/exe).

TRon

  • Hero Member
  • *****
  • Posts: 3170
Re: How to get launched app name in case of symlink
« Reply #5 on: January 19, 2024, 04:39:53 am »
On *nix you can directly use argv[0] for that (cause ParamStr(0) returns /proc/self/exe).
Ah, then I am mistaken. I stand corrected. Thank you !
All software is open source (as long as you can read assembler)

zim

  • New Member
  • *
  • Posts: 17
Re: How to get launched app name in case of symlink
« Reply #6 on: January 23, 2024, 03:43:22 pm »
Sorry, all that stuff does not help. Either ParamStr(0) or argv[0] contains symlink target name, not source.
The problem is I want to share same executable between several tasks located in different folders and care on exact action depending on the folder.
But in the said case, even I run executable (symlinked to every specified folder) - every time I get the same app name, not different names

TRon

  • Hero Member
  • *****
  • Posts: 3170
Re: How to get launched app name in case of symlink
« Reply #7 on: January 23, 2024, 03:55:45 pm »
Sorry, all that stuff does not help. Either ParamStr(0) or argv[0] contains symlink target name, not source.
Oh, that is strange as ti works for me.

Which Linux and version of Free Pascal are you using ?

Code: [Select]
./startupper
Example to demonstrate differences between executable invoked
from command line a) directly and b) using a symbolic link

Running this executable file directly (/media/ramdisk/start/startupper)
           argv[0] = /media/ramdisk/start/startupper -- /media/ramdisk/start/startupper
       paramstr(0) = /media/ramdisk/start/startupper
/proc/self/cmdline = /media/ramdisk/start/startupper INFORM
ExpandFileName(argv[0]) = /media/ramdisk/start/startupper

---

Running this executable file indirectly using a symbolic link (some_directory/test_symlink)
           argv[0] = some_directory/test_symlink -- /media/ramdisk/start/some_directory/test_symlink
       paramstr(0) = /media/ramdisk/start/startupper
/proc/self/cmdline = some_directory/test_symlink INFORM
ExpandFileName(argv[0]) = /media/ramdisk/start/some_directory/test_symlink

---
TODO: clean-up the mess and fix expandfilename usage
All software is open source (as long as you can read assembler)

 

TinyPortal © 2005-2018