Idris2/support/c/idris_support.c

92 lines
1.7 KiB
C
Raw Normal View History

2020-05-18 16:51:10 +03:00
#include "idris_support.h"
#include "idris_file.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
2020-06-16 13:53:36 +03:00
#include <stdlib.h>
2020-05-18 16:51:10 +03:00
2020-05-19 14:11:07 +03:00
#ifdef _WIN32
extern char **_environ;
#include "windows/win_utils.h"
2020-05-19 14:11:07 +03:00
#define environ _environ
#else
2020-05-18 16:51:10 +03:00
extern char** environ;
2020-05-19 14:11:07 +03:00
#endif
2020-05-18 16:51:10 +03:00
int idris2_isNull(void* ptr) {
return (ptr == NULL);
}
char* idris2_getString(void *p) {
return (char*)p;
}
int idris2_getErrno() {
return errno;
}
char* idris2_getStr() {
char *inp = idris2_readLine(stdin);
// Remove trailing newline; easier to do this than in PrimIO which
// doesn't have the relevant functions available yet
for(char *c = inp; *c != '\0'; ++c) {
if (*c == '\n' || *c == '\r') {
*c = '\0';
}
}
return inp;
}
void idris2_putStr(char* f) {
idris2_writeLine(stdout, f);
}
void idris2_sleep(int sec) {
#ifdef _WIN32
win32_sleep(sec*1000);
#else
2020-05-18 16:51:10 +03:00
struct timespec t;
t.tv_sec = sec;
t.tv_nsec = 0;
nanosleep(&t, NULL);
#endif
2020-05-18 16:51:10 +03:00
}
void idris2_usleep(int usec) {
#ifdef _WIN32
win32_sleep(usec/1000);
#else
2020-05-18 16:51:10 +03:00
struct timespec t;
t.tv_sec = usec / 1000000;
t.tv_nsec = (usec % 1000000) * 1000;
nanosleep(&t, NULL);
#endif
2020-05-18 16:51:10 +03:00
}
int idris2_time() {
return time(NULL); // RTS needs to have 32 bit integers at least
}
char* idris2_getEnvPair(int i) {
return *(environ + i);
}
2020-06-16 13:09:22 +03:00
int idris2_setenv(const char *name, const char *value, int overwrite) {
#ifdef _WIN32
return win32_modenv(name, value, overwrite);
2020-06-16 13:09:22 +03:00
#else
2020-06-16 13:22:21 +03:00
return setenv(name, value, overwrite);
2020-06-16 13:09:22 +03:00
#endif
}
int idris2_unsetenv(const char *name) {
#ifdef _WIN32
return win32_modenv(name, "", 1);
2020-06-16 13:09:22 +03:00
#else
return unsetenv(name);
#endif
}