diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2019-07-12 00:54:27 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2019-07-12 00:54:27 +0200 |
commit | a882dbfaa2a878f9fb7c11e361e76e192ef27951 (patch) | |
tree | 02c6f98dd687c9bd3862ed3a6760163e17758431 /src/misc.c | |
parent | 78d3bb2eff827029e9f9ee01f7548f397332943f (diff) | |
download | bbs-a882dbfaa2a878f9fb7c11e361e76e192ef27951.tar.gz |
Some refactoring
Diffstat (limited to 'src/misc.c')
-rw-r--r-- | src/misc.c | 57 |
1 files changed, 57 insertions, 0 deletions
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 + * <Description> + * <License> +*/ + +#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; +} |