sorry, Edouard
authorvb
Mon, 23 Feb 2015 15:39:30 +0100
changeset 683cbe911b2538
parent 67 65cc950ce03d
child 69 c119ffc3065f
sorry, Edouard
src/wrappers.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/wrappers.h	Mon Feb 23 15:39:30 2015 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +#pragma once
     1.5 +
     1.6 +#include <errno.h>
     1.7 +#include <stdarg.h>
     1.8 +#include <stdio.h>
     1.9 +#include <unistd.h>
    1.10 +
    1.11 +static inline FILE * Fopen(const char *filename, const char *mode)
    1.12 +{
    1.13 +    FILE * f;
    1.14 +
    1.15 +    do {
    1.16 +        f = fopen(filename, mode);
    1.17 +    } while (f == NULL && errno == EINTR);
    1.18 +
    1.19 +    return f;
    1.20 +}
    1.21 +
    1.22 +static inline char *Fgets(char * str, int size, FILE * stream)
    1.23 +{
    1.24 +    char * s;
    1.25 +
    1.26 +    do {
    1.27 +        s = fgets(str, size, stream);
    1.28 +    } while (s == NULL && errno == EINTR);
    1.29 +
    1.30 +    return s;
    1.31 +}
    1.32 +
    1.33 +static inline int Fclose(FILE *stream)
    1.34 +{
    1.35 +    int r;
    1.36 +
    1.37 +    do {
    1.38 +        r = fclose(stream);
    1.39 +    } while (r == -1 && errno == EINTR);
    1.40 +
    1.41 +    return r;
    1.42 +}
    1.43 +
    1.44 +static inline FILE * Freopen(
    1.45 +        const char *filename,
    1.46 +        const char *mode,
    1.47 +        FILE * stream
    1.48 +    )
    1.49 +{
    1.50 +    FILE * f;
    1.51 +
    1.52 +    do {
    1.53 +        f = freopen(filename, mode, stream);
    1.54 +    } while (f == NULL && errno == EINTR);
    1.55 +
    1.56 +    return f;
    1.57 +}
    1.58 +
    1.59 +static inline int Fprintf(FILE * stream, const char * format, ...)
    1.60 +{
    1.61 +    int n;
    1.62 +    va_list arglist;
    1.63 +
    1.64 +    va_start(arglist, format);
    1.65 +
    1.66 +    do {
    1.67 +        n = vfprintf(stream, format, arglist);
    1.68 +    } while (n < 0 && errno == EINTR);
    1.69 +
    1.70 +    va_end( arglist );
    1.71 +
    1.72 +    return n;
    1.73 +}
    1.74 +
    1.75 +static inline int Mkstemp(char *template)
    1.76 +{
    1.77 +    int fd;
    1.78 +
    1.79 +    do {
    1.80 +        fd = mkstemp(template);
    1.81 +    } while (fd == -1 && errno == EINTR);
    1.82 +
    1.83 +    return fd;
    1.84 +}
    1.85 +
    1.86 +static inline FILE * Fdopen(int fildes, const char *mode)
    1.87 +{
    1.88 +    FILE * f;
    1.89 +
    1.90 +    do {
    1.91 +        f = fdopen(fildes, mode);
    1.92 +    } while (f == NULL && errno == EINTR);
    1.93 +
    1.94 +    return f;
    1.95 +}
    1.96 +
    1.97 +static inline int Close(int fildes)
    1.98 +{
    1.99 +    int r;
   1.100 +
   1.101 +    do {
   1.102 +        r = close(fildes);
   1.103 +    } while (r == -1 && errno == EINTR);
   1.104 +
   1.105 +    return r;
   1.106 +}
   1.107 +
   1.108 +static inline size_t Fread1(void *ptr, size_t size, FILE *stream)
   1.109 +{
   1.110 +    char *_buf = ptr;
   1.111 +    size_t rest = size;
   1.112 +    size_t bytes_read;
   1.113 +
   1.114 +    for (bytes_read = 0; rest > 0; _buf += rest) {
   1.115 +        clearerr(stream);
   1.116 +
   1.117 +        bytes_read = rest * fread(_buf, rest, 1, stream);
   1.118 +        rest -= bytes_read;
   1.119 +
   1.120 +        if (ferror(stream) != 0 && ferror(stream) != EINTR)
   1.121 +            goto err_file;
   1.122 +
   1.123 +        if (feof(stream))
   1.124 +            goto err_file;
   1.125 +    }
   1.126 +
   1.127 +    return size;
   1.128 +
   1.129 +err_file:
   1.130 +    return size - rest;
   1.131 +}
   1.132 +