From 2289e0ef9f3fa1a0df47849f16938d606a6b51b9 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Thu, 24 Mar 2011 10:16:02 +0100 Subject: [PATCH] [lib] Add close_fd() & close_file() These functions are aimed to be used with pthread_cleanup_push(). --- libowlps/libowlps.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ libowlps/owlps.h | 4 ++++ 2 files changed, 49 insertions(+) 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 */