Thread: equivalent windows FindExecutable for linux

  1. #1
    Registered User
    Join Date
    Jun 2013

    equivalent windows FindExecutable for linux


    i need an equivalent function in c linux for windows c function "FindExecutable"

    Thanks !

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    La-la land
    Linux is not Windows, it does not behave in the same way. What does that function do, anyway? I'm guessing it finds the preferred application to execute some file?

    If you want your program to open some file in a graphical environment, just execute xdg-open path-to-file . It should work in all graphical environments in Linux. If that does not exist, you have an older system. Then, you need to use the desktop environment opener instead: gnome-open path-to-file or kde-open path-to-file or exo-open path-to-file , for Gnome, KDE, or XFCE, respectively.

    For example:
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <errno.h>
    /* Launch preferred application (in parallel) to open the specified file.
     * The function returns errno for (apparent) success,
     * and nonzero error code otherwise.
     * Note that error cases are visually reported by xdg-open to the user,
     * so that there is no need to provide error messages to user.
    int open_preferred(const char *const filename)
        const char *const args[3] = { "xdg-open", filename, NULL };
        pid_t             child, p;
        int               status;
        if (!filename || !*filename)
            return errno = EINVAL; /* Invalid file name */
        /* Fork a child process. */
        child = fork();
        if (child == (pid_t)-1)
            return errno = EAGAIN; /* Out of resources, or similar */
        if (!child) {
            /* Child process. Execute. */
            execvp(args[0], (char **)args);
            /* Failed. Return 3, "a required too could not be found". */
        /* Parent process. Wait for child to exit. */
        do {
            p = waitpid(child, &status, 0);
        } while (p == (pid_t)-1 && errno == EINTR);
        if (p != child)
            return errno = ECHILD; /* Failed; child process vanished */
        /* Did the child process exit normally? */
        if (!WIFEXITED(status))
            return errno = ECHILD; /* Child process was aborted */
        switch (WEXITSTATUS(status)) {
        case 0:  return errno = 0;       /* Success */
        case 1:  return errno = EINVAL;  /* Error in command line syntax */
        case 2:  return errno = ENOENT;  /* File not found */
        case 3:  return errno = ENODEV;  /* Application not found */
        default: return errno = EAGAIN;  /* Failed for other reasons. */
    The above open_preferred(filename) executes the opener in parallel.

    If it returns a nonzero value, the value is an errno number describing the cause of the failure. You should inform the user if that happens.

    Because the opener only launches the preferred application, the function does not check whether the application managed to open the file successfully or not.

    The function will return 0 for normal errors like nonexistent files and files not allowed to be opened by the current user, but the opener will pop up a small error dialog box to inform the user about the problem.

    In other words, the function may return 0 even if there was a problem opening the file. But, you only need to inform the user if the open_preferred(filename) function returns nonzero; otherwise the user already knows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. graphics.h equivalent in Linux??
    By linuxlover in forum C Programming
    Replies: 1
    Last Post: 11-28-2010, 02:47 PM
  2. Windows equivalent to linux curl command?
    By elmutt in forum Windows Programming
    Replies: 5
    Last Post: 02-29-2008, 06:16 AM
  3. Linux equivalent of con
    By dwks in forum Linux Programming
    Replies: 3
    Last Post: 11-12-2005, 11:58 AM
  4. Linux equivalent to CreateDirectory
    By Rak'kar in forum Linux Programming
    Replies: 2
    Last Post: 07-10-2004, 12:04 PM
  5. .bat equivalent for linux
    By ichijoji in forum Linux Programming
    Replies: 8
    Last Post: 03-07-2004, 08:06 AM