Comments
Patch
@@ -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;
+}
@@ -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