#ifndef VILE_PORT_FILE_H #define VILE_PORT_FILE_H #include #include #include #include #include #ifdef _WIN32 #include #include #include #else #include #endif /* Selected file constructors need atomic flag combinations which the V file opener does not expose. This descriptor has no FILE/stdio state. */ static int vile_port_open(const void *path, unsigned int options, int read_write) { int flags; #ifdef _WIN32 flags = (read_write ? _O_RDWR : _O_WRONLY) | _O_BINARY; if (!(options & 1)) flags |= _O_CREAT; if (!(options & 3)) flags |= _O_EXCL; if (!(options & 4)) flags |= _O_TRUNC; int fd; do { fd = _wopen((const wchar_t *)path, flags, _S_IREAD | _S_IWRITE); } while (fd < 0 && errno == EINTR); #else flags = read_write ? O_RDWR : O_WRONLY; if (!(options & 1)) flags |= O_CREAT; if (!(options & 3)) flags |= O_EXCL; if (!(options & 4)) flags |= O_TRUNC; #ifdef O_CLOEXEC flags |= O_CLOEXEC; #endif int fd; do { fd = open((const char *)path, flags, 0666); } while (fd < 0 && errno == EINTR); #endif return fd < 0 ? -(errno ? errno : EIO) : fd; } static int vile_port_close_descriptor(int fd) { /* Do not retry close after EINTR: the descriptor may already be released. */ #ifdef _WIN32 int result = _close(fd); #else int result = close(fd); #endif return result < 0 ? -(errno ? errno : EIO) : 0; } /* Bound transfers for Windows' int-sized CRT API and V's descriptor API. Retry interruptions without allocating or advancing the caller's buffer. */ static int vile_port_flush_standard(int fd) { FILE *stream = fd == 1 ? stdout : stderr; int result; do { result = fflush(stream); if (result && errno == EINTR) clearerr(stream); } while (result && errno == EINTR); return result ? -(errno ? errno : EIO) : 0; } static intptr_t vile_port_read(int fd, void *bytes, size_t length) { size_t count = length > 0x7fffffffU ? 0x7fffffffU : length; intptr_t result; do { #ifdef _WIN32 result = _read(fd, bytes, (unsigned int)count); #else result = read(fd, bytes, count); #endif } while (result < 0 && errno == EINTR); return result < 0 ? -(intptr_t)errno : result; } static intptr_t vile_port_write(int fd, const void *bytes, size_t length) { size_t count = length > 0x7fffffffU ? 0x7fffffffU : length; intptr_t result; do { #ifdef _WIN32 result = _write(fd, bytes, (unsigned int)count); #else result = write(fd, bytes, count); #endif } while (result < 0 && errno == EINTR); return result < 0 ? -(intptr_t)errno : result; } /* Owned ports use descriptor I/O, so their position must use the same layer. Mixing stdio seek/tell with raw reads leaves cached FILE offsets stale. */ static int64_t vile_port_seek(int fd, int64_t offset, int current) { int64_t result; int origin = current ? SEEK_CUR : SEEK_SET; do { #ifdef _WIN32 result = _lseeki64(fd, offset, origin); #else if ((int64_t)(off_t)offset != offset) return -(int64_t)EOVERFLOW; result = (int64_t)lseek(fd, (off_t)offset, origin); #endif } while (result < 0 && errno == EINTR); return result < 0 ? -(int64_t)(errno ? errno : EIO) : result; } /* Nonblocking protocol: -1 means temporarily unavailable, 0 means EOF, and values below -1 encode -(errno+1). Keep C stdio read-ahead coherent for stdin. No V allocation/callback can occur while its lock or flags are held. */ static intptr_t vile_port_read_ready(int fd, void *bytes, size_t length, int use_stdin) { #ifdef _WIN32 (void)fd; (void)bytes; (void)length; (void)use_stdin; return -(intptr_t)(ENOSYS + 1); #else if (use_stdin) flockfile(stdin); int flags = fcntl(fd, F_GETFL); if (flags < 0) { int saved = errno; if (use_stdin) funlockfile(stdin); return -(intptr_t)(saved + 1); } int changed = !(flags & O_NONBLOCK); if (changed && fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { int saved = errno; if (use_stdin) funlockfile(stdin); return -(intptr_t)(saved + 1); } intptr_t result; int saved = 0; if (use_stdin) { int c; do { errno = 0; c = getc_unlocked(stdin); saved = errno; if (c == EOF && ferror(stdin) && saved == EINTR) clearerr(stdin); } while (c == EOF && saved == EINTR); if (c != EOF) { *(unsigned char *)bytes = (unsigned char)c; result = 1; } else if (ferror(stdin)) { result = -1; if (!saved) saved = EIO; clearerr(stdin); } else { result = 0; } } else { size_t count = length > 0x7fffffffU ? 0x7fffffffU : length; do { result = read(fd, bytes, count); } while (result < 0 && errno == EINTR); if (result < 0) saved = errno; } /* The caller exclusively uses this input port. Restore its blocking mode before exposing bytes or invoking host/Scheme code again. */ int restore_error = changed && fcntl(fd, F_SETFL, flags) < 0 ? errno : 0; if (use_stdin) funlockfile(stdin); if (restore_error) return -(intptr_t)(restore_error + 1); if (result < 0) { if (saved == EAGAIN || saved == EWOULDBLOCK) return -1; return -(intptr_t)(saved + 1); } return result; #endif } #endif