src/wrappers.h
author vb
Mon, 23 Feb 2015 15:39:30 +0100
changeset 68 3cbe911b2538
child 89 aef5a4bc78f3
permissions -rw-r--r--
sorry, Edouard
vb@68
     1
#pragma once
vb@68
     2
vb@68
     3
#include <errno.h>
vb@68
     4
#include <stdarg.h>
vb@68
     5
#include <stdio.h>
vb@68
     6
#include <unistd.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@68
    18
vb@68
    19
static inline char *Fgets(char * str, int size, FILE * stream)
vb@68
    20
{
vb@68
    21
    char * s;
vb@68
    22
vb@68
    23
    do {
vb@68
    24
        s = fgets(str, size, stream);
vb@68
    25
    } while (s == NULL && errno == EINTR);
vb@68
    26
vb@68
    27
    return s;
vb@68
    28
}
vb@68
    29
vb@68
    30
static inline int Fclose(FILE *stream)
vb@68
    31
{
vb@68
    32
    int r;
vb@68
    33
vb@68
    34
    do {
vb@68
    35
        r = fclose(stream);
vb@68
    36
    } while (r == -1 && errno == EINTR);
vb@68
    37
vb@68
    38
    return r;
vb@68
    39
}
vb@68
    40
vb@68
    41
static inline FILE * Freopen(
vb@68
    42
        const char *filename,
vb@68
    43
        const char *mode,
vb@68
    44
        FILE * stream
vb@68
    45
    )
vb@68
    46
{
vb@68
    47
    FILE * f;
vb@68
    48
vb@68
    49
    do {
vb@68
    50
        f = freopen(filename, mode, stream);
vb@68
    51
    } while (f == NULL && errno == EINTR);
vb@68
    52
vb@68
    53
    return f;
vb@68
    54
}
vb@68
    55
vb@68
    56
static inline int Fprintf(FILE * stream, const char * format, ...)
vb@68
    57
{
vb@68
    58
    int n;
vb@68
    59
    va_list arglist;
vb@68
    60
vb@68
    61
    va_start(arglist, format);
vb@68
    62
vb@68
    63
    do {
vb@68
    64
        n = vfprintf(stream, format, arglist);
vb@68
    65
    } while (n < 0 && errno == EINTR);
vb@68
    66
vb@68
    67
    va_end( arglist );
vb@68
    68
vb@68
    69
    return n;
vb@68
    70
}
vb@68
    71
vb@68
    72
static inline int Mkstemp(char *template)
vb@68
    73
{
vb@68
    74
    int fd;
vb@68
    75
vb@68
    76
    do {
vb@68
    77
        fd = mkstemp(template);
vb@68
    78
    } while (fd == -1 && errno == EINTR);
vb@68
    79
vb@68
    80
    return fd;
vb@68
    81
}
vb@68
    82
vb@68
    83
static inline FILE * Fdopen(int fildes, const char *mode)
vb@68
    84
{
vb@68
    85
    FILE * f;
vb@68
    86
vb@68
    87
    do {
vb@68
    88
        f = fdopen(fildes, mode);
vb@68
    89
    } while (f == NULL && errno == EINTR);
vb@68
    90
vb@68
    91
    return f;
vb@68
    92
}
vb@68
    93
vb@68
    94
static inline int Close(int fildes)
vb@68
    95
{
vb@68
    96
    int r;
vb@68
    97
vb@68
    98
    do {
vb@68
    99
        r = close(fildes);
vb@68
   100
    } while (r == -1 && errno == EINTR);
vb@68
   101
vb@68
   102
    return r;
vb@68
   103
}
vb@68
   104
vb@68
   105
static inline size_t Fread1(void *ptr, size_t size, FILE *stream)
vb@68
   106
{
vb@68
   107
    char *_buf = ptr;
vb@68
   108
    size_t rest = size;
vb@68
   109
    size_t bytes_read;
vb@68
   110
vb@68
   111
    for (bytes_read = 0; rest > 0; _buf += rest) {
vb@68
   112
        clearerr(stream);
vb@68
   113
vb@68
   114
        bytes_read = rest * fread(_buf, rest, 1, stream);
vb@68
   115
        rest -= bytes_read;
vb@68
   116
vb@68
   117
        if (ferror(stream) != 0 && ferror(stream) != EINTR)
vb@68
   118
            goto err_file;
vb@68
   119
vb@68
   120
        if (feof(stream))
vb@68
   121
            goto err_file;
vb@68
   122
    }
vb@68
   123
vb@68
   124
    return size;
vb@68
   125
vb@68
   126
err_file:
vb@68
   127
    return size - rest;
vb@68
   128
}
vb@68
   129