From b20afc22f707b0227693444f0098bd6c106e69a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Mon, 16 Jul 2007 17:28:03 +0000 Subject: [PATCH] Implement swrap_dup too. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@21592 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/roken/socket_wrapper.c | 78 ++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/lib/roken/socket_wrapper.c b/lib/roken/socket_wrapper.c index c0554800e..9e6bfdd09 100644 --- a/lib/roken/socket_wrapper.c +++ b/lib/roken/socket_wrapper.c @@ -1837,40 +1837,11 @@ _PUBLIC_ int swrap_close(int fd) return ret; } -_PUBLIC_ int swrap_dup(int oldd) +static int +dup_internal(const struct socket_info *si_oldd, int fd) { - struct socket_info *si; + struct socket_info *si_newd; - si = find_socket_info(oldd); - if (si == NULL) - return real_dup(oldd); - - abort(); /* write code here */ -} - - -_PUBLIC_ int swrap_dup2(int oldd, int newd) -{ - struct socket_info *si_newd, *si_oldd; - int fd; - - if (newd == oldd) - return newd; - - si_oldd = find_socket_info(oldd); - si_newd = find_socket_info(newd); - - if (si_oldd == NULL && si_newd == NULL) - return real_dup2(oldd, newd); - - fd = real_dup2(si_oldd->fd, newd); - if (fd < 0) - return fd; - - /* close new socket first */ - if (si_newd) - swrap_close(newd); - si_newd = (struct socket_info *)calloc(1, sizeof(struct socket_info)); si_newd->fd = fd; @@ -1897,3 +1868,46 @@ _PUBLIC_ int swrap_dup2(int oldd, int newd) return fd; } + + +_PUBLIC_ int swrap_dup(int oldd) +{ + struct socket_info *si; + int fd; + + si = find_socket_info(oldd); + if (si == NULL) + return real_dup(oldd); + + fd = real_dup(si->fd); + if (fd < 0) + return fd; + + return dup_internal(si, fd); +} + + +_PUBLIC_ int swrap_dup2(int oldd, int newd) +{ + struct socket_info *si_newd, *si_oldd; + int fd; + + if (newd == oldd) + return newd; + + si_oldd = find_socket_info(oldd); + si_newd = find_socket_info(newd); + + if (si_oldd == NULL && si_newd == NULL) + return real_dup2(oldd, newd); + + fd = real_dup2(si_oldd->fd, newd); + if (fd < 0) + return fd; + + /* close new socket first */ + if (si_newd) + swrap_close(newd); + + return dup_internal(si_oldd, fd); +}