afasias:
this compiles but I can't test it now. I'm still not sure about the "oldSREG = SREG" trick and the CLI that are used in pinMode()
cli() disables interrupts, and SREG = oldSREG restores them to the state they were at when oldSREG was saved.
The general rule is that you'll want to save the status register (SREG) somewhere in your function (doesn't matter where) before cli(). You'll want to call cli() right before you do any manipulation of ports (eg reg and out). Then right after you finish manipulating the ports you want to restore the status register.
However, I think reading them without disabling interrupts is okay because they're 1 byte and therefore atomic. Get a second opinion, though.