32 #include <system_error>
36 #include <sys/types.h>
47 using namespace scc::util;
54 r = ::truncate(path, length);
56 while (r == -1 && errno == EINTR);
62 int r = truncate(path, length);
66 st <<
"truncate(" << path <<
"," << length <<
")";
67 throw std::system_error(errno, std::system_category(), st.str());
77 r = ::ftruncate(fd, length);
79 while (r == -1 && errno == EINTR);
85 int r = ftruncate(fd, length);
89 st <<
"ftruncate(" << fd <<
"," << length <<
")";
90 throw std::system_error(errno, std::system_category(), st.str());
102 while (r == -1 && errno == EINTR);
111 std::stringstream st;
112 st <<
"close(" << fd <<
")";
113 throw std::system_error(errno, std::system_category(), st.str());
123 r = ::read(fd, buf, count);
125 while (r == -1 && errno == EINTR);
134 std::stringstream st;
135 st <<
"read(" << fd <<
", ...)";
136 throw std::system_error(errno, std::system_category(), st.str());
146 r = ::write(fd, buf, count);
148 while (r == -1 && errno == EINTR);
157 std::stringstream st;
158 st <<
"write(" << fd <<
", ...)";
159 throw std::system_error(errno, std::system_category(), st.str());
171 while (r == -1 && errno == EINTR);
180 std::stringstream st;
181 st <<
"dup(" << oldfd <<
")";
182 throw std::system_error(errno, std::system_category(), st.str());
192 r = ::dup2(oldfd, newfd);
194 while (r == -1 && (errno == EINTR||errno == EBUSY));
203 std::stringstream st;
204 st <<
"dup2(" << oldfd <<
"," << newfd <<
")";
205 throw std::system_error(errno, std::system_category(), st.str());
215 r = ::open(pathname, flags);
217 while (r == -1 && errno == EINTR);
226 std::stringstream st;
227 st <<
"open(" << pathname <<
"," << flags <<
")";
228 throw std::system_error(errno, std::system_category(), st.str());
238 r = ::open(pathname, flags, mode);
240 while (r == -1 && errno == EINTR);
246 int r =
safe_open(pathname, flags, mode);
249 std::stringstream st;
250 st <<
"open(" << pathname <<
"," << flags <<
"," << mode <<
")";
251 throw std::system_error(errno, std::system_category(), st.str());
261 r = ::fopen(pathname, mode);
263 while (r ==
nullptr && errno == EINTR);
272 std::stringstream st;
273 st <<
"fopen(" << pathname <<
"," << mode <<
")";
274 throw std::system_error(errno, std::system_category(), st.str());
284 r = ::fclose(stream);
286 while (r == -1 && errno == EINTR);
295 std::stringstream st;
297 throw std::system_error(errno, std::system_category(), st.str());
302 static int safe_vfscanf(FILE *stream,
const char *format, va_list ap)
307 if (ferror(stream) && errno == EINTR)
311 r = ::vfscanf(stream, format, ap);
313 while (r == EOF && ferror(stream) && errno == EINTR);
320 va_start(va, format);
321 int r = safe_vfscanf(stream, format, va);
329 va_start(va, format);
330 int r = safe_vfscanf(stream, format, va);
332 if (r == EOF && ferror(stream))
334 std::stringstream st;
336 throw std::system_error(errno, std::system_category(), st.str());
346 r = ::getline(lineptr, n, stream);
348 while (r == -1 && errno == EINTR);
357 std::stringstream st;
359 throw std::system_error(errno, std::system_category(), st.str());
374 r = ::waitpid(pid, wstatus, options);
376 while (r == -1 && errno == EINTR);
390 std::stringstream st;
391 st <<
"waitpid(" << pid <<
")";
392 throw std::system_error(errno, std::system_category(), st.str());
int safe_close(int fd)
Signal safe close.
int safe_dup2_throw(int oldfd, int newfd)
Signal safe dup2, throws system_error on error.
ssize_t safe_read_throw(int fd, void *buf, size_t count)
Signal safe read, throws system_error on error.
int safe_wait_throw(int *wstatus)
Signal safe wait, throws system_error on error.
int safe_fclose_throw(FILE *stream)
Signal safe fclose, throws system_error on error.
ssize_t safe_write_throw(int fd, const void *buf, size_t count)
Signal safe write, throws system_error on error.
FILE * safe_fopen(const char *pathname, const char *mode)
Signal safe fopen.
ssize_t safe_read(int fd, void *buf, size_t count)
Signal safe read.
int safe_wait(int *wstatus)
Signal safe wait.
ssize_t safe_write(int fd, const void *buf, size_t count)
Signal safe write.
int safe_dup_throw(int oldfd)
Signal safe dup, throws system_error on error.
ssize_t safe_getline_throw(char **lineptr, size_t *n, FILE *stream)
Signal safe getline, throw on error.
ssize_t safe_getline(char **lineptr, size_t *n, FILE *stream)
Signal safe getline.
int safe_fscanf(FILE *stream, const char *format,...)
Signal safe fscanf.
int safe_close_throw(int fd)
Signal safe close, throws system_error on error.
int safe_waitpid_throw(pid_t pid, int *wstatus, int options)
Signal safe waitpid, throws system_error on error.
int safe_ftruncate_throw(int fd, off_t length)
Signal safe ftruncate, throw on error.
int safe_fclose(FILE *stream)
Signal safe fclose.
int safe_open_throw(const char *pathname, int flags)
Signal safe open, throws system_error on error.
int safe_open(const char *pathname, int flags)
Signal safe open.
int safe_truncate(const char *path, off_t length)
Signal safe truncate.
int safe_waitpid(pid_t pid, int *wstatus, int options)
Signal safe waitpid.
int safe_dup2(int oldfd, int newfd)
Signal safe dup2.
int safe_fscanf_throw(FILE *stream, const char *format,...)
Signal safe fscanf, throw on error.
int safe_truncate_throw(const char *path, off_t length)
Signal safe truncate, throw on error.
int safe_ftruncate(int fd, off_t length)
Signal safe ftruncate.
int safe_dup(int oldfd)
Signal safe dup.
FILE * safe_fopen_throw(const char *pathname, const char *mode)
Signal safe fopen, throws system_error on error.
Signal-safe C library wrapper.