commit 4d5c07f297213ba08c70659584a275b97d757bbd
parent f2187f4693a0c48b4470a144a96e413aaf79f702
Author: Paco Esteban <paco@e1e0.net>
Date: Thu, 6 Aug 2020 17:00:33 +0200
better way to create full url
Diffstat:
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/geoloc/geoloc.c b/geoloc/geoloc.c
@@ -1,6 +1,7 @@
#include <sys/socket.h>
#include <arpa/inet.h>
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -9,6 +10,9 @@
#include <curl/curl.h>
#include <json-c/json.h>
+#define BASE_URL "https://ipapi.co"
+#define JSON_ENDPOINT "json"
+
const char *version = "v0.1.0";
struct MemoryStruct {
@@ -60,9 +64,7 @@ main(int argc, char *argv[])
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
- char url[64];
- char *base_url = "https://ipapi.co/";
- char *json_url = "/json";
+ char *url = NULL;
// we get back a json object in key/value pairs, nothing complex
struct json_object *parsed_json;
@@ -120,9 +122,8 @@ main(int argc, char *argv[])
}
// if ip is ok, create the API url we'll call
- strlcpy(url, base_url, sizeof(url));
- strlcat(url, argv[0], sizeof(url));
- strlcat(url, json_url, sizeof(url));
+ if (asprintf(&url, "%s/%s/%s", BASE_URL, argv[0], JSON_ENDPOINT) == -1)
+ err(1, "url creation");
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
@@ -199,6 +200,7 @@ main(int argc, char *argv[])
curl_easy_cleanup(curl_handle);
free(chunk.memory);
+ free(url);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();