Ejemplo n.º 1
0
void
system_sub (const char *fcmd, GFC_INTEGER_4 *status, gfc_charlen_type cmd_len)
{
  char cmd[cmd_len + 1];
  int stat;

  /* Flush all I/O units before executing the command.  */
  flush_all_units();

  memcpy (cmd, fcmd, cmd_len);
  cmd[cmd_len] = '\0';

  stat = system (cmd);
  if (status)
    *status = stat;
}
Ejemplo n.º 2
0
void
flush_i8 (GFC_INTEGER_8 *unit)
{
  gfc_unit *us;

  /* flush all streams */
  if (unit == NULL)
    flush_all_units ();
  else
    {
      us = find_unit (*unit);
      if (us != NULL)
	{
	  sflush (us->s);
	  unlock_unit (us);
	}
    }
}
Ejemplo n.º 3
0
static void
execute_command_line (const char *command, bool wait, int *exitstat,
		      int *cmdstat, char *cmdmsg,
		      gfc_charlen_type command_len,
		      gfc_charlen_type cmdmsg_len)
{
  /* Transform the Fortran string to a C string.  */
  char cmd[command_len + 1];
  memcpy (cmd, command, command_len);
  cmd[command_len] = '\0';

  /* Flush all I/O units before executing the command.  */
  flush_all_units();

#if defined(HAVE_FORK)
  if (!wait)
    {
      /* Asynchronous execution.  */
      pid_t pid;

      set_cmdstat (cmdstat, EXEC_NOERROR);

      if ((pid = fork()) < 0)
	set_cmdstat (cmdstat, EXEC_CHILDFAILED);
      else if (pid == 0)
	{
	  /* Child process.  */
	  int res = system (cmd);
	  _exit (WIFEXITED(res) ? WEXITSTATUS(res) : res);
	}
    }
  else
#endif
    {
      /* Synchronous execution.  */
      int res = system (cmd);

      if (res == -1)
	set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
#ifndef HAVE_FORK
      else if (!wait)
	set_cmdstat (cmdstat, EXEC_SYNCHRONOUS);
#endif
      else
	set_cmdstat (cmdstat, EXEC_NOERROR);

      if (res != -1)
	{
#if defined(WEXITSTATUS) && defined(WIFEXITED)
	  *exitstat = WIFEXITED(res) ? WEXITSTATUS(res) : res;
#else
	  *exitstat = res;
#endif
	}
    }

  /* Now copy back to the Fortran string if needed.  */
  if (cmdstat && *cmdstat > EXEC_NOERROR)
    {
      if (cmdmsg)
	fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat],
		strlen (cmdmsg_values[*cmdstat]));
      else
	runtime_error ("Failure in EXECUTE_COMMAND_LINE: %s",
		       cmdmsg_values[*cmdstat]);
    }
}
Ejemplo n.º 4
0
static void
execute_command_line (const char *command, bool wait, int *exitstat,
                      int *cmdstat, char *cmdmsg,
                      gfc_charlen_type command_len,
                      gfc_charlen_type cmdmsg_len)
{
    /* Transform the Fortran string to a C string.  */
    char *cmd = fc_strdup (command, command_len);

    /* Flush all I/O units before executing the command.  */
    flush_all_units();

#if defined(HAVE_FORK)
    if (!wait)
    {
        /* Asynchronous execution.  */
        pid_t pid;

        set_cmdstat (cmdstat, EXEC_NOERROR);

        if ((pid = fork()) < 0)
            set_cmdstat (cmdstat, EXEC_CHILDFAILED);
        else if (pid == 0)
        {
            /* Child process.  */
            int res = system (cmd);
            _exit (WIFEXITED(res) ? WEXITSTATUS(res) : res);
        }
    }
    else
#endif
    {
        /* Synchronous execution.  */
        int res = system (cmd);

        if (res == -1)
            set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
#ifndef HAVE_FORK
        else if (!wait)
            set_cmdstat (cmdstat, EXEC_SYNCHRONOUS);
#endif
        else if (res == 127 || res == 126
#if defined(WEXITSTATUS) && defined(WIFEXITED)
                 || (WIFEXITED(res) && WEXITSTATUS(res) == 127)
                 || (WIFEXITED(res) && WEXITSTATUS(res) == 126)
#endif
                )
            /* Shell return codes 126 and 127 mean that the command line could
               not be executed for various reasons.  */
            set_cmdstat (cmdstat, EXEC_INVALIDCOMMAND);
        else
            set_cmdstat (cmdstat, EXEC_NOERROR);

        if (res != -1)
        {
#if defined(WEXITSTATUS) && defined(WIFEXITED)
            *exitstat = WIFEXITED(res) ? WEXITSTATUS(res) : res;
#else
            *exitstat = res;
#endif
        }
    }

    free (cmd);

    /* Now copy back to the Fortran string if needed.  */
    if (cmdstat && *cmdstat > EXEC_NOERROR)
    {
        if (cmdmsg)
            fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat],
                     strlen (cmdmsg_values[*cmdstat]));
        else
            runtime_error ("Failure in EXECUTE_COMMAND_LINE: %s",
                           cmdmsg_values[*cmdstat]);
    }
}