diff options
author | pgen <p.gen.progs@gmail.com> | 2021-11-21 18:39:49 +0100 |
---|---|---|
committer | pgen <p.gen.progs@gmail.com> | 2021-11-21 20:43:52 +0100 |
commit | ca30d2edaaf9e4e9e146650f560c271f55c0ce32 (patch) | |
tree | 78628fb33b08a354aa93caa905a9f7e2c1b27a8e /safe.c | |
parent | bf7fde6db92662927ad8e04bd5e55a5619d5b3a2 (diff) |
Add safe wrappers to manage EINTR
Diffstat (limited to 'safe.c')
-rw-r--r-- | safe.c | 63 |
1 files changed, 63 insertions, 0 deletions
@@ -0,0 +1,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; +} |