vile / scheme / port_file.h
158 lines · 158 sloc · 5.35 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1#ifndef VILE_PORT_FILE_H
2#define VILE_PORT_FILE_H
3#include <errno.h>
4#include <stddef.h>
5#include <stdint.h>
6#include <stdio.h>
7#include <fcntl.h>
8#ifdef _WIN32
9#include <io.h>
10#include <wchar.h>
11#include <sys/stat.h>
12#else
13#include <unistd.h>
14#endif
15/* Selected file constructors need atomic flag combinations which the V
16 file opener does not expose. This descriptor has no FILE/stdio state. */
17static int vile_port_open(const void *path, unsigned int options, int read_write) {
18 int flags;
19#ifdef _WIN32
20 flags = (read_write ? _O_RDWR : _O_WRONLY) | _O_BINARY;
21 if (!(options & 1)) flags |= _O_CREAT;
22 if (!(options & 3)) flags |= _O_EXCL;
23 if (!(options & 4)) flags |= _O_TRUNC;
24 int fd;
25 do { fd = _wopen((const wchar_t *)path, flags, _S_IREAD | _S_IWRITE); }
26 while (fd < 0 && errno == EINTR);
27#else
28 flags = read_write ? O_RDWR : O_WRONLY;
29 if (!(options & 1)) flags |= O_CREAT;
30 if (!(options & 3)) flags |= O_EXCL;
31 if (!(options & 4)) flags |= O_TRUNC;
32#ifdef O_CLOEXEC
33 flags |= O_CLOEXEC;
34#endif
35 int fd;
36 do { fd = open((const char *)path, flags, 0666); }
37 while (fd < 0 && errno == EINTR);
38#endif
39 return fd < 0 ? -(errno ? errno : EIO) : fd;
40}
41static int vile_port_close_descriptor(int fd) {
42 /* Do not retry close after EINTR: the descriptor may already be released. */
43#ifdef _WIN32
44 int result = _close(fd);
45#else
46 int result = close(fd);
47#endif
48 return result < 0 ? -(errno ? errno : EIO) : 0;
49}
50/* Bound transfers for Windows' int-sized CRT API and V's descriptor API.
51 Retry interruptions without allocating or advancing the caller's buffer. */
52static int vile_port_flush_standard(int fd) {
53 FILE *stream = fd == 1 ? stdout : stderr;
54 int result;
55 do {
56 result = fflush(stream);
57 if (result && errno == EINTR) clearerr(stream);
58 } while (result && errno == EINTR);
59 return result ? -(errno ? errno : EIO) : 0;
60}
61static intptr_t vile_port_read(int fd, void *bytes, size_t length) {
62 size_t count = length > 0x7fffffffU ? 0x7fffffffU : length;
63 intptr_t result;
64 do {
65#ifdef _WIN32
66 result = _read(fd, bytes, (unsigned int)count);
67#else
68 result = read(fd, bytes, count);
69#endif
70 } while (result < 0 && errno == EINTR);
71 return result < 0 ? -(intptr_t)errno : result;
72}
73static intptr_t vile_port_write(int fd, const void *bytes, size_t length) {
74 size_t count = length > 0x7fffffffU ? 0x7fffffffU : length;
75 intptr_t result;
76 do {
77#ifdef _WIN32
78 result = _write(fd, bytes, (unsigned int)count);
79#else
80 result = write(fd, bytes, count);
81#endif
82 } while (result < 0 && errno == EINTR);
83 return result < 0 ? -(intptr_t)errno : result;
84}
85/* Owned ports use descriptor I/O, so their position must use the same layer.
86 Mixing stdio seek/tell with raw reads leaves cached FILE offsets stale. */
87static int64_t vile_port_seek(int fd, int64_t offset, int current) {
88 int64_t result;
89 int origin = current ? SEEK_CUR : SEEK_SET;
90 do {
91#ifdef _WIN32
92 result = _lseeki64(fd, offset, origin);
93#else
94 if ((int64_t)(off_t)offset != offset) return -(int64_t)EOVERFLOW;
95 result = (int64_t)lseek(fd, (off_t)offset, origin);
96#endif
97 } while (result < 0 && errno == EINTR);
98 return result < 0 ? -(int64_t)(errno ? errno : EIO) : result;
99}
100/* Nonblocking protocol: -1 means temporarily unavailable, 0 means EOF,
101 and values below -1 encode -(errno+1). Keep C stdio read-ahead coherent for
102 stdin. No V allocation/callback can occur while its lock or flags are held. */
103static intptr_t vile_port_read_ready(int fd, void *bytes, size_t length, int use_stdin) {
104#ifdef _WIN32
105 (void)fd; (void)bytes; (void)length; (void)use_stdin;
106 return -(intptr_t)(ENOSYS + 1);
107#else
108 if (use_stdin) flockfile(stdin);
109 int flags = fcntl(fd, F_GETFL);
110 if (flags < 0) {
111 int saved = errno;
112 if (use_stdin) funlockfile(stdin);
113 return -(intptr_t)(saved + 1);
114 }
115 int changed = !(flags & O_NONBLOCK);
116 if (changed && fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
117 int saved = errno;
118 if (use_stdin) funlockfile(stdin);
119 return -(intptr_t)(saved + 1);
120 }
121 intptr_t result;
122 int saved = 0;
123 if (use_stdin) {
124 int c;
125 do {
126 errno = 0;
127 c = getc_unlocked(stdin);
128 saved = errno;
129 if (c == EOF && ferror(stdin) && saved == EINTR) clearerr(stdin);
130 } while (c == EOF && saved == EINTR);
131 if (c != EOF) {
132 *(unsigned char *)bytes = (unsigned char)c;
133 result = 1;
134 } else if (ferror(stdin)) {
135 result = -1;
136 if (!saved) saved = EIO;
137 clearerr(stdin);
138 } else {
139 result = 0;
140 }
141 } else {
142 size_t count = length > 0x7fffffffU ? 0x7fffffffU : length;
143 do { result = read(fd, bytes, count); } while (result < 0 && errno == EINTR);
144 if (result < 0) saved = errno;
145 }
146 /* The caller exclusively uses this input port. Restore its blocking mode
147 before exposing bytes or invoking host/Scheme code again. */
148 int restore_error = changed && fcntl(fd, F_SETFL, flags) < 0 ? errno : 0;
149 if (use_stdin) funlockfile(stdin);
150 if (restore_error) return -(intptr_t)(restore_error + 1);
151 if (result < 0) {
152 if (saved == EAGAIN || saved == EWOULDBLOCK) return -1;
153 return -(intptr_t)(saved + 1);
154 }
155 return result;
156#endif
157}
158#endif
159