diff options
author | 2020-02-17 00:04:01 +0200 | |
---|---|---|
committer | 2020-02-17 00:04:01 +0200 | |
commit | 3fa68c95644b9a69c0e519987577fb1bb57bf1dc (patch) | |
tree | 2bd6db828ff45deb7a5772ba9837964e082ff0e1 /src/io.c | |
parent | 7980ded002b380a9b3c8e8656c3272ee9923e8c9 (diff) | |
download | algos-ld1-3fa68c95644b9a69c0e519987577fb1bb57bf1dc.tar.gz algos-ld1-3fa68c95644b9a69c0e519987577fb1bb57bf1dc.tar.bz2 algos-ld1-3fa68c95644b9a69c0e519987577fb1bb57bf1dc.zip |
fix saving the output file.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/io.c')
-rw-r--r-- | src/io.c | 22 |
1 files changed, 20 insertions, 2 deletions
@@ -91,7 +91,6 @@ err: int stream_close(struct stream *in) { - char path[PATH_MAX]; int ret = 0; if (!in || in->fd < 0) { @@ -99,8 +98,26 @@ int stream_close(struct stream *in) goto early_err; } - if (in->out && in->name) { + if (!in->out) { + goto out; + } + + if (in->name) { + char path[PATH_MAX]; + struct stat st; + snprintf(path, PATH_MAX, "/proc/self/fd/%i", in->fd); + + if (!stat(in->name, &st)) { + if (st.st_mode & S_IFREG) { + unlink(in->name); + } else { + ret = EINVAL; + /* TODO: error message */ + goto err; + } + } + if (linkat(AT_FDCWD, path, AT_FDCWD, in->name, AT_SYMLINK_FOLLOW)) { ret = errno; /* TODO: error message */ @@ -111,6 +128,7 @@ int stream_close(struct stream *in) goto err; } +out: err: close(in->fd); in->fd = -1; |