Initial escape library

Not working yet
This commit is contained in:
shrkey
2016-10-10 22:42:09 +01:00
parent d3429c677a
commit 9f4dffe704
24 changed files with 2872 additions and 0 deletions

70
darkwater/Util.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include <cstdio>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "Util.h"
#define SCRIPT_PATH "../../../check_apm.sh"
int write_file(const char *path, const char *fmt, ...)
{
errno = 0;
int fd = ::open(path, O_WRONLY | O_CLOEXEC);
if (fd == -1) {
return -errno;
}
va_list args;
va_start(args, fmt);
int ret = ::vdprintf(fd, fmt, args);
int errno_bkp = errno;
::close(fd);
va_end(args);
if (ret < 1) {
return -errno_bkp;
}
return ret;
}
int read_file(const char *path, const char *fmt, ...)
{
errno = 0;
FILE *file = ::fopen(path, "re");
if (!file)
return -errno;
va_list args;
va_start(args, fmt);
int ret = ::vfscanf(file, fmt, args);
int errno_bkp = errno;
::fclose(file);
va_end(args);
if (ret < 1)
return -errno_bkp;
return ret;
}
bool check_apm()
{
int ret = system("ps -AT | grep -c sched-timer > /dev/null");
if (WEXITSTATUS(ret) <= 0) {
fprintf(stderr, "APM is running. Can't launch the example\n");
return true;
}
return false;
}