Emulov's stupid questions: nr 24 
Author Message
 Emulov's stupid questions: nr 24

Does the system() function return a value?
Let's say that I use system("del c:\\somedir\\*.*"); in a dos-environment
for example. Then how would i know if the operation was successful or not?

--
Hey, I speak for myself. No _nagging_ afterwards...
remove NOSPAM when replying
-
http://www.*-*-*.com/ ~emulov/index_english.htm
computer, internet, programming, search engines



Tue, 09 Jan 2001 03:00:00 GMT  
 Emulov's stupid questions: nr 24

Quote:

> Does the system() function return a value?

System returns a value. This _might_ be the command return value, but
this is not guaranteed AFAIK.

You might get a better answer by trying a os-specific NG.
--
Daniel Zinsli
University of Bergen, Norway



Tue, 09 Jan 2001 03:00:00 GMT  
 Emulov's stupid questions: nr 24

Quote:

> Does the system() function return a value?

Yes. If you call 'system(NULL)', it returns non-zero if there's a
command processor, otherwise it returns zero to indicate there's no
command processor.

If you call it with a command string it will return an implementation
defined value, which may or may not be your command's exitcode.

Quote:
> Let's say that I use system("del c:\\somedir\\*.*"); in a dos-environment
> for example. Then how would i know if the operation was successful or not?

I'm afraid you can't be sure unless you rely on some compiler's
documented method of indicating success, which is of course unportable.


Tue, 09 Jan 2001 03:00:00 GMT  
 Emulov's stupid questions: nr 24
I quoted the answer from the C FAQ before.  Here it is again:

19.27:  How can I invoke another program (a standalone executable, or an
        operating system command) from within a C program?

A:      Use the library function system(), which does exactly that.
        Note that system's return value is at best the command's exit
        status (although even that is not guaranteed), and usually has
        nothing to do with the output of the command.  Note also that
        system() accepts a single string representing the command to be
        invoked; if you need to build up a complex command line, you can
        use sprintf().  See also question 19.30.

        References: K&R1 Sec. 7.9 p. 157; K&R2 Sec. 7.8.4 p. 167,
        Sec. B6 p. 253; ANSI Sec. 4.10.4.5; ISO Sec. 7.10.4.5; H&S
        Sec. 19.2 p. 407; PCS Sec. 11 p. 179.

You really, really should download your own copy.  When you have a question,
pop it into your favorite editor and search for some key words in the
expression about which you have a question.  You are probably as smart or
smarter than I am.  So you can find this stuff as fast or faster than I can.
Then you won't have to wait around for a message on usenet.

If you want better results, you will need an OS specific trick.  The popen()
stuff on UNIX is way superior to anything that I know of on PC's.
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com

Quote:

>Does the system() function return a value?
>Let's say that I use system("del c:\\somedir\\*.*"); in a dos-environment
>for example. Then how would i know if the operation was successful or not?

>--
>Hey, I speak for myself. No _nagging_ afterwards...
>remove NOSPAM when replying
>-
>http://www.sin.khk.be/~emulov/index_english.htm
>computer, internet, programming, search engines



Tue, 09 Jan 2001 03:00:00 GMT  
 Emulov's stupid questions: nr 24

Quote:

> Does the system() function return a value?
> Let's say that I use system("del c:\\somedir\\*.*"); in a dos-environment
> for example. Then how would i know if the operation was successful or not?

Check your manual, this changes for each implementation.  The only thing
you can portably do is pass NULL.

--

I believe we can change anything.
I believe in my dream.
    - Joe Satriani



Tue, 09 Jan 2001 03:00:00 GMT  
 Emulov's stupid questions: nr 24

|Does the system() function return a value?
|Let's say that I use system("del c:\\somedir\\*.*"); in a
dos-environment
|for example. Then how would i know if the operation was successful or
not?
========
The return value of system() is implementation-defined except for
the value of system(NULL); which is non-zero only if a command processor
is available (note that it is not "if and only if").
Not very useful in any portable sense.

As two implementation-specific examples:
In the implementation I usually use, which tries to avoid use of the
shell,
    v = system("whatever");
v == 0    when the shell was invoked (rare)
  == -1    when the child could not be run and errno is set
otherwise
v & 0xFF    is the return value of the child
(v >> 8) &0xFF    is 0, SIGINT, or SIGABRT

Borland, on the other hand, returns 0 or -1 as above, since it always
uses the shell (command.com).


/* Newsgroup posts also e-mailed */



Tue, 09 Jan 2001 03:00:00 GMT  
 Emulov's stupid questions: nr 24
: Does the system() function return a value?
: Let's say that I use system("del c:\\somedir\\*.*"); in a dos-environment
: for example. Then how would i know if the operation was successful or not?

This is not an appropriate question for USENet.  Comp.lang.c is not
a reference manual.  You are wasting our time and your own by not
opening your C book or even on-line help.  It will not take you more
than a few minutes, as opposed to hours or days before somebody on
USENet gives you an answer.

I post this for a purpose.  You have apparently chose to become a
regular poster in comp.lang.c, so I urge you to be more selective
with your questions.



Sat, 13 Jan 2001 03:00:00 GMT  
 Emulov's stupid questions: nr 24
OK OK!! DON'T SHOOT ME!!!

--
Hey, I speak for myself. No _nagging_ afterwards...
And I mean no _nagging_ whatsoever!!!!!!
-
http://www.sin.khk.be/~emulov/index_english.htm
computer, internet, programming, search engines



Sat, 13 Jan 2001 03:00:00 GMT  
 Emulov's stupid questions: nr 24

Quote:


> : Does the system() function return a value?
> : Let's say that I use system("del c:\\somedir\\*.*"); in a dos-environment
> : for example. Then how would i know if the operation was successful or not?

> This is not an appropriate question for USENet.  Comp.lang.c is not
> a reference manual.  You are wasting our time and your own by not
> opening your C book or even on-line help.  It will not take you more
> than a few minutes, as opposed to hours or days before somebody on
> USENet gives you an answer.

> I post this for a purpose.  You have apparently chose to become a
> regular poster in comp.lang.c, so I urge you to be more selective
> with your questions.

There is however a bit of wisdom that can be said about "system()":
yes it does return a value and yes, this value does say a bit about
the execution of the command. I do agree that any good C book and
any good compiler should be able to offer this information.

But since I've already started writing this little reply, I might as
well go on to explain "system()" a bit. If the argument to "system()"
is NULL, a return value of 0 indicates that a command interpreter
could not be found. If the argument is NULL and the return value is
nonzero, the command interpreter has been found. If the argument
is not NULL, the processing of the argument and the return value
are both implementation defined.

This means that the way in which an argument string is processed
and the resulting return value may vary from compiler to compiler.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 14 Jan 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Emulov's stupid questions: nr 23

2. Emulov's Stupid Questions: nr 22

3. Emulov's Stupid Questions: nr25 (I think, I lost count)

4. Emulov's 21st question

5. Emulov's question (nr 20?): an illegal while loop?

6. 250 times 'fopen(...,"a")':errno 24

7. Can I use 24-Bit TrueColour in Turbo C++ 3.0 's

8. typedef'ing a 24-bit integer

9. ld.so.1: sh: fatal: libw.so.1: can't open file: errno=24

10. Stupid Beginners' Question

11. Want nr sources (from Dr. Dobb's Journal)

 

 
Powered by phpBB® Forum Software