1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 17:57:17 +00:00

Add os/readlink.

This commit is contained in:
Leah Neukirchen 2020-03-09 17:34:30 +01:00
parent 381dd1ce98
commit 6e67899401

View File

@ -32,6 +32,7 @@
#include <time.h> #include <time.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -737,6 +738,23 @@ static Janet os_remove(int32_t argc, Janet *argv) {
return janet_wrap_nil(); return janet_wrap_nil();
} }
static Janet os_readlink(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
#ifdef JANET_WINDOWS
(void) argc;
(void) argv;
janet_panic("os/readlink not supported on Windows");
return janet_wrap_nil();
#else
static char buffer[PATH_MAX];
const char *path = janet_getcstring(argv, 0);
ssize_t len = readlink(path, buffer, sizeof buffer);
if (len < 0 || (size_t)len >= sizeof buffer)
janet_panicf("%s: %s", strerror(errno), path);
return janet_stringv((const uint8_t *)buffer, len);
#endif
}
#ifdef JANET_WINDOWS #ifdef JANET_WINDOWS
static const uint8_t *janet_decode_permissions(unsigned short m) { static const uint8_t *janet_decode_permissions(unsigned short m) {
uint8_t flags[9] = {0}; uint8_t flags[9] = {0};
@ -1145,6 +1163,11 @@ static const JanetReg os_cfuns[] = {
"Create a symlink from oldpath to newpath. The 3 optional paramater " "Create a symlink from oldpath to newpath. The 3 optional paramater "
"enables a hard link over a soft link. Does not work on Windows.") "enables a hard link over a soft link. Does not work on Windows.")
}, },
{
"os/readlink", os_readlink,
JDOC("(os/readlink path)\n\n"
"Read the contents of a symbolic link. Does not work on Windows.\n")
},
{ {
"os/execute", os_execute, "os/execute", os_execute,
JDOC("(os/execute args &opts flags env)\n\n" JDOC("(os/execute args &opts flags env)\n\n"