src/wrappers.h
author Krista Bennett <krista@pep-project.org>
Fri, 04 May 2018 16:30:21 +0200
branchlocal_cpptest
changeset 2652 43b913f99a27
parent 1513 e7f7e42385b5
permissions -rw-r--r--
Shelving broken things to break other things
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@68
     4
#pragma once
vb@68
     5
vb@130
     6
#include "platform.h"
vb@130
     7
vb@68
     8
#include <errno.h>
vb@68
     9
#include <stdarg.h>
vb@130
    10
#include <stdio.h>
vb@130
    11
#include <fcntl.h>
vb@68
    12
vb@68
    13
static inline FILE * Fopen(const char *filename, const char *mode)
vb@68
    14
{
vb@68
    15
    FILE * f;
vb@68
    16
vb@68
    17
    do {
vb@68
    18
        f = fopen(filename, mode);
vb@68
    19
    } while (f == NULL && errno == EINTR);
vb@68
    20
vb@68
    21
    return f;
vb@68
    22
}
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@68
    34
vb@68
    35
static inline char *Fgets(char * str, int size, FILE * stream)
vb@68
    36
{
vb@68
    37
    char * s;
vb@68
    38
vb@68
    39
    do {
vb@68
    40
        s = fgets(str, size, stream);
vb@68
    41
    } while (s == NULL && errno == EINTR);
vb@68
    42
vb@68
    43
    return s;
vb@68
    44
}
vb@89
    45
vb@89
    46
static inline int Fputs(const char *str, FILE * stream)
vb@89
    47
{
vb@89
    48
    int r;
vb@89
    49
vb@89
    50
    do {
vb@89
    51
        r = fputs(str, stream);
vb@89
    52
    } while (r == EOF && errno == EINTR);
vb@89
    53
vb@89
    54
    return r;
vb@89
    55
}
vb@68
    56
vb@68
    57
static inline int Fclose(FILE *stream)
vb@68
    58
{
vb@68
    59
    int r;
vb@68
    60
vb@68
    61
    do {
vb@68
    62
        r = fclose(stream);
vb@89
    63
    } while (r == EOF && errno == EINTR);
vb@68
    64
vb@68
    65
    return r;
vb@68
    66
}
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@68
    82
vb@68
    83
static inline int Fprintf(FILE * stream, const char * format, ...)
vb@68
    84
{
vb@68
    85
    int n;
vb@68
    86
    va_list arglist;
vb@68
    87
vb@68
    88
    va_start(arglist, format);
vb@68
    89
vb@68
    90
    do {
vb@68
    91
        n = vfprintf(stream, format, arglist);
vb@68
    92
    } while (n < 0 && errno == EINTR);
vb@68
    93
vb@68
    94
    va_end( arglist );
vb@68
    95
vb@68
    96
    return n;
vb@68
    97
}
vb@89
    98
vb@89
    99
static inline size_t Fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
vb@89
   100
{
vb@89
   101
    size_t r = 0;
vb@89
   102
vb@89
   103
    do {
vb@89
   104
        clearerr(stream);
vb@89
   105
        size_t n = fwrite((char *) ptr + r, size, nitems, stream);
vb@89
   106
        nitems -= n;
vb@89
   107
        r += n * size;
vb@89
   108
    } while (nitems && ferror(stream) == EINTR);
vb@89
   109
vb@89
   110
    return r;
vb@89
   111
}
vb@89
   112
vb@89
   113
static inline size_t Fread(void *ptr, size_t size, size_t nitems, FILE *stream)
vb@89
   114
{
vb@89
   115
    size_t r = 0;
vb@89
   116
vb@89
   117
    do {
vb@89
   118
        clearerr(stream);
vb@89
   119
        size_t n = fread((char *) ptr + r, size, nitems, stream);
vb@89
   120
        nitems -= n;
vb@89
   121
        r += n * size;
vb@89
   122
    } while (!feof(stream) && nitems && ferror(stream) == EINTR);
vb@89
   123
vb@89
   124
    return r;
vb@89
   125
}
vb@89
   126
vb@89
   127
static inline int Fflush(FILE *stream)
vb@89
   128
{
vb@89
   129
    int r;
vb@89
   130
vb@89
   131
    do {
vb@89
   132
        r = fflush(stream);
vb@89
   133
    } while (r == -1 && errno == EINTR);
vb@89
   134
vb@89
   135
    return r;
vb@89
   136
}
vb@68
   137
vb@68
   138
static inline int Mkstemp(char *template)
vb@68
   139
{
vb@68
   140
    int fd;
vb@68
   141
vb@68
   142
    do {
vb@68
   143
        fd = mkstemp(template);
vb@68
   144
    } while (fd == -1 && errno == EINTR);
vb@68
   145
vb@68
   146
    return fd;
vb@68
   147
}
vb@68
   148
vb@68
   149
static inline int Close(int fildes)
vb@68
   150
{
vb@68
   151
    int r;
vb@68
   152
vb@68
   153
    do {
vb@68
   154
        r = close(fildes);
vb@68
   155
    } while (r == -1 && errno == EINTR);
vb@68
   156
vb@68
   157
    return r;
vb@68
   158
}