Move platform specific to sys/

Move unix/ and win/ to sys/, so that we can use android/ for android
headers without confusion.
This commit is contained in:
Romain Vimont
2017-12-14 17:22:43 +01:00
parent a919944372
commit 95591d2938
3 changed files with 4 additions and 4 deletions

View File

@@ -0,0 +1,39 @@
#include "../../command.h"
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
pid_t cmd_execute(const char *path, const char *const argv[]) {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
return -1;
}
if (pid == 0) {
execvp(path, (char *const *)argv);
perror("exec");
_exit(1);
}
return pid;
}
SDL_bool cmd_terminate(pid_t pid) {
return kill(pid, SIGTERM) != -1;
}
SDL_bool cmd_simple_wait(pid_t pid, int *exit_code) {
int status;
int code;
if (waitpid(pid, &status, 0) == -1 || !WIFEXITED(status)) {
// cannot wait, or exited unexpectedly, probably by a signal
code = -1;
} else {
code = WEXITSTATUS(status);
}
if (exit_code) {
*exit_code = code;
}
return !code;
}

44
app/src/sys/win/command.c Normal file
View File

@@ -0,0 +1,44 @@
#include "../../command.h"
#include <stdio.h>
#include "../strutil.h"
HANDLE cmd_execute(const char *path, const char *const argv[]) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
// Windows command-line parsing is WTF:
// <http://daviddeley.com/autohotkey/parameters/parameters.htm#WINPASS>
// only make it work for this very specific program
// (don't handle escaping nor quotes)
char cmd[256];
size_t ret = xstrjoin(cmd, argv, ' ', sizeof(cmd));
if (ret >= sizeof(cmd)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Command too long (%" PRIsizet " chars)", sizeof(cmd) - 1);
return NULL;
}
if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
return NULL;
}
return pi.hProcess;
}
SDL_bool cmd_terminate(HANDLE handle) {
return CloseHandle(handle);
}
SDL_bool cmd_simple_wait(HANDLE handle, DWORD *exit_code) {
DWORD code;
if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0 || !GetExitCodeProcess(handle, &code)) {
// cannot wait or retrieve the exit code
code = -1; // max value, it's unsigned
}
if (exit_code) {
*exit_code = code;
}
return !code;
}