From 3534b428629be185e096be99e3bd5fdfe32d5544 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 18 Sep 2014 18:55:44 +0000 Subject: initial commit with rc for skalibs-2.0.0.0 --- src/libstddjb/fd_move2.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/libstddjb/fd_move2.c (limited to 'src/libstddjb/fd_move2.c') diff --git a/src/libstddjb/fd_move2.c b/src/libstddjb/fd_move2.c new file mode 100644 index 0000000..7894e2f --- /dev/null +++ b/src/libstddjb/fd_move2.c @@ -0,0 +1,37 @@ +/* ISC license. */ + +#include +#include +#include + +int fd_move2 (int to1, int from1, int to2, int from2) +{ + register int tmp = from2 ; + if (to1 == from1) return fd_move(to2, from2) ; + if (to2 == from2) return fd_move(to1, from1) ; + if (from1 == from2) return (to1 == to2) ? fd_move(to1, from1) : (errno = EINVAL, -1) ; + if (to1 == to2) return (errno = EINVAL, -1) ; + if (from2 == to1) + { + tmp = dup(from2) ; + if (tmp == -1) return -1 ; + } + if (fd_copy(to1, from1) == -1) + { + register int e = errno ; + if (from2 != tmp) fd_close(tmp) ; + errno = e ; + return -1 ; + } + if (fd_copy(to2, tmp) == -1) + { + register int e = errno ; + fd_close(to1) ; + if (from2 != tmp) fd_move(from2, tmp) ; + errno = e ; + return -1 ; + } + if (from1 != to2) fd_close(from1) ; + fd_close(tmp) ; + return 0 ; +} -- cgit v1.2.3