summaryrefslogtreecommitdiffstats
path: root/safe.c
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2021-11-21 18:39:49 +0100
committerpgen <p.gen.progs@gmail.com>2021-11-21 20:43:52 +0100
commitca30d2edaaf9e4e9e146650f560c271f55c0ce32 (patch)
tree78628fb33b08a354aa93caa905a9f7e2c1b27a8e /safe.c
parentbf7fde6db92662927ad8e04bd5e55a5619d5b3a2 (diff)
Add safe wrappers to manage EINTR
Diffstat (limited to 'safe.c')
-rw-r--r--safe.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/safe.c b/safe.c
new file mode 100644
index 0000000..5b3b870
--- /dev/null
+++ b/safe.c
@@ -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;
+}