spawn.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <config.h>
  2. #include <glib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #ifdef HAVE_UNISTD_H
  6. #include <unistd.h>
  7. #endif
  8. #include "test.h"
  9. #ifdef G_OS_WIN32
  10. #include <io.h>
  11. #define read _read
  12. #define close _close
  13. #endif
  14. RESULT
  15. test_spawn_sync ()
  16. {
  17. gchar *out;
  18. gchar *err;
  19. gint status = -1;
  20. GError *error = NULL;
  21. if (!g_spawn_command_line_sync ("ls", &out, &err, &status, &error))
  22. return FAILED ("Error executing 'ls'");
  23. if (status != 0)
  24. return FAILED ("Status is %d", status);
  25. if (out == NULL || strlen (out) == 0)
  26. return FAILED ("Didn't get any output from ls!?");
  27. g_free (out);
  28. g_free (err);
  29. return OK;
  30. }
  31. RESULT
  32. test_spawn_async ()
  33. {
  34. /*
  35. gboolean
  36. g_spawn_async_with_pipes (const gchar *working_directory,
  37. gchar **argv,
  38. gchar **envp,
  39. GSpawnFlags flags,
  40. GSpawnChildSetupFunc child_setup,
  41. gpointer user_data,
  42. GPid *child_pid,
  43. gint *standard_input,
  44. gint *standard_output,
  45. gint *standard_error,
  46. GError **error) */
  47. char *argv [15];
  48. int stdout_fd = -1;
  49. char buffer [512];
  50. pid_t child_pid = 0;
  51. memset (argv, 0, 15 * sizeof (char *));
  52. argv [0] = "ls";
  53. if (!g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &child_pid, NULL, &stdout_fd, NULL, NULL))
  54. return FAILED ("1 Failed to run ls");
  55. if (child_pid == 0)
  56. return FAILED ("2 child pid not returned");
  57. if (stdout_fd == -1)
  58. return FAILED ("3 out fd is -1");
  59. while (read (stdout_fd, buffer, 512) > 0);
  60. close (stdout_fd);
  61. return OK;
  62. }
  63. static Test spawn_tests [] = {
  64. {"g_shell_spawn_sync", test_spawn_sync},
  65. {"g_shell_spawn_async_with_pipes", test_spawn_async},
  66. {NULL, NULL}
  67. };
  68. DEFINE_TEST_GROUP_INIT(spawn_tests_init, spawn_tests)