int read(int fd, void* buffer, unsigned int count) { _FD_STRUCT* fds; DWORD numRead; fds = fds_from_index(fd); if (fds == NULL) { errno = EBADF; return -1; } initStdHandlesInline(); if (fds->pipe != NULL) { numRead = pipeRead(fds->pipe, (unsigned char*)buffer, count); } else if (fds->hFile != INVALID_HANDLE_VALUE) { if (!ReadFile(fds->hFile, buffer, count, &numRead, NULL)) { if (GetLastError() == ERROR_HANDLE_EOF) fds->eof = TRUE; // else // fds->error = TRUE; return 0; } } else return 0; return (int)numRead; }
// returns non-zero if data is available on stdin int _kbhit(void) { if (!valid_fd(STDIN)) { if (!initStdHandlesInline()) return 0; if (!valid_fd(STDIN)) return 0; } if (fd_stdin->pipe != NULL) { return pipeReadable(fd_stdin->pipe) ? 1 : 0; } else return 0; }
int write(int fd, const void* buffer, unsigned int count) { _FD_STRUCT* fds; DWORD numWritten; fds = fds_from_index(fd); if (fds == NULL) { errno = EBADF; return -1; } initStdHandlesInline(); if (fds->pipe != NULL) { if (fds->pipeChannel != -1) { // write header (for distinguishing stdout from stderr) unsigned long length = count; unsigned char header[5]; header[0] = fds->pipeChannel; memcpy(&header[1], &length, sizeof(length)); /*int x = */ pipeWrite(fds->pipe, header, sizeof(header)); } /*int x =*/ pipeWrite(fds->pipe, (unsigned char*)buffer, count); numWritten = count; } else if (fds->hFile != INVALID_HANDLE_VALUE) { if (!WriteFile(fds->hFile, buffer, count, &numWritten, NULL)) { // fds->error = TRUE; return 0; } } else return 0; return (int)numWritten; }