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