/* system.c (emx+gcc) */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>

int main (int argc, char *argv[])
{
  int i, len;
  char *p, buf[512];

  if (argc == 2 && strcmp (argv[1], "-i") == 0)
    {
      printf ("system: ");
      fflush (stdout);
      if (fgets (buf, sizeof (buf), stdin) == NULL)
        return 1;
      p = strchr (buf, '\n');
      if (p != NULL) *p = 0;
      p = buf;
    }
  else
    {
      len = 1;
      for (i = 1; i < argc; ++i)
        len += strlen (argv[i]) + 1;
      p = malloc (len);
      if (p == NULL)
        {
          perror ("malloc");
          return 1;
        }
      *p = 0;
      for (i = 1; i < argc; ++i)
        {
          if (i > 1)
            strcat (p, " ");
          strcat (p, argv[i]);
        }
    }
  i = system (p);
  if (i < 0)
    {
      perror ("system");
      return 1;
    }
  else
    printf ("Return code = %d\n", i);
  return 0;
}
