Based on the patch supplied by Takuya Shiozaki <tshiozak@astec.co.jp>. See http://mail-index.netbsd.org/tech-userlevel/2000/02/23/0000.html.