execlp to sort

Hi, I have a very small program where I call execlp() to execute sort.

Everything works fine. But sort has a -T option which can be used to specify a temporary directory. Now when I use -T with execlp() it does not work.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char *argv[])
{
        int ret;

        ret = execlp("sort","sort","hugefile.txt","-T /u/mmr/somedir",NULL);
        if (ret == -1) {
                fprintf(stderr, "Could not sort file (%s)", strerror(errno));
                exit(1);
        }
        return(1);
}

sort when called using execlp() is unable to find the specified directory "somedir"

This is the error that I get.

However when I run the same command from the shell. Things work fine:

does anybody have a clue on what might be going on? any input will be appreciated!

This works:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char *argv[])
{
        int ret;

        ret = execlp("sort","sort","-T", "/tmp","zma.txt",NULL);
        if (ret == -1) {
                fprintf(stderr, "Could not sort file (%s)", strerror(errno));
                exit(1);
        }
        return(1);
}

I used zma.txt as the file to sort, because that's an all-purpose junk file I have.

hey thanks a lot jim, that works !!

ret = execlp("sort","sort","-T", "/tmp","zma.txt",NULL);

Jim,
I feel using absolute paths here would be much more better.

Isn't that why he's using 'execlp'?