summaryrefslogtreecommitdiffstats
path: root/safe.c
blob: 5b2213d430eefb226b1bf84337a1e204c8219159 (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
64
65
66
67
/* ################################################################### */
/* Copyright 2015, Pierre Gentile (p.gen.progs@gmail.com)              */
/*                                                                     */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/.           */
/* ################################################################### */

/* ************************************ */
/* 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;
}