159 lines
4.2 KiB
C
159 lines
4.2 KiB
C
#include <arpa/inet.h>
|
|
#include <ifaddrs.h>
|
|
#include <netinet/in.h>
|
|
#include <pwd.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/statvfs.h>
|
|
#include <sys/types.h>
|
|
#include <sys/utsname.h>
|
|
#include <unistd.h>
|
|
|
|
#include "moonbit.h"
|
|
|
|
static moonbit_bytes_t cstr_to_moonbit_bytes(const char *s) {
|
|
if (!s) {
|
|
return moonbit_make_bytes(0, 0);
|
|
}
|
|
size_t len = strlen(s);
|
|
moonbit_bytes_t b = moonbit_make_bytes((int32_t)len, 0);
|
|
if (len > 0) {
|
|
memcpy(b, s, len);
|
|
}
|
|
return b;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT moonbit_bytes_t moonfetch_uname_info(void) {
|
|
struct utsname u;
|
|
if (uname(&u) != 0) {
|
|
return cstr_to_moonbit_bytes("");
|
|
}
|
|
char buf[1024];
|
|
snprintf(buf, sizeof(buf), "%s\n%s\n%s\n%s", u.sysname, u.nodename, u.release,
|
|
u.machine);
|
|
return cstr_to_moonbit_bytes(buf);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT moonbit_bytes_t moonfetch_get_username(void) {
|
|
const char *env_user = getenv("USER");
|
|
if (env_user && env_user[0] != '\0') {
|
|
return cstr_to_moonbit_bytes(env_user);
|
|
}
|
|
struct passwd *pw = getpwuid(geteuid());
|
|
if (pw && pw->pw_name) {
|
|
return cstr_to_moonbit_bytes(pw->pw_name);
|
|
}
|
|
return cstr_to_moonbit_bytes("user");
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT moonbit_bytes_t moonfetch_get_disk_usage(moonbit_bytes_t path) {
|
|
struct statvfs st;
|
|
if (statvfs((const char *)path, &st) != 0) {
|
|
return cstr_to_moonbit_bytes("");
|
|
}
|
|
unsigned long long total =
|
|
(unsigned long long)st.f_blocks * (unsigned long long)st.f_frsize;
|
|
unsigned long long avail =
|
|
(unsigned long long)st.f_bavail * (unsigned long long)st.f_frsize;
|
|
unsigned long long free_all =
|
|
(unsigned long long)st.f_bfree * (unsigned long long)st.f_frsize;
|
|
unsigned long long used = total >= free_all ? (total - free_all) : 0;
|
|
(void)avail;
|
|
char buf[128];
|
|
snprintf(buf, sizeof(buf), "%llu,%llu", used, total);
|
|
return cstr_to_moonbit_bytes(buf);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT moonbit_bytes_t moonfetch_get_term_size(void) {
|
|
struct winsize ws;
|
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0 &&
|
|
ws.ws_row > 0) {
|
|
char buf[64];
|
|
snprintf(buf, sizeof(buf), "%u,%u", (unsigned)ws.ws_col,
|
|
(unsigned)ws.ws_row);
|
|
return cstr_to_moonbit_bytes(buf);
|
|
}
|
|
return cstr_to_moonbit_bytes("");
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT moonbit_bytes_t moonfetch_get_local_ip(void) {
|
|
struct ifaddrs *ifaddr = NULL;
|
|
if (getifaddrs(&ifaddr) != 0) {
|
|
return cstr_to_moonbit_bytes("");
|
|
}
|
|
char result[INET_ADDRSTRLEN] = "";
|
|
for (struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
|
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) {
|
|
continue;
|
|
}
|
|
if (ifa->ifa_name && strcmp(ifa->ifa_name, "lo") == 0) {
|
|
continue;
|
|
}
|
|
struct sockaddr_in *sa = (struct sockaddr_in *)ifa->ifa_addr;
|
|
if (inet_ntop(AF_INET, &sa->sin_addr, result, sizeof(result)) != NULL) {
|
|
if (strncmp(result, "127.", 4) != 0) {
|
|
break;
|
|
}
|
|
result[0] = '\0';
|
|
}
|
|
}
|
|
freeifaddrs(ifaddr);
|
|
return cstr_to_moonbit_bytes(result);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT moonbit_bytes_t moonfetch_popen_first_line(moonbit_bytes_t cmd) {
|
|
FILE *fp = popen((const char *)cmd, "r");
|
|
if (!fp) {
|
|
return cstr_to_moonbit_bytes("");
|
|
}
|
|
char buf[512];
|
|
buf[0] = '\0';
|
|
if (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
size_t len = strlen(buf);
|
|
while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) {
|
|
buf[--len] = '\0';
|
|
}
|
|
}
|
|
pclose(fp);
|
|
return cstr_to_moonbit_bytes(buf);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT moonbit_bytes_t moonfetch_read_file_stream(moonbit_bytes_t path) {
|
|
FILE *fp = fopen((const char *)path, "rb");
|
|
if (!fp) {
|
|
return moonbit_make_bytes(0, 0);
|
|
}
|
|
size_t cap = 4096;
|
|
size_t len = 0;
|
|
char *buf = (char *)malloc(cap);
|
|
if (!buf) {
|
|
fclose(fp);
|
|
return moonbit_make_bytes(0, 0);
|
|
}
|
|
while (1) {
|
|
if (len + 4096 > cap) {
|
|
size_t new_cap = cap * 2;
|
|
char *new_buf = (char *)realloc(buf, new_cap);
|
|
if (!new_buf) {
|
|
break;
|
|
}
|
|
buf = new_buf;
|
|
cap = new_cap;
|
|
}
|
|
size_t n = fread(buf + len, 1, 4096, fp);
|
|
len += n;
|
|
if (n < 4096) {
|
|
break;
|
|
}
|
|
}
|
|
fclose(fp);
|
|
moonbit_bytes_t res = moonbit_make_bytes((int32_t)len, 0);
|
|
if (len > 0) {
|
|
memcpy(res, buf, len);
|
|
}
|
|
free(buf);
|
|
return res;
|
|
}
|