# execlp to sort

**URL:** <https://community.unix.com/t/execlp-to-sort/178240>\
**Category:** Programming\
**Created:** [November 28, 2007, 4:32pm UTC](https://community.unix.com/t/execlp-to-sort/178240 "2007-11-28T16:32:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![the\_learner](https://community.unix.com/letter_avatar/the_learner/32/5_5575768a8748004e209b776fc1b2916d.png) [@the\_learner](https://community.unix.com/u/the_learner)\
**Post date:** [November 28, 2007, 4:32pm UTC](https://community.unix.com/t/execlp-to-sort/178240/1 "2007-11-28T16:32:48Z")

</div>

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.

```plaintext
#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.

> [@](#):
>
> sort: cannot create temporary file: /u/mmr/somedir/sortPbxXPn: No such file or directory

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

> [@](#):
>
> %\> sort hugefile.txt -T /u/mmr/somedir

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

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [November 28, 2007, 4:49pm UTC](https://community.unix.com/t/execlp-to-sort/178240/2 "2007-11-28T16:49:42Z")

</div>

This works:

```nohighlight
#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.

---

<div class="post-metadata">

**Author:** ![the\_learner](https://community.unix.com/letter_avatar/the_learner/32/5_5575768a8748004e209b776fc1b2916d.png) [@the\_learner](https://community.unix.com/u/the_learner)\
**Post date:** [November 28, 2007, 5:03pm UTC](https://community.unix.com/t/execlp-to-sort/178240/3 "2007-11-28T17:03:00Z")

</div>

hey thanks a lot jim, that works !!

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [November 29, 2007, 12:05am UTC](https://community.unix.com/t/execlp-to-sort/178240/4 "2007-11-29T00:05:49Z")

</div>

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

```

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

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/5_5575768a8748004e209b776fc1b2916d.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [November 29, 2007, 12:50am UTC](https://community.unix.com/t/execlp-to-sort/178240/5 "2007-11-29T00:50:02Z")

</div>

> [@matrixmadhan](#):
>
> I feel using absolute paths here would be much more better.

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