Re: Can't compile this code:

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 11/25/04


Date: Thu, 25 Nov 2004 20:07:25 +1100


"Alwyn" <dt015a1979@mac.com.invalid> wrote in message
news:241120041137162728%dt015a1979@mac.com.invalid...
| In article <30j3caF316oodU1@uni-berlin.de>, Val \
| <chrisval@bigpond.com.au> 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:
|
| <quote>
| 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
| by
| 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
| failed.
| </quote>
|
| 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:

Return Value
  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
PITA.

This function is ultimately therefore just as dangerous
as using 'gets()' :-).

Cheers.
Chris Val



Relevant Pages

  • Re: Redirection issue
    ... 1- execute input commands from standard input, ... the phrase "it does not work anymore" carries very little meaning. ... after compilation and execution of the shell with a simple command like ... all the command in the file are executed as well. ...
    (comp.lang.c)
  • Re: Redirection issue
    ... 1- execute input commands from standard input, ... the phrase "it does not work anymore" carries very little meaning. ... after compilation and execution of the shell with a simple command like ... all the command in the file are executed as well. ...
    (comp.lang.c)
  • Re: shell scripting
    ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
    (comp.unix.questions)
  • Re: shell scripting
    ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
    (comp.unix.shell)
  • Re: Sleep freezing
    ... > word of a command, and each distinct command becomes a sub- ... > When it says that the arguments take the form of a standard shell ... When you execute a command from the shell is it not the ... instead of whatever commands remain in the calling script. ...
    (comp.unix.shell)