The Haskell Library Report informally defines the function System.system
as follows:

  Computation /system cmd/ returns the exit code produced when the
  operating system processes the command /cmd/.

This is fairly vague.  For the following program:

>|  import System
>|  main =
>|   do
>|   system("exit 0") >>= printExitCode
>|   system("exit 1") >>= printExitCode
>|   system("exit 2") >>= printExitCode
>|
>|  printExitCode r = case r of
>|   ExitSuccess   -> print 0
>|   ExitFailure n -> print n

some people might expect the behaviour

>|  test $ ./a.out
>|  0
>|  1
>|  2

while others might expect the behaviour

>|  test $ ./a.out
>|  0
>|  256
>|  512

which is what you get if you try the same test in C, using the
system(3) call:  the man page says:

>                       system() then returns the exit status returned
>    by wait(2V).  Unless the shell was interrupted by a  signal,  its
>    termination status [i.e. the shell command] is contained in the 8
>    bits higher up from the low-order 8 bits of the value returned by
>    wait()

i.e. the low byte tells you whether your shell command was executed or
not, and the high byte tells you what the result was, if it did in fact
execute.

Currently, nhc98 matches C's behaviour by giving the result code
returned by the operating system (i.e. 256/512/etc).  Currently, Hugs
and ghc give the (more natural) shell result code, not the one returned
by the operating system.

Obviously it would be useful if the expected behaviour were documented
more tightly, and conformed across compilers.  Which result should we
choose to be standard?  (And can the decision please be recorded in the
Haskell 98 errata?)

Regards,
    Malcolm

Reply via email to