host.c raw
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdint.h>
4 #include <unistd.h>
5 #include <sys/syscall.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8
9 int32_t mxc_readfile(const char* path, int32_t pathLen, char* buf, int32_t bufCap) {
10 char pbuf[4096];
11 if (pathLen > 4095) pathLen = 4095;
12 memcpy(pbuf, path, pathLen);
13 pbuf[pathLen] = 0;
14 int fd = open(pbuf, O_RDONLY);
15 if (fd < 0) return -1;
16 int32_t total = 0;
17 while (total < bufCap) {
18 long n = read(fd, buf + total, bufCap - total);
19 if (n <= 0) break;
20 total += (int32_t)n;
21 }
22 close(fd);
23 return total;
24 }
25
26 int32_t mxc_filesize(const char* path, int32_t pathLen) {
27 char pbuf[4096];
28 if (pathLen > 4095) return -1;
29 memcpy(pbuf, path, pathLen);
30 pbuf[pathLen] = 0;
31 struct stat st;
32 if (syscall(SYS_newfstatat, -100, pbuf, &st, 0) != 0) return -1;
33 return (int32_t)st.st_size;
34 }
35