fixed fdopen error by implementing fcntl