diff --git a/libowlps/libowlps.c b/libowlps/libowlps.c index d9a2927..586cb6a 100644 --- a/libowlps/libowlps.c +++ b/libowlps/libowlps.c @@ -444,3 +444,48 @@ void owl_sigterm_handler(int num) owl_sigint_handler(SIGINT) ; } + + + +/* + * Closes the file descriptor 'fd'. + * 'fd' must be passed as an int pointer (int*). + */ +void owl_close_fd(void *fd) +{ + if (fd == NULL) + return ; + + int *file_desc = fd ; + if (close(*file_desc) != 0) + perror("Error closing file descriptor") ; +#ifdef DEBUG + else + fprintf(stderr, "File descriptor %d closed successfully.\n", + *file_desc) ; +#endif // DEBUG +} + + + +/* + * Closes the stream 'file'. + * 'file' must be passed as a pointer on a pointer of FILE (FILE**). + * If *file either stdout, stderr or stdin, it will not be closed. + */ +void owl_close_file(void *file) +{ + if (file == NULL) + return ; + + FILE **stream = file ; + if (*stream == stdout || *stream == stderr || *stream == stdin) + return ; + + if (fclose(*stream) != 0) + perror("Error closing stream") ; +#ifdef DEBUG + else + fprintf(stderr, "Stream closed successfully.\n") ; +#endif // DEBUG +} diff --git a/libowlps/owlps.h b/libowlps/owlps.h index 8952d89..f5b3fa1 100644 --- a/libowlps/owlps.h +++ b/libowlps/owlps.h @@ -271,6 +271,10 @@ int owl_iface_channel_hop(char *iface) ; void owl_sigint_handler(int num) ; void owl_sigterm_handler(int num) ; +// Threads +void owl_close_fd(void *fd) ; +void owl_close_file(void *file) ; + /* Macros */