views:

137

answers:

5

Is there a 'Posixy' way to open an URL, preferrably in the default browser? I would like to do something like

ShellExecute(0, _T("open"), url, 0, 0, SW_SHOWDEFAULT);

that works on GNU/Linux and MAC. I read some answer saying that`

if (fork() == 0)
    system("sensible-browser http://wherever.com");

does the trick on Debian systems at least. Is there an easy way to extend this to something that works on other distributions and Mac OS X?

A: 

One way might be the $BROWSER variable. Xorg provides xrdb, which is the preffered way for this environment, but this wont get you anywhere on Mac OS X, I assume.

pmr
+1  A: 

A user's browser preference is not really something the POSIX standard concerns itself with that I'm aware of.

Azeem.Butt
+2  A: 

On a Mac, you can just use the open command. open http://www.google.com from the Terminal opens a new Chrome tab for me. Just wrap that up in a system call.

eduffy
A: 

There’s no portable way to achieve this. On Mac OS X the solution would be to use LSOpenCFURLRef or other LaunchServices API to open the URL with the default handler for the URL scheme. Other platforms will do things differently.

bdash
+1  A: 

On Linux you should try launching your URL with the xdg-open command. Newer distributions should launch the user's preferred browser when that command is called. It'll even try to launch links if there's no GUI running.

Obviously there's nothing in POSIX with respect to a GUI desktop environment. Projects like freedesktop.org exist to fill that role. It attempts to define some basic functionality that will work in all Unix-like desktops, such as GNOME and KDE.

Doing fork() and calling system(), instead of exec() is undefined behavior on POSIX, so I don't recommend that.

karunski
So system() is only defined for parent processes? I'm not saying your wrong but I don't understand why since a forked process has its own address space and all. To me that's like saying you cant use printf() after you fork.
SiegeX
I can't see why system() is undefined behavior on POSIX after fork() either, can you cite the relevant part of the POSIX standard? Thanks.
Chris Young
Fork() must be followed by exec(). In between you may only call async-signal-safe functions. System() is not guaranteed to be async signal safe, neither is printf.
karunski
See: http://www.opengroup.org/onlinepubs/000095399/functions/fork.html
karunski
Calling system() after a fork doesn't make sense. You could do fork, exec, and then system(), but then it's redundant.
karunski
From the fork() manual: "When a programmer is writing a multi-threaded program, the first described use of fork(), creating new threads in the same program, is provided by the pthread_create() function. The fork() function is thus used only to run new programs, and the effects of calling functions that require certain resources between the call to fork() and the call to an exec function are undefined."
karunski
I believe that is only true for multi-threaded programs, though.
ephemient
Strictly speaking, yes the restriction only applies to multi-threaded programs. But as a practical matter other OS code, such as the C library, or any other framework, could be making threads implicitly. Note the manual for fork() on OS X makes no distinction for multi-threaded programs.
karunski