Implementation of dup2

Hi all,I'm reading <Advanced programming in the UNIX environment>,that book asked the reader to implement a function which has same functions with dup2 without calling fcntl.Could anyone give me a tip?Any help will be appreciated.:slight_smile:

I don't think there's a straightfoward, platform-independent way to do that, which is probably why it exists... I can think of lots of half-hacks that'd work in very specific circumstances but wouldn't be able to copy other things -- i.e. no sockets, no pipes, etc.

Another hackish solution would be to close the original and use dup() until you get the FD you want. For really low FD's you might be able to depend on getting the one you want, if there aren't any closed FD's lower than it.

On linux I'd cheat and use dup3.

Remember that the reader is expected to use only those concepts introduced so far in the book. So there is no reason to think about exotic solutions. This is why I think that looping on dup() is the answer Rich was looking for. Remember to clean up after the dup loop. I don't see the dup loop as a real terrible problem... it just seems that way because we are mentally comparing it to the real dup2.

It's off the subject, but here is a worse example of looping... around 1990 I was sysadmin of a Vax running Ultrix. We had some game (rogue maybe?) where you could save your status and then do a restore if you "died", but only during the current run. The game stored the pid in the save file and the restore would fail if the current pid was different. So one of our programmers wrote an "allocate_pid" function. :eek: Guess how it worked. And this was before today's fast fork. :wall:

Thanks for your reply,the only question is how could I know if the file is opened? i.e. I have 0,1,2 already opened,and i wanna dup 1 on 7,i must first open 0~6 and close 7 then call dup,is there a easy way to know the stat of the file such as opened or closed?I see the manpage of fcntl and seems there's no information about that.

The same way you find out if a window is open using your fist. :wink:

Just close it, if close succeeds it was open, if it wasn't, it was already closed...

1 Like

I would just close 7 and then dup until I finally get 7. Then close 4 through 6 or whatever. But if 5 was already open, the dup loop will go from 4 straight to 6 and in this case you should not close 5. You need to store in an array that fd's returned by dup.

Btw, fstat can tell you by succeeding or failing if fd n is open, but I see no reason for that here.

1 Like

yes,that's a good idea except i have to drop some status of the fd such as current position.Btw,thanks.

pls tell me some detail about that.I did't figure out some method to use "st_mode" to tell whether the fd is opened.Thanks a lot.

Just look at the return code. All system calls return -1 if they fail and they set errno. If you fstat an fd that is not open the fstat will fail. It will return -1 and set errno to EBADF. If it failed it will not touch the stat structure.

My man page says:

     The fstat() function will fail if:

     EBADF The fildes argument is not a valid open file  descrip-
           tor.
1 Like

Thanks so much Perderabo.