Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!cica!ctrsol!ginosko!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.sources.d Subject: Re: Perl on Microport 286: can't find library routines Message-ID: <2432@auspex.auspex.com> Date: 9 Sep 89 20:13:49 GMT References: <2869@splut.conmicro.com> <9257@attctc.Dallas.TX.US> <1989Sep7.222720.3381@NCoast.ORG> Reply-To: guy@auspex.auspex.com (Guy Harris) Organization: Auspex Systems, Santa Clara Lines: 58 >dup2() under System V unixes: (System V prior to R3, anyway; "dup2()" under S5R3 and later is just "dup2()", it's already in "libc".) Actually, to be strictly correct, you should do a bit more (1003.1 says that "dup2" shall be equivalent to the sequence you give, except for some extra checks it lists, which are done here): #include #include #include int dup2(fildes, fildes2) int fildes, fildes2; { /* * Check whether "fildes" is a valid file descriptor: * "If 'fildes' is not a valid file descriptor, 'dup2()' * shall fail and not close 'fildes2'." */ if (fcntl(fildes, F_GETFL, 0) == -1) return -1; /* "fcntl" set "errno" correctly */ /* * Check whether "fildes2" is within the correct range: * "If 'fildes2' is less than zero or greater than * {OPEN_MAX}, the 'dup2()' function shall return -1 and * 'errno' shall be set to [EBADF]." * No, don't use "ulimit" to get the maximum valid file * descriptor; that first appeared in S5R3, but S5R3 * already *has* a "dup2()". */ if (fildes2 < 0 || fildes2 >= NOFILE) { errno = EBADF; return -1; } /* * Check whether "fildes" is equal to "fildes2": * "If 'fildes' is a valid file descriptor and is equal * to 'fildes2', the 'dup2()' function shall return * 'fildes2' without closing it." */ if (fildes == fildes2) return 0; /* * Now do the real work: * "The call 'fid = dup2 (fildes, fildes2);' shall be equivalent * to 'close (fildes2); fid = fcntl (fildes, F_DUPFD, fildes2);' * except for the following:" where "the following" are the * cases listed above. */ (void) close(fildes2); return fcntl(fildes, F_DUPFD, fildes2); }