src/wrappers.h
author Volker Birk <vb@pep-project.org>
Wed, 25 Mar 2015 15:20:25 +0100
changeset 125 5119178815d8
parent 89 aef5a4bc78f3
child 130 507895068f83
permissions -rw-r--r--
Windoze fixes
     1 #pragma once
     2 
     3 #include <errno.h>
     4 #include <stdarg.h>
     5 
     6 #ifdef WIN32
     7 #include "platform_windows.h"
     8 #endif
     9 
    10 #ifdef EOF // stdio.h
    11 
    12 static inline FILE * Fopen(const char *filename, const char *mode)
    13 {
    14     FILE * f;
    15 
    16     do {
    17         f = fopen(filename, mode);
    18     } while (f == NULL && errno == EINTR);
    19 
    20     return f;
    21 }
    22 #define fopen >@<
    23 
    24 static inline FILE * Fdopen(int fildes, const char *mode)
    25 {
    26     FILE * f;
    27 
    28     do {
    29         f = fdopen(fildes, mode);
    30     } while (f == NULL && errno == EINTR);
    31 
    32     return f;
    33 }
    34 #define fdopen >@<
    35 
    36 static inline char *Fgets(char * str, int size, FILE * stream)
    37 {
    38     char * s;
    39 
    40     do {
    41         s = fgets(str, size, stream);
    42     } while (s == NULL && errno == EINTR);
    43 
    44     return s;
    45 }
    46 #define fgets >@<
    47 
    48 static inline int Fputs(const char *str, FILE * stream)
    49 {
    50     int r;
    51 
    52     do {
    53         r = fputs(str, stream);
    54     } while (r == EOF && errno == EINTR);
    55 
    56     return r;
    57 }
    58 #define fputs >@<
    59 
    60 static inline int Fclose(FILE *stream)
    61 {
    62     int r;
    63 
    64     do {
    65         r = fclose(stream);
    66     } while (r == EOF && errno == EINTR);
    67 
    68     return r;
    69 }
    70 #define fclose >@<
    71 
    72 static inline FILE * Freopen(
    73         const char *filename,
    74         const char *mode,
    75         FILE * stream
    76     )
    77 {
    78     FILE * f;
    79 
    80     do {
    81         f = freopen(filename, mode, stream);
    82     } while (f == NULL && errno == EINTR);
    83 
    84     return f;
    85 }
    86 #define freopen >@<
    87 
    88 static inline int Fprintf(FILE * stream, const char * format, ...)
    89 {
    90     int n;
    91     va_list arglist;
    92 
    93     va_start(arglist, format);
    94 
    95     do {
    96         n = vfprintf(stream, format, arglist);
    97     } while (n < 0 && errno == EINTR);
    98 
    99     va_end( arglist );
   100 
   101     return n;
   102 }
   103 #define fprintf >@<
   104 
   105 static inline size_t Fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
   106 {
   107     size_t r = 0;
   108 
   109     do {
   110         clearerr(stream);
   111         size_t n = fwrite((char *) ptr + r, size, nitems, stream);
   112         nitems -= n;
   113         r += n * size;
   114     } while (nitems && ferror(stream) == EINTR);
   115 
   116     return r;
   117 }
   118 #define fwrite >@<
   119 
   120 static inline size_t Fread(void *ptr, size_t size, size_t nitems, FILE *stream)
   121 {
   122     size_t r = 0;
   123 
   124     do {
   125         clearerr(stream);
   126         size_t n = fread((char *) ptr + r, size, nitems, stream);
   127         nitems -= n;
   128         r += n * size;
   129     } while (!feof(stream) && nitems && ferror(stream) == EINTR);
   130 
   131     return r;
   132 }
   133 #define fread >@<
   134 
   135 static inline int Fflush(FILE *stream)
   136 {
   137     int r;
   138 
   139     do {
   140         r = fflush(stream);
   141     } while (r == -1 && errno == EINTR);
   142 
   143     return r;
   144 }
   145 #define fflush >@<
   146 
   147 #endif // stdio.h
   148 
   149 #ifdef F_OK // unistd.h
   150 
   151 static inline int Mkstemp(char *template)
   152 {
   153     int fd;
   154 
   155     do {
   156         fd = mkstemp(template);
   157     } while (fd == -1 && errno == EINTR);
   158 
   159     return fd;
   160 }
   161 #define mkstemp >@<
   162 
   163 static inline int Close(int fildes)
   164 {
   165     int r;
   166 
   167     do {
   168         r = close(fildes);
   169     } while (r == -1 && errno == EINTR);
   170 
   171     return r;
   172 }
   173 #define close >@<
   174 
   175 static inline ssize_t Write(int fildes, const void *buf, size_t nbyte)
   176 {
   177     ssize_t r;
   178 
   179     do {
   180         r = write(fildes, buf, nbyte);
   181     } while (r == -1 && errno == EINTR);
   182 
   183     return r;
   184 }
   185 #define write >@<
   186 
   187 static inline ssize_t Read(int fildes, void *buf, size_t nbyte)
   188 {
   189     ssize_t r;
   190 
   191     do {
   192         r = read(fildes, buf, nbyte);
   193     } while (r == -1 && errno == EINTR);
   194 
   195     return r;
   196 }
   197 #define read >@<
   198 
   199 static inline int Ftruncate(int fildes, off_t length)
   200 {
   201     int r;
   202 
   203     do {
   204         r = ftruncate(fildes, length);
   205     } while (r == -1 && errno == EINTR);
   206 
   207     return r;
   208 }
   209 #define ftruncate >@<
   210 
   211 static inline int Dup2(int fildes, int fildes2)
   212 {
   213     int fd;
   214 
   215     do {
   216         fd = dup2(fildes, fildes2);
   217     } while (fd == -1 && errno == EINTR);
   218 
   219     return fd;
   220 }
   221 #define dup2 >@<
   222 
   223 #endif
   224 
   225 #ifdef FD_CLR // select.h
   226 
   227 static inline int Select(
   228         int nfds,
   229         fd_set *readfds,
   230         fd_set *writefds,
   231         fd_set *errorfds,
   232         struct timeval *timeout
   233     )
   234 {
   235     int r;
   236 
   237     do {
   238         r = select(nfds, readfds, writefds, errorfds, timeout);
   239     } while (r == -1 && errno == EINTR);
   240 
   241     return r;
   242 }
   243 #define select >@<
   244 
   245 #endif // select.h
   246 
   247 #ifdef F_SETLKW // fcntl.h
   248 
   249 static inline int Fcntl(int fildes, int cmd, void *structure)
   250 {
   251     int r;
   252 
   253     do {
   254         r = fcntl(fildes, cmd, structure);
   255     } while (r == -1 && errno == EINTR);
   256 
   257     return r;
   258 }
   259 // fnctl does only risk EINTR if cmd == F_SETLKW
   260 
   261 static inline int Open(const char *path, int oflag, ...)
   262 {
   263     int fd;
   264 
   265     do {
   266         fd = open(path, oflag, 0);
   267     } while (fd == -1 && errno == EINTR);
   268 
   269     return fd;
   270 }
   271 #define open >@<
   272 
   273 static inline int Creat(const char *path, mode_t mode)
   274 {
   275     int fd;
   276 
   277     do {
   278         fd = creat(path, mode);
   279     } while (fd == -1 && errno == EINTR);
   280 
   281     return fd;
   282 }
   283 #define creat >@<
   284 
   285 #endif // fcntl.h
   286 
   287 #ifdef MSG_PEEK // socket.h
   288 
   289 static inline ssize_t Recv(int socket, void *buffer, size_t length, int flags)
   290 {
   291     ssize_t r;
   292 
   293     do {
   294         r = recv(socket, buffer, length, flags);
   295     } while (r == -1 && errno == EINTR);
   296 
   297     return r;
   298 }
   299 #define recv >@<
   300 
   301 static inline ssize_t Send(int socket, const void *buffer, size_t length, int flags)
   302 {
   303     ssize_t r;
   304 
   305     do {
   306         r = send(socket, buffer, length, flags);
   307     } while (r == -1 && errno == EINTR);
   308 
   309     return r;
   310 }
   311 #define send >@<
   312 
   313 #endif // socket.h
   314 
   315 #ifdef WNOHANG // wait.h
   316 
   317 static inline pid_t Waitpid(pid_t pid, int *stat_loc, int options)
   318 {
   319     pid_t r;
   320 
   321     do {
   322         r = waitpid(pid, stat_loc, options);
   323     } while (r == -1 && errno == EINTR);
   324 
   325     return r;
   326 }
   327 #define waitpid >@<
   328 
   329 #endif
   330