system() Statuscode

Discussion in 'C++' started by Frank Liebelt, Mar 8, 2006.

  1. 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
    Frank Liebelt, Mar 8, 2006
    #1
    1. Advertising

  2. Frank Liebelt

    Phlip Guest

    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.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
    Phlip, Mar 8, 2006
    #2
    1. Advertising

  3. 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
    Frank Liebelt, Mar 8, 2006
    #3
  4. Frank Liebelt wrote:
    > 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
    Aaron Isotton, Mar 8, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesse Napier

    CustomErrors statusCode 500

    Jesse Napier, Aug 7, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,773
    Michal A. Valasek
    Aug 7, 2003
  2. Shane
    Replies:
    1
    Views:
    3,043
    Anthony Williams
    Nov 21, 2003
  3. Shane

    Response.StatusCode

    Shane, Nov 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,847
    Shane
    Nov 20, 2003
  4. Rich
    Replies:
    1
    Views:
    721
    =?Utf-8?B?RXNraW1v?=
    May 26, 2005
  5. Flip Rayner
    Replies:
    1
    Views:
    652
    bruce barker
    Jan 23, 2007
Loading...

Share This Page