summaryrefslogtreecommitdiffstats
path: root/safe.c
blob: 5b3b8708e7736231172c6f79422b62f6a324d09c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ########################################################### */
/* This Software is licensed under the GPL licensed Version 2, */
/* please read http://www.gnu.org/copyleft/gpl.html            */
/* ########################################################### */

/* ************************************ */
/* Some wrappers to manage EINTR errors */
/* ************************************ */

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "safe.h"

FILE *
fopen_safe(const char * restrict stream, const char * restrict mode)
{
  FILE * file;

  while ((file = fopen(stream, mode)) == NULL && errno == EINTR)
    ;

  return file;
}

int
tcsetattr_safe(int fildes, int optional_actions,
               const struct termios * termios_p)
{
  int res;

  while ((res = tcsetattr(fildes, optional_actions, termios_p)) == -1
         && errno == EINTR)
    ;

  return res;
}

int
fputc_safe(int c, FILE * stream)
{
  int res;

  while ((res = fputc(c, stream)) == -1 && errno == EINTR)
    ;

  return res;
}

int
fputs_safe(const char * restrict s, FILE * restrict stream)
{
  int res;

  while ((res = fputs(s, stream)) == -1 && errno == EINTR)
    ;

  return res;
}