[haiku-3rdparty-dev] Re: Can Haiku emulate Amiga CLI/GUI startup?

  • From: Michael Lotz <mmlr@xxxxxxxx>
  • To: haiku-3rdparty-dev@xxxxxxxxxxxxx
  • Date: Sun, 22 Feb 2015 10:47:39 +0100

Hi

On Feb 22, 2015 2:37 AM, Earl Pottinger <dmarc-noreply@xxxxxxxxxxxxx> wrote:
>
> ... Haiku-OS ...

Note that it's actually just called "Haiku"...

> Is there any way for a single Haiku-OS program to tell what environment 
> invokes the progam

There are a couple of ways, not specific to Haiku, that you could use. None of 
them are really perfect.

1: There's the isatty() POSIX function to determine if a file descriptor is a 
terminal. You usually give it the file descriptor number of stdin or stdout (0 
and 1 respectively according to POSIX). If stdin is connected to a TTY you 
could assume the program to be running from a terminal. Note that redirecting 
the std* FDs on the command line will throw this detection off, so if you 
expect the program to be run with redirects this isn't a good method. I 
actually haven't checked if this method works as expected under Haiku, but it 
should.

2: Make a wrapper script that supplies an argument like "--gui" to your program 
and use that in Deskbar or where you expect the user to click. This is a rather 
clunky way to do it of course.

3: Make a symlink with a different name. If the command line version of your 
program is called "myprogram" you could name the symlink for the GUI version 
"MyProgram" for example. Then you can check the name that was used to invoke 
the program by checking the (basename) value of argv[0] and vary the behaviour. 
This is how some command line tools work (BusyBox exposes its commands like 
this and determines which one was run by the name). You'd use the GUI symlink 
in places like Deskbar as above. If a user renames your program or makes a 
symlink to it with a different name, this approach will fail.

You could combine 1 and 3 to reduce the chances of misdetection.

Generally it might be considered confusing if a program changes behaviour based 
on the environment, so making actually seperate programs might be better in any 
case. To share code and reduce size, you could use a shared library from both 
versions. Or make one call the other, like lpe does for Pe, which essentially 
is what 2 does.

Regards,
Michael

Other related posts: