From 371472d9fb6a936149b105a6563a0550d35bdf1a Mon Sep 17 00:00:00 2001 From: Manoj Srivastava Date: Mon, 1 Dec 2003 17:11:15 +0000 Subject: Initial import of upstream branch Initial import of upstream branch git-archimport-id: srivasta@debian.org--2003-primary/dist--upstream--3.70--base-0 --- lib/C/fake/dup2.C | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 lib/C/fake/dup2.C (limited to 'lib/C/fake/dup2.C') diff --git a/lib/C/fake/dup2.C b/lib/C/fake/dup2.C new file mode 100644 index 0000000..27c3c48 --- /dev/null +++ b/lib/C/fake/dup2.C @@ -0,0 +1,78 @@ +/* + * dup2.C -- A dup2 emulation. + */ + +/* + * $Id: dup2.C,v 3.0.1.1 1994/01/24 13:58:37 ram Exp $ + * + * Copyright (c) 1991-1993, Raphael Manfredi + * + * You may redistribute only under the terms of the Artistic Licence, + * as specified in the README file that comes with the distribution. + * You may reuse parts of this distribution only within the terms of + * that same Artistic Licence; a copy of which may be found at the root + * of the source tree for dist 3.0. + * + * Original Author: Larry Wall + * + * $Log: dup2.C,v $ + * Revision 3.0.1.1 1994/01/24 13:58:37 ram + * patch16: created + * + */ + +#include "config.h" + +#ifdef I_FCNTL +#include +#endif + +#include "confmagic.h" /* Remove if not metaconfig -M */ + +#ifndef HAS_DUP2 +/* + * dup2 + * + * This routine duplicates file descriptor 'old' into 'new'. After the + * operation, both 'new' and 'old' refer to the same file 'old' was referring + * to in the first place. + * + * Returns 0 if OK, -1 on failure with errno being set to indicate the error. + * + */ +V_FUNC(int dup2, (old, new), + int old /* Opened file descriptor */ NXT_ARG + int new /* File descriptor we'd like to get */) +{ +#ifdef HAS_FCNTL +#ifdef F_DUPFD +#define USE_FNCTL +#endif +#endif + +#ifdef USE_FCNTL + if (old == new) + return 0; + + close(new); + return fcntl(old, F_DUPFD, new); +#else + int fd_used[256]; /* Fixed stack used to record dup'ed files */ + int fd_top = 0; /* Top in the fixed stack */ + int fd; /* Currently dup'ed file descriptor */ + + if (old == new) + return 0; + + close(new); /* Ensure one free slot */ + while ((fd = dup(old)) != new) /* Until dup'ed file matches */ + fd_used[fd_top++] = fd; /* Remember we have to close it later */ + + while (fd_top > 0) /* Close all useless dup'ed slots */ + close(fd_used[--fd_top]); + + return 0; +#endif +} +#endif + -- cgit v1.2.3