Re: Can't compile this code:
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: Thu, 25 Nov 2004 20:07:25 +1100
"Alwyn" <firstname.lastname@example.org> wrote in message
| In article <30j3caF316oodU1@uni-berlin.de>, Val \
| <email@example.com> wrote:
| > I don't know if it is just my implementation that is broken,
| > but I have tried a few times to use this function in a safe
| > manner, but it has been a very difficult one for me to trap
| > errors for(specifically for malformed command strings).
| I don't know about Windows, but on a POSIX-compliant system, the
| following applies:
| If string is a NULL pointer, system() will return non-zero if the
| command interpreter sh(1) is available, and zero if it is not.
| The system() function returns the exit status of the shell as returned
| waitpid(2), or -1 if an error occurred when invoking fork(2) or
| waitpid(2). A return value of 127 means the execution of the shell
| I believe the shell will return 0 even though it may have failed to
| execute the command passed to it.
Hmn, very interesting.
| > From my understanding (apart from the return value of system),
| > 'errno' should also be set to one of the following as well,
| > should an error occur:
| > ENOENT Path or file function not found
| > ENOEXEC Exec format error
| > ENOMEM Not enough memory
| > ...but, errno never yields a result other than zero.
| I'm not aware that this is required by any standard.
My bad, you are correct. The standard does not specify
that it must set 'errno', but my compiler documentation
does. FWIW, here is what it says:
If command is a NULL pointer, system returns
nonzero if a command processor is available.
If command is not a NULL pointer, system
returns 0 if the command processor was successfully started.
If an error occurred, a -1 is returned and errno is set to
one of the following:
ENOENT Path or file function not found
ENOEXEC Exec format error
ENOMEM Not enough memory
My main frustration was, that even though it is documented
as such, I haven't been able to trap any of these errors
via 'errno', as it is always zero.
| Generally speaking, if the command given to 'system' fails to execute,
| there is no way for your program to know about it. This has led some
| people to advise against the use of this library function.
That is all fair and well I suppose, but it's a mystery
to me why such seemingly simple things, should be such a
This function is ultimately therefore just as dangerous
as using 'gets()' :-).