Phlip wrote:
Frank Liebelt wrote:
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.
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