C call equivalent of 'which'? - Linux

This is a discussion on C call equivalent of 'which'? - Linux ; Is there a C call that does what 'which' does? i.e. return the full path to a program....

+ Reply to Thread
Results 1 to 6 of 6

Thread: C call equivalent of 'which'?

  1. C call equivalent of 'which'?

    Is there a C call that does what 'which' does? i.e. return the full path
    to a program.

  2. Re: C call equivalent of 'which'?

    On 2007-07-06, General Schvantzkoph wrote:

    > Is there a C call that does what 'which' does? i.e. return the full path
    > to a program.


    /* untested */

    FILE *fp;
    char buf[1024];
    size_t n;
    int status;

    fp = popen("which foobar");
    if (fp)
    {
    n = fread(buf, sizeof buf, 1, fp);
    status = pclose(fp);
    }

    --
    Grant Edwards grante Yow! Life is a POPULARITY
    at CONTEST! I'm REFRESHINGLY
    visi.com CANDID!!

  3. Re: C call equivalent of 'which'?

    On 2007-07-06, Grant Edwards wrote:
    > On 2007-07-06, General Schvantzkoph wrote:
    >
    >> Is there a C call that does what 'which' does? i.e. return the full path
    >> to a program.

    >
    > /* untested */
    >
    > FILE *fp;
    > char buf[1024];
    > size_t n;
    > int status;
    >
    > fp = popen("which foobar");


    Oops, that should be

    fp = popen("which foobar","r");

    > if (fp)
    > {
    > n = fread(buf, sizeof buf, 1, fp);
    > status = pclose(fp);
    > }
    >



    --
    Grant Edwards grante Yow! Kids, don't gross me
    at off ... "Adventures with
    visi.com MENTAL HYGIENE" can be
    carried too FAR!

  4. Re: C call equivalent of 'which'?

    On Fri, 06 Jul 2007 21:26:11 +0000, Grant Edwards wrote:

    > On 2007-07-06, Grant Edwards wrote:
    >> On 2007-07-06, General Schvantzkoph wrote:
    >>
    >>> Is there a C call that does what 'which' does? i.e. return the full
    >>> path to a program.

    >>
    >> /* untested */
    >>
    >> FILE *fp;
    >> char buf[1024];
    >> size_t n;
    >> int status;
    >>
    >> fp = popen("which foobar");

    >
    > Oops, that should be
    >
    > fp = popen("which foobar","r");
    >
    >> if (fp)
    >> {
    >> n = fread(buf, sizeof buf, 1, fp);
    >> status = pclose(fp);
    >> }
    >>


    Thanks

  5. Re: C call equivalent of 'which'?

    > Is there a C call that does what 'which' does? i.e. return the full path
    > to a program.


    Remember that the answer can vary, instant by instant, unless your
    $PATH contains only directories that are particulary well-controlled.
    Even then, temporary resource exhaustion may produce EAGAIN, EMFILE, etc.
    Also, the Linux kernel provides no way to answer the question, "Does
    this execve() get to the point-of-no-return?" without actually
    performing the call. Therefore, the answer given by 'which' may well
    be incorrect. Do not use in situations involving life support,
    safety-critical applications, etc.

    --

  6. Re: C call equivalent of 'which'?

    On Jul 6, 2:15 pm, General Schvantzkoph
    wrote:
    > Is there a C call that does what 'which' does? i.e. return the full path
    > to a program.


    What about:

    http://developer.gnome.org/doc/API/2...rogram-in-path

    gchar * g_find_program_in_path(const gchar * program);

    Stéphane Charette


+ Reply to Thread