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