summaryrefslogtreecommitdiffstats
path: root/src/io.c
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2020-02-15 21:10:46 +0200
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2020-02-15 21:10:46 +0200
commit8d72ffe268305a823057053f6a172ac7debee3b0 (patch)
treeb2ceb490c89702527c32e467044f4701b67c6a17 /src/io.c
parent671cef6ab353a6143f88622a86049681ee19e8f4 (diff)
downloadalgos-ld1-8d72ffe268305a823057053f6a172ac7debee3b0.tar.gz
algos-ld1-8d72ffe268305a823057053f6a172ac7debee3b0.tar.bz2
algos-ld1-8d72ffe268305a823057053f6a172ac7debee3b0.zip
add a wrapper for closing files.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c36
1 files changed, 32 insertions, 4 deletions
diff --git a/src/io.c b/src/io.c
index 74ea274..142e230 100644
--- a/src/io.c
+++ b/src/io.c
@@ -1,5 +1,6 @@
#include <sys/types.h>
#include <sys/stat.h>
+#include <linux/limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
@@ -9,16 +10,14 @@
#include <libgen.h>
#include "io.h"
-int openstream(struct stream *in)
+int stream_open(struct stream *in)
{
struct stat st;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP;
char *dname = NULL;
int ret = 0;
- in->fd = -1;
-
- if (!in->name) {
+ if (!in || in->fd > 0 || !in->name) {
ret = EINVAL;
goto err;
}
@@ -89,3 +88,32 @@ err:
free(dname);
return ret;
}
+
+int stream_close(struct stream *in)
+{
+ char path[PATH_MAX];
+ int ret = 0;
+
+ if (!in || in->fd < 0) {
+ ret = EINVAL;
+ goto early_err;
+ }
+
+ if (in->out && in->name) {
+ snprintf(path, PATH_MAX, "/proc/self/fd/%i", in->fd);
+ if (linkat(AT_FDCWD, path, AT_FDCWD, in->name, AT_SYMLINK_FOLLOW)) {
+ ret = errno;
+ /* TODO: error message */
+ goto err;
+ }
+ } else {
+ ret = EINVAL;
+ goto err;
+ }
+
+err:
+ close(in->fd);
+ in->fd = -1;
+early_err:
+ return ret;
+}