#include "../SDL_sysurl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
int
SDL_SYS_OpenURL(const char *url)
{
const pid_t pid1 = fork();
if (pid1 == 0) {
pid_t pid2;
unsetenv("LD_PRELOAD");
pid2 = vfork();
if (pid2 == 0) {
execlp("xdg-open", "xdg-open", url, NULL);
_exit(EXIT_FAILURE);
} else if (pid2 < 0) {
_exit(EXIT_FAILURE);
} else {
_exit(EXIT_SUCCESS);
}
} else if (pid1 < 0) {
return SDL_SetError("fork() failed: %s", strerror(errno));
} else {
int status;
if (waitpid(pid1, &status, 0) == pid1) {
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) == 0) {
return 0;
} else {
return SDL_SetError("xdg-open reported error or failed to launch: %d", WEXITSTATUS(status));
}
} else {
return SDL_SetError("xdg-open failed for some reason");
}
} else {
return SDL_SetError("Waiting on xdg-open failed: %s", strerror(errno));
}
}
}