[lib] Add close_fd() & close_file()

These functions are aimed to be used with pthread_cleanup_push().
This commit is contained in:
Matteo Cypriani 2011-03-24 10:16:02 +01:00
parent ddd4e16b70
commit 2289e0ef9f
2 changed files with 49 additions and 0 deletions

View File

@ -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
}

View File

@ -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 */