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