示例#1
0
文件: omrfile.c 项目: ChengJin01/omr
/**
 * Read bytes from a file descriptor into a user provided buffer.
 *
 * @param[in] portLibrary The port library
 * @param[in] fd The file descriptor.
 * @param[in,out] buf Buffer to read into.
 * @param[in] nbytes Size of buffer.
 *
 * @return The number of bytes read, or -1 on failure.
 */
intptr_t
omrfile_read(struct OMRPortLibrary *portLibrary, intptr_t inFD, void *buf, intptr_t nbytes)
{
	int fd = (int)inFD;
	intptr_t result;

	Trc_PRT_file_read_Entry(fd, buf, nbytes);

	if (nbytes == 0) {
		Trc_PRT_file_read_Exit(0);
		return 0;
	}

#ifdef J9ZOS390
	if (fd == OMRPORT_TTY_IN) {
		result = fread(buf, sizeof(char), nbytes, stdin);
	}  else	if (fd < FD_BIAS) {
		/* Cannot read from STDOUT/ERR, and no other FD's should exist <FD_BIAS */
		Trc_PRT_file_read_Exit(-1);
		return -1;
	} else
#elif (FD_BIAS != 0)
#error FD_BIAS must be 0
#endif
	{
		/* CMVC 178203 - Restart system calls interrupted by EINTR */
		do {
			result = read(fd - FD_BIAS, buf, nbytes);
		} while ((-1 == result) && (EINTR == errno));
	}

	if ((0 == result) || (-1 == result)) {
		if (-1 == result) {
			portLibrary->error_set_last_error(portLibrary, errno, findError(errno));
		}
		result = -1;
	}

	Trc_PRT_file_read_Exit(result);
	return result;

}
示例#2
0
文件: omrfile.c 项目: dinogun/omr
intptr_t
omrfile_read(struct OMRPortLibrary *portLibrary, intptr_t fd, void *buf, intptr_t nbytes)
{
	DWORD bytesRead;
	HANDLE handle;
	int32_t errorCode = 0;

	Trc_PRT_file_read_Entry(fd, buf, nbytes);

	if (OMRPORT_TTY_IN == fd) {
		handle = PPG_tty_consoleInputHd;
	} else {
		handle = (HANDLE)fd;
	}

	if (0 == nbytes) {
		Trc_PRT_file_read_Exit(0);
		return 0;
	}

	/* ReadFile limits the buffer to DWORD size and returns the number of bytes read,
	 * 	so the (DWORD)nbytes cast is functionally correct, though less than ideal.
	 * If the caller wants to read more than 2^32 bytes they will have to make more than one call,
	 * 	but the logic of the calling code should already account for this.
	 */
	if (FALSE == ReadFile(handle, buf, (DWORD)nbytes, &bytesRead, NULL)) {
		int32_t error = GetLastError();
		errorCode = portLibrary->error_set_last_error(portLibrary, error, findError(error));
		Trc_PRT_file_read_Exit(errorCode);
		return -1;
	}
	if (0 == bytesRead) {
		errorCode = portLibrary->error_set_last_error(portLibrary, -1, OMRPORT_ERROR_FILE_READ_NO_BYTES_READ);
		Trc_PRT_file_read_Exit(errorCode);
		return -1;
	}

	Trc_PRT_file_read_Exit(bytesRead);

	return bytesRead;
}