From patchwork Fri Sep 13 20:31:55 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [05, of, 11, c-hglib:level0, V2] hg_close: close the connection with hg cmd server for the given handle From: Iulian Stana X-Patchwork-Id: 2435 Message-Id: <0296ca1fc1214ad6dd81.1379104315@doppler> To: mercurial-devel@selenic.com Date: Fri, 13 Sep 2013 23:31:55 +0300 # HG changeset patch # User Iulian Stana # Date 1379103340 -10800 # Fri Sep 13 23:15:40 2013 +0300 # Node ID 0296ca1fc1214ad6dd813613ae8280d0ffc96b00 # Parent 07360f1314434cf1f9c514ecedbc916ab2958c6d hg_close: close the connection with hg cmd server for the given handle The Handle paramater it's pass by address because I want to set the handle pointer to NULL. It's a defence action. In this way I can test if a handle is set or not. diff --git a/client.c b/client.c --- a/client.c +++ b/client.c @@ -11,6 +11,14 @@ #define HGPATH "hg" +/* Release data from handle pointers. */ +void free_data(hg_handle *handle){ + if(handle->out_data){ + free(handle->out_data); + handle->out_data = NULL; + handle->out_data_size = 0; + } +} /* * The function will read the header from the command server and will save it to @@ -163,3 +171,24 @@ handle->out_data_size = 0; return handle; } + +/* + * Close the connection for the given handle. + * */ +int hg_close(hg_handle **handle) +{ + if(!(*handle)) { + errno = EINVAL; + return -1; + } + + close((*handle)->p_read); + close((*handle)->p_write); + + free((*handle)->header); + free_data(*handle); + free(*handle); + *handle = NULL; + + return 0; +} diff --git a/client.h b/client.h --- a/client.h +++ b/client.h @@ -124,4 +124,18 @@ * */ hg_handle *hg_open(const char *path, char *encoding); +/** + * \brief Close the connection for the given handle. + * + * Erase the handle and free the memory + * \param handle The handle of the connection that I want to close + * \retval 0 if successful + * \retval -1 to indicate an error, with errno set appropriately. + * + * errno can be: + * - EINVAL - Invalid argument ( handle it's set to a null pointer) + * - kill(2) command errors + * */ +int hg_close(hg_handle **handle); + #endif