changed O_RDONLY to O_RDWR.