Get system() error message

Discussion in 'C Programming' started by jose luis fernandez diaz, May 14, 2004.

  1. Hi,

    In the program below:

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>

    using namespace std;
    int main()
    {
    system("ls asdf");

    printf("%d\n", errno);

    return 0;
    }


    the output is:

    ls: asdf not found
    0


    Two questions:

    1) How can I get the error message(ls: asdf not found) in the program?

    2) Why is errno 0 ?

    Thanks,
    Jose Luis.
    jose luis fernandez diaz, May 14, 2004
    #1
    1. Advertising

  2. jose luis fernandez diaz <> scribbled the following
    on comp.lang.c:
    > Hi,


    > In the program below:


    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <errno.h>


    > using namespace std;
    > int main()
    > {
    > system("ls asdf");


    > printf("%d\n", errno);


    > return 0;
    > }



    > the output is:


    > ls: asdf not found
    > 0



    > Two questions:


    > 1) How can I get the error message(ls: asdf not found) in the program?


    By redirecting it to a file and reading from there. It's the only way
    supported by ISO standard C or C++.

    > 2) Why is errno 0 ?


    Because the program was correctly able to call the command ls. The fact
    that the command ls itself failed is none of C's or C++'s concern.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
    - ALF
    Joona I Palaste, May 14, 2004
    #2
    1. Advertising

  3. On Fri, 14 May 2004, jose luis fernandez diaz wrote:

    > Hi,
    >
    > In the program below:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <errno.h>
    >
    > using namespace std;
    > int main()
    > {
    > system("ls asdf");
    > printf("%d\n", errno);
    > return 0;
    > }
    >
    > the output is:
    >
    > ls: asdf not found
    > 0
    >
    > Two questions:
    >
    > 1) How can I get the error message(ls: asdf not found) in the program?


    Cannot give a definitive answer using just ISO/ANSI C. You need to get
    into system specific code. Something like:

    system("ls asdf > error.txt");

    might work. But not if > only captures stdout and the message is sent to
    stderr. The type of shell that system is using will make a difference. You
    might need to use:

    system("ls asdf 2> error.txt");
    or
    system("ls asdf >& error.txt");

    The possibilities are many. It will depend on so many factors. Namely, how
    is system() implemented? How is the command implemented? how is the system
    configured?

    > 2) Why is errno 0 ?


    Why not? There is nothing in the system() command that indicates it will
    set errno for any reason. I could run a program as a user where system()
    fails and it still won't set errno to anything different.

    You are going to have to restrict your solution to a specific operating
    system. In which case you need to ask a newsgroup that deals with your
    operating system to find out the best solution.

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
    Darrell Grainger, May 14, 2004
    #3
  4. jose luis fernandez diaz

    Dan Pop Guest

    In <> (jose luis fernandez diaz) writes:

    >In the program below:
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >#include <errno.h>
    >
    >using namespace std;


    This is a syntax error in C. If you cross-post, make sure that your code
    is correct in both languages!

    >int main()
    >{
    > system("ls asdf");
    >
    > printf("%d\n", errno);
    >
    > return 0;
    >}
    >
    >the output is:
    >
    >ls: asdf not found
    >0
    >
    >
    >Two questions:
    >
    > 1) How can I get the error message(ls: asdf not found) in the program?


    By not using system() in the first place. Your platform provides a better
    alternative. It's even documented in the c.l.c FAQ, which you didn't
    bother to check *before* posting.

    > 2) Why is errno 0 ?


    Why not? The C standard doesn't require system() to touch errno at all
    and your platform's documentation says:

    ERRORS

    The system() function may set errno values as described by fork().
    In addition, system() may fail if:

    [ECHILD]
    The status of the child process created by system()
    is no longer available.

    The implied fork() call obviously succeeded, so it had no reason to touch
    errno, either.

    Before posting such questions, try to understand how things work and
    check the documentation. Sure, there *was* an errno set somewhere,
    but this somewhere was the process executing the ls command, not *your*
    program. That errno was lost without trace by the time that process
    terminated, before your system() call returned.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, May 14, 2004
    #4
  5. The way I understand it, piping output to a file via system() isn't easily
    do-able portably. You might want to look into popen() or a pipe() - fork() -
    exec()

    --


    ~Kieran Simkin
    Digital Crocus
    http://digital-crocus.com/

    "jose luis fernandez diaz" <> wrote in
    message news:...
    > Hi,
    >
    > In the program below:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <errno.h>
    >
    > using namespace std;
    > int main()
    > {
    > system("ls asdf");
    >
    > printf("%d\n", errno);
    >
    > return 0;
    > }
    >
    >
    > the output is:
    >
    > ls: asdf not found
    > 0
    >
    >
    > Two questions:
    >
    > 1) How can I get the error message(ls: asdf not found) in the program?
    >
    > 2) Why is errno 0 ?
    >
    > Thanks,
    > Jose Luis.
    Kieran Simkin, May 15, 2004
    #5
  6. jose luis fernandez diaz

    Dan Pop Guest

    In <1Odpc.876$> "Kieran Simkin" <> writes:

    >The way I understand it, piping output to a file via system() isn't easily
    >do-able portably. You might want to look into popen() or a pipe() - fork() -
    >exec()


    Redirecting output to a file via system() is a lot more portable than
    popen() or pipe-fork-exec. So, if portability is an overriding issue,
    output redirection is the winning approch.

    OTOH, portability is seldom an issue when system() is used, considering
    that system's argument is highly non-portable (when it's not a null
    pointer).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, May 17, 2004
    #6
    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. Miguel Angel Dinelli
    Replies:
    1
    Views:
    442
    Chris Jackson
    Nov 13, 2003
  2. tshad
    Replies:
    0
    Views:
    784
    tshad
    Jan 27, 2005
  3. rabbits77
    Replies:
    0
    Views:
    857
    rabbits77
    Feb 26, 2004
  4. jose luis fernandez diaz

    Get system() error message

    jose luis fernandez diaz, May 14, 2004, in forum: C++
    Replies:
    5
    Views:
    475
    Dan Pop
    May 17, 2004
  5. Replies:
    6
    Views:
    174
Loading...

Share This Page