fd_move2.c (803B)
1 /* ISC license. */ 2 3 #include <unistd.h> 4 #include <errno.h> 5 #include <skalibs/djbunix.h> 6 7 int fd_move2 (int to1, int from1, int to2, int from2) 8 { 9 int tmp = from2 ; 10 if (to1 == from1) return fd_move(to2, from2) ; 11 if (to2 == from2) return fd_move(to1, from1) ; 12 if (from1 == from2) return (to1 == to2) ? fd_move(to1, from1) : (errno = EINVAL, -1) ; 13 if (to1 == to2) return (errno = EINVAL, -1) ; 14 if (from2 == to1) 15 { 16 tmp = dup(from2) ; 17 if (tmp == -1) return -1 ; 18 } 19 if (fd_copy(to1, from1) == -1) 20 { 21 if (from2 != tmp) fd_close(tmp) ; 22 return -1 ; 23 } 24 if (fd_copy(to2, tmp) == -1) 25 { 26 int e = errno ; 27 fd_close(to1) ; 28 if (from2 != tmp) fd_move(from2, tmp) ; 29 errno = e ; 30 return -1 ; 31 } 32 if (from1 != to2) fd_close(from1) ; 33 fd_close(tmp) ; 34 return 0 ; 35 }