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