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