Xref: utzoo comp.unix.wizards:24044 comp.lang.perl:3946 gnu.emacs.bug:3023 Path: utzoo!utgpu!watserv1!watmath!uunet!zaphod.mps.ohio-state.edu!usc!snorkelwacker.mit.edu!ai-lab!ai.mit.edu!gnulists From: rbj@uunet.uu.net (Root Boy Jim) Newsgroups: comp.unix.wizards,comp.lang.perl,gnu.emacs.bug Subject: Re: dup2 (emacs implementation broken) Message-ID: Date: 8 Feb 91 07:43:45 GMT References: <27B1CA1C.22559@ics.uci.edu> <1991Feb8.002436.21328@usenet.ins.cwru.edu> Followup-To: comp.unix.wizards Organization: UUNET Communications Services, Falls Church, VA Lines: 49 Approved: info-gnu@prep.ai.mit.edu To: gnu-emacs-bug@uunet.uu.net --text follows this line-- In article <1991Feb8.002436.21328@usenet.ins.cwru.edu> chet@po.CWRU.Edu writes: >Doug Schmidt writes: > >> I'm curious, is it possible to implement the dup2() system >>call using only routines available in the standard C library and other >>existing system calls? > >When you really get down to it. the kernel is going to have to do the >nitty-gritty duplication for you, otherwise it gets tricky. If you have >an fcntl(..., F_DUPFD, ...), it's straightforward. > >Here's how we do it in bash: Hmmmm. The two left feet don't know what the right hand is doing. Go check your emacs sources, src/sysdep.c. It's trivial once you realize the recursive solution. First, the one from emacs: OOPS! It doesn't work! And I feel really stupid cuz I sent it to Larry Wall. Oh well, I just tested this: dup2(old,new) { register int fd, ret; close(new); fd = dup(old); if (fd == -1) return(-1); if (fd == new) return(new); ret = dup2(old,new); close(fd); return(ret); } If you want to try it out, here's a driver program: main(c,v) char **v; { int a,b; (c > 2) || (printf("usage: %s srcfd dstfd\n",v[0]), exit(1)); a = atoi(v[1]); b = atoi(v[2]); printf("dup2(%d,%d) = %d\n",a,b,dup2(a,b)), exit(0); } -- Root Boy Jim Cottrell I got a head full of ideas They're driving me insane