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