Search code examples
cwindowssubprocessglib

Checking the exit status of subprocesses on Windows


In gEDA, we have a helper program that needs to create a subprocess and check its exit status to ensure that it completed successfully. On Linux, we use something similar to:

#include <glib.h>
#include <sys/wait.h>

static gboolean
build_and_run_command (const gchar *format, ...)
{
  int result, status;
  gchar *args, *standard_error;
  GError *error = NULL;

  /* Set up argument variables */

  if (g_spawn_sync (".",
                    args,
                    NULL,
                    G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL,
                    NULL,
                    NULL,
                    NULL,
                    &standard_error,
                    &status,
                    &error)) {
    result = (WIFEXITED (status) && WEXITSTATUS(status) == 0);
  }

  /* Clean up */

  return result;
}

The full source code for the program can be found in our git repository.

Unfortunately, when compiling for Windows using MinGW, we have discovered that sys/wait.h doesn't exist, and neither do the WIFEXITED or WEXITSTATUS macros. What is the "right way" to check for a normal exit and to retrieve the exit status on Windows using g_spawn_sync? Google has been surprisingly unhelpful!


Solution

  • The documentation for g_spawn_async_with_pipes() explains how to do it on Windows.