Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
I tried that and I get an error :-

Quote:

/home/username/directory/filename.tar.gz-xvzpf : old option g
requires an argument.

Try -xvzpf or --help for more information.



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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Hi,

It works when I use it in a command line app. But when I call it from a GTK app it seems to give the same error as I was getting on my previous post.

Is exec* not compatible with GTK ? Currently I am resorting to system(..) function.

Share this post


Link to post
Share on other sites
Sign in to follow this