# using execlp function call in linux

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I am trying to call the tar command to extract a file from another app. But when I do I get the error that a file is not found. This is the code I currently have :-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include "archive.h"

void Extract(ArchiveName *name)
{
pid_t pid;
char *buffer = "tar -xvzpf";

pid = fork();

if(pid < 0)
{
perror("fork failed");
exit(1);
}

if(pid == 0)
{
if(execlp(buffer, buffer, name->name, (char *)NULL) == -1)
{
fprintf(stderr, "\nexeclp failed : %s\n", strerror(errno));
}

exit(2);
kill(pid, SIGINT);
}

//system(str);
}


What I am trying to achieve is tar -xvzpf filename . I a not sure where I am going wrong in the execlp command. Thanks.

##### Share on other sites
The first parameter should be just "tar", otherwise the system looks for a file named "tar -xvzpf".
Similarly, the second parameter should be just "-xvzpf", you should not repeat "tar".

So, basically: execlp("tar", "-xvzpf", filename, 0);

##### Share on other sites
I tried that and I get an error :-

Quote:

Does the "/" have any effect here - will it work like an escape sequence or like it should in a shell. I tried using -P no effect either.

##### Share on other sites
arg0 was required [smile]

execlp("tar", "tar", "-xzvpf", filename, 0);

That works on my machine.

##### Share on other sites
Quote:
 execlp("tar", "tar", "-xzvpf", filename, 0);That works on my machine.

But it won't on a system where sizeof(int) isn't the same as sizeof(char*) (or where a null pointer isn't all-bits-zero, but that's rather less common to come across). Pass (char*)0 as the last argument or it'll break on 64 bit computers.