7

How do I programmatically open a file in its default program in Linux (im using Ubuntu 10.10).

For example, opening *.mp3 will open the file in Movie Player (or something else).

1
  • Ubuntu by default comes with firefox and if you can run firefox command from your terminal .... you can make it right.... Have a look on my solution....and see if this works for you Commented Feb 15, 2014 at 20:05

3 Answers 3

7

You need to run gnome-open, kde-open, or exo-open, depending on which desktop you are using.

I believe there is a project called xdg-utils that attempts to provide a unified interface to the local desktop.

So, something like:

snprintf(s, sizeof s, "%s %s", "xdg-open", the_file);
system(s);

Beware of code injection. It's safer to bypass scripting layers with user input, so consider something like:

pid = fork();
if (pid == 0) {
  execl("/usr/bin/xdg-open", "xdg-open", the_file, (char *)0);
  exit(1);
}
// parent will usually wait for child here
Sign up to request clarification or add additional context in comments.

5 Comments

xdg-open calls the appropiate one.
@ninjalj, excellent, I was hoping the pkg did exactly that. I think it's what Chrome uses.
though I'd recommend using execv* instead of system.
@Stepan: the filename, which, if you are using system, is a pretty obvious attack vector.
Anyway, execl("xdg-open", "xdg-open", file, (char *) NULL) is much safer. (Of course, after a fork()).
2

Ubuntu 10.10 is based on GNOME. So, it would be good idea to use g_app_info_launch_default_for_uri().

Something like this should work.

#include <stdio.h>
#include <gio/gio.h>

int main(int argc, char *argv[])
{
        gboolean ret;
        GError *error = NULL;

        g_type_init();

        ret = g_app_info_launch_default_for_uri("file:///etc/passwd",
                                                NULL,
                                                &error);
        if (ret)
                g_message("worked");
        else
                g_message("nop: %s", error->message);

        return 0;
}

BTW, xdg-open, a shell script, tries to determin your desktop environment and call a known helper like gvfs-open for GNOME, kde-open for KDE, or something else. gvfs-open ends up calling g_app_info_launch_default_for_uri().

3 Comments

I prefer this method if the application already depends on GNOME.
I recommend you don't use this function, it does not seem to respect your mime settings. On Xfce, I set all browser and browser-related associations to librewolf but it still spawns firefox for urls. I have this issue rarely with other programs too, probably always those that use this function...
@phil294, that's your mis-configuration. Try gio mime x-scheme-handler/https to see how your system answers. If it recommends firefox, try running strace gio mime x-scheme-handler/https 2>&1 | grep open | grep -v -e ') = -1' -e O_DIRECTORY, and grep though each files to see which file you have your wrong settings are.
0

A simple solution with less coding:

I've tested this program on my Ubuntu and it is working fine, and if I am not wrong you are looking for something like this


#include <stdio.h>
#include <stdlib.h>

int main()
{
    system("firefox file:///dox/song.mp3");
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.