src/wrappers.h
changeset 89 aef5a4bc78f3
parent 68 3cbe911b2538
child 125 5119178815d8
     1.1 --- a/src/wrappers.h	Wed Mar 04 13:32:07 2015 +0100
     1.2 +++ b/src/wrappers.h	Sat Mar 07 20:29:13 2015 +0100
     1.3 @@ -2,8 +2,8 @@
     1.4  
     1.5  #include <errno.h>
     1.6  #include <stdarg.h>
     1.7 -#include <stdio.h>
     1.8 -#include <unistd.h>
     1.9 +
    1.10 +#ifdef EOF // stdio.h
    1.11  
    1.12  static inline FILE * Fopen(const char *filename, const char *mode)
    1.13  {
    1.14 @@ -15,6 +15,19 @@
    1.15  
    1.16      return f;
    1.17  }
    1.18 +#define fopen >@<
    1.19 +
    1.20 +static inline FILE * Fdopen(int fildes, const char *mode)
    1.21 +{
    1.22 +    FILE * f;
    1.23 +
    1.24 +    do {
    1.25 +        f = fdopen(fildes, mode);
    1.26 +    } while (f == NULL && errno == EINTR);
    1.27 +
    1.28 +    return f;
    1.29 +}
    1.30 +#define fdopen >@<
    1.31  
    1.32  static inline char *Fgets(char * str, int size, FILE * stream)
    1.33  {
    1.34 @@ -26,6 +39,19 @@
    1.35  
    1.36      return s;
    1.37  }
    1.38 +#define fgets >@<
    1.39 +
    1.40 +static inline int Fputs(const char *str, FILE * stream)
    1.41 +{
    1.42 +    int r;
    1.43 +
    1.44 +    do {
    1.45 +        r = fputs(str, stream);
    1.46 +    } while (r == EOF && errno == EINTR);
    1.47 +
    1.48 +    return r;
    1.49 +}
    1.50 +#define fputs >@<
    1.51  
    1.52  static inline int Fclose(FILE *stream)
    1.53  {
    1.54 @@ -33,10 +59,11 @@
    1.55  
    1.56      do {
    1.57          r = fclose(stream);
    1.58 -    } while (r == -1 && errno == EINTR);
    1.59 +    } while (r == EOF && errno == EINTR);
    1.60  
    1.61      return r;
    1.62  }
    1.63 +#define fclose >@<
    1.64  
    1.65  static inline FILE * Freopen(
    1.66          const char *filename,
    1.67 @@ -52,6 +79,7 @@
    1.68  
    1.69      return f;
    1.70  }
    1.71 +#define freopen >@<
    1.72  
    1.73  static inline int Fprintf(FILE * stream, const char * format, ...)
    1.74  {
    1.75 @@ -68,6 +96,53 @@
    1.76  
    1.77      return n;
    1.78  }
    1.79 +#define fprintf >@<
    1.80 +
    1.81 +static inline size_t Fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
    1.82 +{
    1.83 +    size_t r = 0;
    1.84 +
    1.85 +    do {
    1.86 +        clearerr(stream);
    1.87 +        size_t n = fwrite((char *) ptr + r, size, nitems, stream);
    1.88 +        nitems -= n;
    1.89 +        r += n * size;
    1.90 +    } while (nitems && ferror(stream) == EINTR);
    1.91 +
    1.92 +    return r;
    1.93 +}
    1.94 +#define fwrite >@<
    1.95 +
    1.96 +static inline size_t Fread(void *ptr, size_t size, size_t nitems, FILE *stream)
    1.97 +{
    1.98 +    size_t r = 0;
    1.99 +
   1.100 +    do {
   1.101 +        clearerr(stream);
   1.102 +        size_t n = fread((char *) ptr + r, size, nitems, stream);
   1.103 +        nitems -= n;
   1.104 +        r += n * size;
   1.105 +    } while (!feof(stream) && nitems && ferror(stream) == EINTR);
   1.106 +
   1.107 +    return r;
   1.108 +}
   1.109 +#define fread >@<
   1.110 +
   1.111 +static inline int Fflush(FILE *stream)
   1.112 +{
   1.113 +    int r;
   1.114 +
   1.115 +    do {
   1.116 +        r = fflush(stream);
   1.117 +    } while (r == -1 && errno == EINTR);
   1.118 +
   1.119 +    return r;
   1.120 +}
   1.121 +#define fflush >@<
   1.122 +
   1.123 +#endif // stdio.h
   1.124 +
   1.125 +#ifdef F_OK // unistd.h
   1.126  
   1.127  static inline int Mkstemp(char *template)
   1.128  {
   1.129 @@ -79,17 +154,7 @@
   1.130  
   1.131      return fd;
   1.132  }
   1.133 -
   1.134 -static inline FILE * Fdopen(int fildes, const char *mode)
   1.135 -{
   1.136 -    FILE * f;
   1.137 -
   1.138 -    do {
   1.139 -        f = fdopen(fildes, mode);
   1.140 -    } while (f == NULL && errno == EINTR);
   1.141 -
   1.142 -    return f;
   1.143 -}
   1.144 +#define mkstemp >@<
   1.145  
   1.146  static inline int Close(int fildes)
   1.147  {
   1.148 @@ -101,29 +166,161 @@
   1.149  
   1.150      return r;
   1.151  }
   1.152 +#define close >@<
   1.153  
   1.154 -static inline size_t Fread1(void *ptr, size_t size, FILE *stream)
   1.155 +static inline ssize_t Write(int fildes, const void *buf, size_t nbyte)
   1.156  {
   1.157 -    char *_buf = ptr;
   1.158 -    size_t rest = size;
   1.159 -    size_t bytes_read;
   1.160 +    ssize_t r;
   1.161  
   1.162 -    for (bytes_read = 0; rest > 0; _buf += rest) {
   1.163 -        clearerr(stream);
   1.164 +    do {
   1.165 +        r = write(fildes, buf, nbyte);
   1.166 +    } while (r == -1 && errno == EINTR);
   1.167  
   1.168 -        bytes_read = rest * fread(_buf, rest, 1, stream);
   1.169 -        rest -= bytes_read;
   1.170 +    return r;
   1.171 +}
   1.172 +#define write >@<
   1.173  
   1.174 -        if (ferror(stream) != 0 && ferror(stream) != EINTR)
   1.175 -            goto err_file;
   1.176 +static inline ssize_t Read(int fildes, void *buf, size_t nbyte)
   1.177 +{
   1.178 +    ssize_t r;
   1.179  
   1.180 -        if (feof(stream))
   1.181 -            goto err_file;
   1.182 -    }
   1.183 +    do {
   1.184 +        r = read(fildes, buf, nbyte);
   1.185 +    } while (r == -1 && errno == EINTR);
   1.186  
   1.187 -    return size;
   1.188 +    return r;
   1.189 +}
   1.190 +#define read >@<
   1.191  
   1.192 -err_file:
   1.193 -    return size - rest;
   1.194 +static inline int Ftruncate(int fildes, off_t length)
   1.195 +{
   1.196 +    int r;
   1.197 +
   1.198 +    do {
   1.199 +        r = ftruncate(fildes, length);
   1.200 +    } while (r == -1 && errno == EINTR);
   1.201 +
   1.202 +    return r;
   1.203  }
   1.204 +#define ftruncate >@<
   1.205  
   1.206 +static inline int Dup2(int fildes, int fildes2)
   1.207 +{
   1.208 +    int fd;
   1.209 +
   1.210 +    do {
   1.211 +        fd = dup2(fildes, fildes2);
   1.212 +    } while (fd == -1 && errno == EINTR);
   1.213 +
   1.214 +    return fd;
   1.215 +}
   1.216 +#define dup2 >@<
   1.217 +
   1.218 +#endif
   1.219 +
   1.220 +#ifdef FD_CLR // select.h
   1.221 +
   1.222 +static inline int Select(
   1.223 +        int nfds,
   1.224 +        fd_set *readfds,
   1.225 +        fd_set *writefds,
   1.226 +        fd_set *errorfds,
   1.227 +        struct timeval *timeout
   1.228 +    )
   1.229 +{
   1.230 +    int r;
   1.231 +
   1.232 +    do {
   1.233 +        r = select(nfds, readfds, writefds, errorfds, timeout);
   1.234 +    } while (r == -1 && errno == EINTR);
   1.235 +
   1.236 +    return r;
   1.237 +}
   1.238 +#define select >@<
   1.239 +
   1.240 +#endif // select.h
   1.241 +
   1.242 +#ifdef F_SETLKW // fcntl.h
   1.243 +
   1.244 +static inline int Fcntl(int fildes, int cmd, void *structure)
   1.245 +{
   1.246 +    int r;
   1.247 +
   1.248 +    do {
   1.249 +        r = fcntl(fildes, cmd, structure);
   1.250 +    } while (r == -1 && errno == EINTR);
   1.251 +
   1.252 +    return r;
   1.253 +}
   1.254 +// fnctl does only risk EINTR if cmd == F_SETLKW
   1.255 +
   1.256 +static inline int Open(const char *path, int oflag, ...)
   1.257 +{
   1.258 +    int fd;
   1.259 +
   1.260 +    do {
   1.261 +        fd = open(path, oflag, 0);
   1.262 +    } while (fd == -1 && errno == EINTR);
   1.263 +
   1.264 +    return fd;
   1.265 +}
   1.266 +#define open >@<
   1.267 +
   1.268 +static inline int Creat(const char *path, mode_t mode)
   1.269 +{
   1.270 +    int fd;
   1.271 +
   1.272 +    do {
   1.273 +        fd = creat(path, mode);
   1.274 +    } while (fd == -1 && errno == EINTR);
   1.275 +
   1.276 +    return fd;
   1.277 +}
   1.278 +#define creat >@<
   1.279 +
   1.280 +#endif // fcntl.h
   1.281 +
   1.282 +#ifdef MSG_PEEK // socket.h
   1.283 +
   1.284 +static inline ssize_t Recv(int socket, void *buffer, size_t length, int flags)
   1.285 +{
   1.286 +    ssize_t r;
   1.287 +
   1.288 +    do {
   1.289 +        r = recv(socket, buffer, length, flags);
   1.290 +    } while (r == -1 && errno == EINTR);
   1.291 +
   1.292 +    return r;
   1.293 +}
   1.294 +#define recv >@<
   1.295 +
   1.296 +static inline ssize_t Send(int socket, const void *buffer, size_t length, int flags)
   1.297 +{
   1.298 +    ssize_t r;
   1.299 +
   1.300 +    do {
   1.301 +        r = send(socket, buffer, length, flags);
   1.302 +    } while (r == -1 && errno == EINTR);
   1.303 +
   1.304 +    return r;
   1.305 +}
   1.306 +#define send >@<
   1.307 +
   1.308 +#endif // socket.h
   1.309 +
   1.310 +#ifdef WNOHANG // wait.h
   1.311 +
   1.312 +static inline pid_t Waitpid(pid_t pid, int *stat_loc, int options)
   1.313 +{
   1.314 +    pid_t r;
   1.315 +
   1.316 +    do {
   1.317 +        r = waitpid(pid, stat_loc, options);
   1.318 +    } while (r == -1 && errno == EINTR);
   1.319 +
   1.320 +    return r;
   1.321 +}
   1.322 +#define waitpid >@<
   1.323 +
   1.324 +#endif
   1.325 +