stdio: handle file position correctly at program exit