Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:18:02+00:00 2026-06-06T19:18:02+00:00

Is there a POSIX function that works like the which command? That is, I

  • 0

Is there a POSIX function that works like the which command? That is, I pass it a command name, and it looks in $PATH for executables with that name, and returns the absolute path to the command, if any.

A longer explanation: My POSIX-C application wants to launch a subprocess whose process might be called foo or bar. The first idea I had was something like (ignoring that I need the child’s stdin/stdout/stderr):

 system("which foo && foo || which bar && bar");

I don’t like this general approach because this shoves all errors concerning process invocation into the exit code of the child process and the stdout/stderr (which I need as binary streams in my application!).

So it looks like I need to replicate the behavior of which in my application code, to locate the foo or bar executable. Is there a suitable POSIX function, or do you even have a code snippet?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:18:04+00:00Added an answer on June 6, 2026 at 7:18 pm

    You can use the standard fork / exec pattern, but simply run exec twice. If exec fails, you try with the other process.

    pid_t pid;
    pid = fork();
    if (pid < 0)
        abort();
    if (pid == 0) {
        // child process
        execlp("foo", "foo", "--flag");
        if (errno != ENOENT)
            abort();
        execlp("bar", "bar", "--bar-flags");
        abort();
    }
    // parent process
    

    This is probably the easiest way to do it, since by capturing the error code from exec, you can find out exactly why it failed (because foo does not exist).

    Alternative: You could also implement which yourself in C, which may or may not be less annoying than calling which directly.

    function which(file)
        path = getenv("PATH")
        for prefix in path
            if access(prefix "/" file, R_OK | X_OK)
                return prefix "/" file
            end if
        end for
        return NULL
    end function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a POSIX function that searches PATH for an executable according to the
Is there a POSIX function that will give me the size of a directory
Is there any POSIX signals that I could utilize in my Perl program to
I know that on MacOSX / PosiX systems, there is atomic-compare-and-swap for C/C++ code
Is there any way to check the granularity of gettimeofday() function provided by POSIX?
I have written a function which, when provided a range of dates, the name
There are many functions (especially in the POSIX library) that return pointers to almost-necessarily
There is the POSIX library in c++ support wchar_t? if not, then how is
There is a moment in my app, that I need to force to show
There is a column that exists in 2 tables. In table 1, this column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.