Forum > Unix

How to get launched app name in case of symlink

(1/2) > >>

zim:
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:
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

zim:

--- Quote from: KodeZwerg 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

--- End quote ---

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:

--- Quote from: zim on January 18, 2024, 12:42:40 pm ---The problem is that ParamStr(0) initially contains the symlink target name, not source, and Argv[0] also does

--- End quote ---
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.

PascalDragon:

--- Quote from: zim on January 17, 2024, 01:02:09 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 ?

--- End quote ---

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

Navigation

[0] Message Index

[#] Next page

Go to full version