2020-05-18 16:51:10 +03:00
|
|
|
#include "idris_file.h"
|
2022-09-21 13:13:15 +03:00
|
|
|
#include "getline.h"
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2021-07-04 10:53:53 +03:00
|
|
|
#include <dirent.h>
|
2020-05-18 16:51:10 +03:00
|
|
|
#include <errno.h>
|
2021-07-04 10:53:53 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2020-05-27 14:52:16 +03:00
|
|
|
#include <sys/time.h>
|
2021-07-04 10:53:53 +03:00
|
|
|
#include <time.h>
|
2020-05-18 16:51:10 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "windows/win_utils.h"
|
|
|
|
#else
|
|
|
|
#include <sys/select.h>
|
2021-12-08 14:57:20 +03:00
|
|
|
#include <sys/wait.h>
|
2020-05-18 16:51:10 +03:00
|
|
|
#endif
|
|
|
|
|
2021-07-04 10:53:53 +03:00
|
|
|
#include "idris_util.h"
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
FILE *idris2_openFile(char *name, char *mode) {
|
2020-05-18 16:51:10 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
FILE *f = win32_u8fopen(name, mode);
|
2020-05-18 16:51:10 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
FILE *f = fopen(name, mode);
|
2020-05-18 16:51:10 +03:00
|
|
|
#endif
|
2022-09-21 13:13:15 +03:00
|
|
|
return (void *)f;
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
void idris2_closeFile(FILE *f) {
|
|
|
|
IDRIS2_VERIFY(fclose(f) == 0, "fclose failed: %s", strerror(errno));
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_getFileNo(FILE *f) {
|
2021-09-10 10:05:21 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
return win32_getFileNo(f);
|
2021-09-10 10:05:21 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
return fileno(f);
|
2021-09-10 10:05:21 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_fileError(FILE *f) { return ferror(f); }
|
2020-05-18 16:51:10 +03:00
|
|
|
|
|
|
|
int idris2_fileErrno() {
|
2022-09-21 13:13:15 +03:00
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
return 2;
|
|
|
|
case EACCES:
|
|
|
|
return 3;
|
|
|
|
case EEXIST:
|
|
|
|
return 4;
|
|
|
|
default:
|
|
|
|
return (errno + 5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int idris2_chmod(const char *path, mode_t mode) {
|
2021-07-21 16:35:21 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
// return _chmod(path, mode);
|
|
|
|
return 0; /* ??? (from win_hack.c) */
|
2021-07-21 16:35:21 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
return chmod(path, mode);
|
2021-07-21 16:35:21 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_removeFile(const char *filename) { return remove(filename); }
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_fileSize(FILE *f) {
|
|
|
|
int fd = idris2_getFileNo(f);
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
struct stat buf;
|
|
|
|
if (fstat(fd, &buf) == 0) {
|
|
|
|
return (int)(buf.st_size);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_fpoll(FILE *f) {
|
2020-05-18 16:51:10 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
return win_fpoll(f);
|
2020-05-18 16:51:10 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
fd_set x;
|
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
int fd = idris2_getFileNo(f);
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
FD_ZERO(&x);
|
|
|
|
FD_SET(fd, &x);
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int r = select(fd + 1, &x, 0, 0, &timeout);
|
|
|
|
return r;
|
2020-05-18 16:51:10 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-23 19:42:05 +03:00
|
|
|
void *idris2_popen(const char *cmd, const char *mode) {
|
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
FILE *f = win32_u8popen(cmd, mode);
|
2020-05-23 19:42:05 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
FILE *f = popen(cmd, mode);
|
2020-05-23 19:42:05 +03:00
|
|
|
#endif
|
2022-09-21 13:13:15 +03:00
|
|
|
return f;
|
2020-05-23 19:42:05 +03:00
|
|
|
}
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2021-11-03 18:10:43 +03:00
|
|
|
int idris2_pclose(void *stream) {
|
2020-05-28 17:12:16 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
int r = _pclose(stream);
|
|
|
|
IDRIS2_VERIFY(r != -1, "pclose failed");
|
|
|
|
return r;
|
2020-05-28 17:12:16 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
int r = pclose(stream);
|
|
|
|
IDRIS2_VERIFY(WIFEXITED(r), "pclose failed");
|
|
|
|
return WEXITSTATUS(r);
|
2020-05-28 17:12:16 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:13:25 +03:00
|
|
|
// seek through the next newline, consuming and
|
|
|
|
// throwing away anything until then.
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_seekLine(FILE *f) {
|
|
|
|
while (1) {
|
|
|
|
int c = fgetc(f);
|
|
|
|
if (c == -1) {
|
|
|
|
if (feof(f)) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '\n') {
|
|
|
|
return 0;
|
2021-02-18 14:13:25 +03:00
|
|
|
}
|
2022-09-21 13:13:15 +03:00
|
|
|
}
|
2021-02-18 14:13:25 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
char *idris2_readLine(FILE *f) {
|
|
|
|
char *buffer = NULL;
|
|
|
|
size_t n = 0;
|
|
|
|
ssize_t len;
|
|
|
|
len = getline(&buffer, &n, f);
|
|
|
|
if (len < 0 && buffer != NULL) {
|
|
|
|
buffer[0] = '\0'; // Copy Idris 1 behaviour - empty string if nothing read
|
|
|
|
}
|
|
|
|
return buffer; // freed by RTS if not NULL
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
char *idris2_readChars(int num, FILE *f) {
|
|
|
|
char *buffer = malloc((num + 1) * sizeof(char));
|
|
|
|
size_t len;
|
|
|
|
len = fread(buffer, sizeof(char), (size_t)num, f);
|
|
|
|
buffer[len] = '\0';
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
if (len <= 0) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return buffer; // freed by RTS
|
|
|
|
}
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
size_t idris2_readBufferData(FILE *h, char *buffer, size_t loc, size_t max) {
|
|
|
|
return fread(buffer + loc, sizeof(uint8_t), max, h);
|
2021-06-10 13:19:09 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_writeLine(FILE *f, char *str) {
|
|
|
|
if (fputs(str, f) == EOF) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
size_t idris2_writeBufferData(FILE *h, const char *buffer, size_t loc,
|
|
|
|
size_t len) {
|
|
|
|
return fwrite(buffer + loc, sizeof(uint8_t), len, h);
|
2021-06-10 13:19:09 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_eof(FILE *f) { return feof(f); }
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_fileAccessTime(FILE *f) {
|
|
|
|
int fd = idris2_getFileNo(f);
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
struct stat buf;
|
|
|
|
if (fstat(fd, &buf) == 0) {
|
|
|
|
return buf.st_atime;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_fileModifiedTime(FILE *f) {
|
|
|
|
int fd = idris2_getFileNo(f);
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
struct stat buf;
|
|
|
|
if (fstat(fd, &buf) == 0) {
|
|
|
|
return buf.st_mtime;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_fileStatusTime(FILE *f) {
|
|
|
|
int fd = idris2_getFileNo(f);
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
struct stat buf;
|
|
|
|
if (fstat(fd, &buf) == 0) {
|
|
|
|
return buf.st_ctime;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_fileIsTTY(FILE *f) {
|
|
|
|
int fd = idris2_getFileNo(f);
|
2021-09-10 10:05:21 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
return win32_isTTY(fd);
|
2021-09-10 10:05:21 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
return isatty(fd);
|
2021-09-10 10:05:21 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
FILE *idris2_stdin() { return stdin; }
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
FILE *idris2_stdout() { return stdout; }
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
FILE *idris2_stderr() { return stderr; }
|