system() Statuscode

F

Frank Liebelt

Hi

I am a beginner in c++.

In my little program i would unpack a tar file with a system call.
system("tar -xfv unpack.tar");

Is it possible to check that the tar command was successfull or not?
I know system() gives -1 on error, 0 on OK but how to check tar was
successfull?

I thought about if (access(... but i hope there is a better way to do this.

Frank
 
P

Phlip

Frank said:
In my little program i would unpack a tar file with a system call.
system("tar -xfv unpack.tar");

Is it possible to check that the tar command was successfull or not?
I know system() gives -1 on error, 0 on OK but how to check tar was
successfull?

Use popen (or _popen), and read each line returned from the call. Parse the
line to find success or failure indicators. Then use pclose to get the final
return value.
 
F

Frank Liebelt

Phlip said:
Use popen (or _popen), and read each line returned from the call. Parse the
line to find success or failure indicators. Then use pclose to get the final
return value.


I tried this but it wont work:

bool unpack(int value)
{
FILE *tar;
char buf[128];

switch (value)
{
case 1:
if ((tar = popen("tar -xf /tmp/mytar.tar -C /home/user1", "rt")) == NULL)
return false

if (pclose(tar) == 0)
return true;

return false;
break;
}
case 2:
if ((tar = popen("tar -xf /tmp/mytar.tar -C /home/user2", "rt")) == NULL)
return false

if (pclose(tar) == 0)
return true;

return false;
break;
}

}

It doesnt work cause this gives allways false
if ((tar = popen("tar -xf /tmp/mytar.tar -C /home/user", "rt")) == NULL)

tar is installed an this command works in the console with no probs.

Frank
 
A

Aaron Isotton

Frank said:
It doesnt work cause this gives allways false
if ((tar = popen("tar -xf /tmp/mytar.tar -C /home/user", "rt")) == NULL)

tar is installed an this command works in the console with no probs.

Maybe you need to give the full path of tar. Anyway, that's rather a
question for comp.unix.programmer.

Greetings,
Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top