summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <prabowo@darapsa.org>2022-10-05 20:37:25 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <prabowo@darapsa.org>2022-10-05 20:37:25 +0800
commit74b5ca99a3ae4672e12b3ac4bdbfe369f1f58733 (patch)
tree03f0f5cb0e35154ee05e6d536ea53f2c2153f865
parent10460616dff719c154b2459dc3cf07019231d180 (diff)
No strlen every time response is appended
Also make it almost portable to glibc by using less strlcpy.
-rw-r--r--midtrans.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/midtrans.c b/midtrans.c
index 4943455..1c0d99a 100644
--- a/midtrans.c
+++ b/midtrans.c
@@ -16,12 +16,19 @@ static struct curl_slist *slist;
static pthread_t *threads;
static size_t num_threads = 0;
-static size_t append(char *data, size_t size, size_t nmemb, char **json)
+struct response {
+ size_t size;
+ char *data;
+};
+
+static size_t append(char *data, size_t size, size_t nmemb,
+ struct response *res)
{
size_t realsize = size * nmemb;
- size_t json_len = *json ? strlen(*json) : 0;
- *json = realloc(*json, json_len + realsize + 1);
- strlcpy(&(*json)[json_len], data, realsize + 1);
+ res->data = realloc(res->data, res->size + realsize + 1);
+ strncpy(&(res->data[res->size]), data, realsize);
+ res->size += realsize;
+ res->data[res->size] = '\0';
return realsize;
}
@@ -75,13 +82,13 @@ void midtrans_init(const char *api_key, const char *cainfo)
static void *request(void *arg)
{
- char *json = NULL;
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json);
+ struct response res = { 0, NULL };
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
curl_easy_perform(curl);
#ifdef DEBUG
- printf("%s\n", json);
+ printf("%s\n", res.data);
#endif
- free(json);
+ free(res.data);
return NULL;
}