src/wrappers.h
author Volker Birk <vb@pep-project.org>
Thu, 26 Mar 2015 15:30:21 +0100
changeset 130 507895068f83
parent 125 5119178815d8
child 1513 e7f7e42385b5
permissions -rw-r--r--
modifications for Windoze
vb@68
     1
#pragma once
vb@68
     2
vb@130
     3
#include "platform.h"
vb@130
     4
vb@68
     5
#include <errno.h>
vb@68
     6
#include <stdarg.h>
vb@130
     7
#include <stdio.h>
vb@130
     8
#include <fcntl.h>
vb@68
     9
vb@68
    10
static inline FILE * Fopen(const char *filename, const char *mode)
vb@68
    11
{
vb@68
    12
    FILE * f;
vb@68
    13
vb@68
    14
    do {
vb@68
    15
        f = fopen(filename, mode);
vb@68
    16
    } while (f == NULL && errno == EINTR);
vb@68
    17
vb@68
    18
    return f;
vb@68
    19
}
vb@89
    20
vb@89
    21
static inline FILE * Fdopen(int fildes, const char *mode)
vb@89
    22
{
vb@89
    23
    FILE * f;
vb@89
    24
vb@89
    25
    do {
vb@89
    26
        f = fdopen(fildes, mode);
vb@89
    27
    } while (f == NULL && errno == EINTR);
vb@89
    28
vb@89
    29
    return f;
vb@89
    30
}
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
vb@89
    43
static inline int Fputs(const char *str, FILE * stream)
vb@89
    44
{
vb@89
    45
    int r;
vb@89
    46
vb@89
    47
    do {
vb@89
    48
        r = fputs(str, stream);
vb@89
    49
    } while (r == EOF && errno == EINTR);
vb@89
    50
vb@89
    51
    return r;
vb@89
    52
}
vb@68
    53
vb@68
    54
static inline int Fclose(FILE *stream)
vb@68
    55
{
vb@68
    56
    int r;
vb@68
    57
vb@68
    58
    do {
vb@68
    59
        r = fclose(stream);
vb@89
    60
    } while (r == EOF && errno == EINTR);
vb@68
    61
vb@68
    62
    return r;
vb@68
    63
}
vb@68
    64
vb@68
    65
static inline FILE * Freopen(
vb@68
    66
        const char *filename,
vb@68
    67
        const char *mode,
vb@68
    68
        FILE * stream
vb@68
    69
    )
vb@68
    70
{
vb@68
    71
    FILE * f;
vb@68
    72
vb@68
    73
    do {
vb@68
    74
        f = freopen(filename, mode, stream);
vb@68
    75
    } while (f == NULL && errno == EINTR);
vb@68
    76
vb@68
    77
    return f;
vb@68
    78
}
vb@68
    79
vb@68
    80
static inline int Fprintf(FILE * stream, const char * format, ...)
vb@68
    81
{
vb@68
    82
    int n;
vb@68
    83
    va_list arglist;
vb@68
    84
vb@68
    85
    va_start(arglist, format);
vb@68
    86
vb@68
    87
    do {
vb@68
    88
        n = vfprintf(stream, format, arglist);
vb@68
    89
    } while (n < 0 && errno == EINTR);
vb@68
    90
vb@68
    91
    va_end( arglist );
vb@68
    92
vb@68
    93
    return n;
vb@68
    94
}
vb@89
    95
vb@89
    96
static inline size_t Fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
vb@89
    97
{
vb@89
    98
    size_t r = 0;
vb@89
    99
vb@89
   100
    do {
vb@89
   101
        clearerr(stream);
vb@89
   102
        size_t n = fwrite((char *) ptr + r, size, nitems, stream);
vb@89
   103
        nitems -= n;
vb@89
   104
        r += n * size;
vb@89
   105
    } while (nitems && ferror(stream) == EINTR);
vb@89
   106
vb@89
   107
    return r;
vb@89
   108
}
vb@89
   109
vb@89
   110
static inline size_t Fread(void *ptr, size_t size, size_t nitems, FILE *stream)
vb@89
   111
{
vb@89
   112
    size_t r = 0;
vb@89
   113
vb@89
   114
    do {
vb@89
   115
        clearerr(stream);
vb@89
   116
        size_t n = fread((char *) ptr + r, size, nitems, stream);
vb@89
   117
        nitems -= n;
vb@89
   118
        r += n * size;
vb@89
   119
    } while (!feof(stream) && nitems && ferror(stream) == EINTR);
vb@89
   120
vb@89
   121
    return r;
vb@89
   122
}
vb@89
   123
vb@89
   124
static inline int Fflush(FILE *stream)
vb@89
   125
{
vb@89
   126
    int r;
vb@89
   127
vb@89
   128
    do {
vb@89
   129
        r = fflush(stream);
vb@89
   130
    } while (r == -1 && errno == EINTR);
vb@89
   131
vb@89
   132
    return r;
vb@89
   133
}
vb@68
   134
vb@68
   135
static inline int Mkstemp(char *template)
vb@68
   136
{
vb@68
   137
    int fd;
vb@68
   138
vb@68
   139
    do {
vb@68
   140
        fd = mkstemp(template);
vb@68
   141
    } while (fd == -1 && errno == EINTR);
vb@68
   142
vb@68
   143
    return fd;
vb@68
   144
}
vb@68
   145
vb@68
   146
static inline int Close(int fildes)
vb@68
   147
{
vb@68
   148
    int r;
vb@68
   149
vb@68
   150
    do {
vb@68
   151
        r = close(fildes);
vb@68
   152
    } while (r == -1 && errno == EINTR);
vb@68
   153
vb@68
   154
    return r;
vb@68
   155
}