On Fri, 28 Aug 2009 04:24:00 PDT
madhu <iitmadhu at gmail.com> wrote:

> system() call imeplemented in solaris is such a way that:
> Command not found - return code 1
> Command executed successfully without Output - return code 1

Um, which Solaris? This is the OpenSolaris help list.

And that's not the way it is on OpenSolaris (or Solaris 9 or 10, for
that matter - their man pages describe the same behavior as
OpenSolaris).

> how to distinguish between these two based on return code in a c - file?

You don't. Instead, you file a bug report against wherever you found
that incorrect information, and then read the system(3C) man page for
the appropriate release, which should point you to the waitpid(2) man
page and your shell documentation. Linux and the various BSD's all do
the same thing.

If you're running a Solaris that's so old that system doesn't return
the status of the shell process, you should consider upgrading.

If you've got things set up so that your shell behaves as you
describe, you need to fix your /bin/sh.

Possibly the command you're executing is returning the same status for
successful completion with no output as the shell returns for command
not found. In that case, the command isn't following Unix command
conventions, and should be fixed to do so.

> Can you help on this ?

Here's a trivial test program that handles the behavior of system as
documented on solaris.

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>

int
main(int argc, char **argv) {
  int res ;
  res = system(argv[1]) ;
  printf("system(\"%s\") returned 0x%x, error 0x%x, ", argv[1], res, errno) ;
  if (WIFEXITED(res))
    printf("status 0x%x\n", WEXITSTATUS(res)) ;
  else if (WIFSIGNALED(res))
    printf("terminated by signal 0x%x\n", WTERMSIG(res)) ;
  else if (WIFSTOPPED(res))
    printf("stopped by signal 0x%x\n", WSTOPSIG(res)) ;
  else 
    printf("I'm not sure what happend.\n") ;
}

And here's a few runs:


sensei$ ./a.out true
system("true") returned 0x0, error 0x0, status 0x0
sensei$ ./a.out false
system("false") returned 0x100, error 0x0, status 0x1
sensei$ ./a.out nosuchcommand
sh: line 1: nosuchcommand: not found
system("nosuchcommand") returned 0x7f00, error 0x0, status 0x7f

Note that the case where the command executed successfully with no
output returns 0, whereas the case where the command is not found
returns status 127.

        <mike
-- 
Mike Meyer <mwm at mired.org>           http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

Reply via email to