fixed bug as ttyname can return NULL