summaryrefslogtreecommitdiffstats
path: root/stream.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2014-02-24 20:59:18 -0800
committerKaz Kylheku <kaz@kylheku.com>2014-02-24 20:59:18 -0800
commit15c42fa37fced6cb65b09dce07d59fc729748018 (patch)
tree6c35c40aa32d354a67b0cb44b4d0e4104aa93f3b /stream.c
parent17703842631784a8f623b5478380e90e4e17c8ef (diff)
downloadtxr-15c42fa37fced6cb65b09dce07d59fc729748018.tar.gz
txr-15c42fa37fced6cb65b09dce07d59fc729748018.tar.bz2
txr-15c42fa37fced6cb65b09dce07d59fc729748018.zip
* eval.c (eval_init): Intern symlink_wrap, link_wrap, readlink_wrap.
* stream.c (symlink_wrap, link_wrap, readlink_wrap): New functions. * stream.h (symlink_wrap, link_wrap, readlink_wrap): Declared. * txr.1: Documented.
Diffstat (limited to 'stream.c')
-rw-r--r--stream.c59
1 files changed, 58 insertions, 1 deletions
diff --git a/stream.c b/stream.c
index e436ee34..7fde754a 100644
--- a/stream.c
+++ b/stream.c
@@ -2419,7 +2419,9 @@ val getcwd_wrap(void)
num(errno), string_utf8(strerror(errno)), nao);
}
if (2 * guess > guess)
- guess = 2 * guess;
+ guess *= 2;
+ else
+ uw_throwf(file_error_s, lit("getcwd: weird problem"), nao);
} else {
val out = string_utf8(u8buf);
free(u8buf);
@@ -2457,6 +2459,61 @@ val mknod_wrap(val path, val mode, val dev)
return t;
}
+val symlink_wrap(val target, val to)
+{
+ char *u8target = utf8_dup_to(c_str(target));
+ char *u8to = utf8_dup_to(c_str(to));
+ int err = symlink(u8target, u8to);
+ free(u8target);
+ free(u8to);
+ if (err < 0)
+ uw_throwf(file_error_s, lit("symlink ~a ~a: ~a/~s"),
+ target, to, num(errno), string_utf8(strerror(errno)), nao);
+ return t;
+}
+
+val link_wrap(val target, val to)
+{
+ char *u8target = utf8_dup_to(c_str(target));
+ char *u8to = utf8_dup_to(c_str(to));
+ int err = link(u8target, u8to);
+ free(u8target);
+ free(u8to);
+ if (err < 0)
+ uw_throwf(file_error_s, lit("link ~a ~a: ~a/~s"),
+ target, to, num(errno), string_utf8(strerror(errno)), nao);
+ return t;
+}
+
+val readlink_wrap(val path)
+{
+ char *u8path = utf8_dup_to(c_str(path));
+ ssize_t guess = 256;
+
+ for (;;) {
+ char *u8buf = (char *) chk_malloc(guess);
+ ssize_t bytes = readlink(u8path, u8buf, guess);
+
+ if (bytes >= guess) {
+ free(u8buf);
+ if (2 * guess > guess)
+ guess *= 2;
+ else
+ uw_throwf(file_error_s, lit("readlink: weird problem"), nao);
+ } else if (bytes <= 0) {
+ free(u8buf);
+ uw_throwf(file_error_s, lit("readlink ~a: ~a/~s"),
+ path, num(errno), string_utf8(strerror(errno)), nao);
+ } else {
+ val out;
+ u8buf[bytes] = 0;
+ out = string_utf8(u8buf);
+ free(u8buf);
+ return out;
+ }
+ }
+}
+
#endif
void stream_init(void)