This is a test program that demonstrates a Terminal bug.

When Terminal is set to invoke the shell directly, rather than via /usr/bin/login, it leaves a number of Carbon- and Terminal-related files open that the shell inherits, and of course passes onto its own subprocesses.

In this case, when you run ./printfirstfd (or make test), the program will printa number in the vicinity of 14. (It prints 14 for me with this program, but a previous program I was working on printed 11. I attribute this to different Terminal processes.)

The number that it prints is the return value of open(2) - i.e., the first (lowest) available FD. It *should* print 3, the number after 2 (stderr). This is what it prints when Terminal invokes the shell through /usr/bin/login.

If you pass a number to printfirstfd, the program will sleep for that many seconds, affording you the opportunity to run lsof on it and see all the files that Terminal has left open.

---
Peter Hosey
2006-03-24
