From a882dbfaa2a878f9fb7c11e361e76e192ef27951 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Fri, 12 Jul 2019 00:54:27 +0200 Subject: Some refactoring --- src/misc.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/misc.c (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c new file mode 100644 index 0000000..c2803a6 --- /dev/null +++ b/src/misc.c @@ -0,0 +1,57 @@ +/* + * misc.c + * (c) 2019, Jonas Gunz, jonasgunz.de + * + * +*/ + +#include "misc.h" + +pid_t fork_run(int _stdin, int _stdout, int _stderr, int argc, char* argv[]) +{ + pid_t pid = fork(); + + if(pid == 0) {//child + dup2 (_stdin, STDIN_FILENO); + dup2 (_stdout, STDOUT_FILENO); + dup2 (_stderr, STDERR_FILENO); + + char* arv[argc + 1]; + + for(int i = 0; i < argc; i++) + arv[i] = argv[i]; + + arv[argc] = NULL; + + execv(argv[0], arv); + + printf("EXEC ERROR %i: %s\r\n", errno, strerror(errno)); + exit(1); + } + else { + return pid; + } +} + + +int try_write(int _fd, char *_buff, int _size, int _retry) +{ + if( _size <= 0) + return 0; + + int written = 0; + int cntr = 0; + + while (written != _size) { + int ret = write ( _fd, _buff + written, _size - written); + + if(ret < 0) + return 1; + else + written += ret; + + if(++cntr > _retry) + return 1; + } + return 0; +} -- cgit v1.2.3