Could you use `ttyname_r()`?