SDL  2.0
SDL_sysurl.c File Reference
#include "../SDL_sysurl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
+ Include dependency graph for SDL_sysurl.c:

Go to the source code of this file.

Functions

int SDL_SYS_OpenURL (const char *url)
 

Function Documentation

◆ SDL_SYS_OpenURL()

int SDL_SYS_OpenURL ( const char *  url)

Definition at line 32 of file SDL_sysurl.c.

33 {
34  const pid_t pid1 = fork();
35  if (pid1 == 0) { /* child process */
36  /* Notice this is vfork and not fork! */
37  const pid_t pid2 = vfork();
38  if (pid2 == 0) { /* Grandchild process will try to launch the url */
39  execlp("xdg-open", "xdg-open", url, NULL);
40  _exit(EXIT_FAILURE);
41  } else if (pid2 < 0) { /* There was an error forking */
42  _exit(EXIT_FAILURE);
43  } else {
44  /* Child process doesn't wait for possibly-blocking grandchild. */
45  _exit(EXIT_SUCCESS);
46  }
47  } else if (pid1 < 0) {
48  return SDL_SetError("fork() failed: %s", strerror(errno));
49  } else {
50  int status;
51  if (waitpid(pid1, &status, 0) == pid1) {
52  if (WIFEXITED(status)) {
53  if (WEXITSTATUS(status) == 0) {
54  return 0; /* success! */
55  } else {
56  return SDL_SetError("xdg-open reported error or failed to launch: %d", WEXITSTATUS(status));
57  }
58  } else {
59  return SDL_SetError("xdg-open failed for some reason");
60  }
61  } else {
62  return SDL_SetError("Waiting on xdg-open failed: %s", strerror(errno));
63  }
64  }
65 
66  return 0;
67 }
#define SDL_SetError
#define NULL
Definition: begin_code.h:163

References NULL, and SDL_SetError.