Advertisement

using execlp function call in linux

Started by September 11, 2004 03:36 PM
4 comments, last by CRACK123 20 years ago
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.
The more applications I write, more I find out how less I know
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);
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
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.
The more applications I write, more I find out how less I know
arg0 was required [smile]

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

That works on my machine.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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.
The more applications I write, more I find out how less I know

This topic is closed to new replies.

Advertisement