Hacker News new | ask | show | jobs
by phoyd 996 days ago
Under UNIX, the lowest available file descriptor for the process is returned from open/create (also in Posix). So if you close(0) and then immediately open() something, you will (probably) get 0 as the new file descriptor.

This is the way redirection was done in early UNIX versions, at least I think until System 7, where dup2() was introduced to address the race condition lurking here.