summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/io.c36
-rw-r--r--src/io.h3
-rw-r--r--src/main.c9
3 files changed, 38 insertions, 10 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;
+}
diff --git a/src/io.h b/src/io.h
index efc3f94..79216f2 100644
--- a/src/io.h
+++ b/src/io.h
@@ -11,6 +11,7 @@ struct stream {
char *name;
};
-int openstream(struct stream *in);
+int stream_open(struct stream *in);
+int stream_close(struct stream *in);
#endif /* ALGOS_IO_H_INCLUDED */
diff --git a/src/main.c b/src/main.c
index 7cc0c5e..2e1ffe1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,3 @@
-#include <unistd.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
@@ -31,7 +30,7 @@ int main(int argc, char **argv)
file_in.name = settings.filein;
file_in.stride = 0; /* TODO */
- if ((ret = openstream(&file_in))) {
+ if ((ret = stream_open(&file_in))) {
goto out;
}
@@ -40,13 +39,13 @@ int main(int argc, char **argv)
file_out.n = file_in.n;
file_out.stride = 0; /* TODO */
- if ((ret = openstream(&file_out))) {
+ if ((ret = stream_open(&file_out))) {
goto out;
}
out:
- close(file_in.fd);
- close(file_out.fd);
+ stream_close(&file_in);
+ stream_close(&file_out);
return ret;
}