aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt43
-rw-r--r--cmake/FindJsonC.cmake78
-rw-r--r--cmake/FindLibarchive.cmake74
-rw-r--r--cmake/FindLibcurl.cmake74
-rw-r--r--src/CMakeLists.txt61
-rw-r--r--src/lutris.c2
-rw-r--r--src/mock/CMakeLists.txt19
-rw-r--r--src/mock/libcurl/libcurl.c93
-rw-r--r--src/net.c20
-rw-r--r--src/wine.c4
10 files changed, 334 insertions, 134 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 145b89f..e6ef9ec 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,51 +1,26 @@
cmake_minimum_required(VERSION 3.0)
+if(CMAKE_POLICY_DEFAULT_CMP0017 OR CMAKE_POLICY_DEFAULT_CMP0020)
+ # touch these to remove warnings
+endif()
+
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()
+option(BUILD_MOCK "build polecat against mock libraries" OFF)
+
project(polecat VERSION 0.1.9 LANGUAGES C)
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
+
add_compile_definitions(NAME="${CMAKE_PROJECT_NAME}")
add_compile_definitions(VERSION="${CMAKE_PROJECT_VERSION}")
message(STATUS "Name: ${CMAKE_PROJECT_NAME}")
message(STATUS "Version: ${CMAKE_PROJECT_VERSION}")
-set(FILES common config dxvk lutris main net tar)
-if(NOT WIN32)
- set(FILES ${FILES} wine)
-endif()
-
-foreach(FILE ${FILES})
- set(SOURCES ${SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/src/${FILE}.c ${CMAKE_CURRENT_SOURCE_DIR}/src/${FILE}.h)
-endforeach()
-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
-find_package(Libcurl REQUIRED)
-find_package(JsonC REQUIRED)
-find_package(Libarchive REQUIRED)
-
-if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
- add_compile_definitions(DEBUG)
-endif()
-
-add_executable(${CMAKE_PROJECT_NAME} ${SOURCES})
-
-target_link_libraries(${CMAKE_PROJECT_NAME} LINK_PUBLIC ${LIBCURL_LIBRARIES})
-target_link_libraries(${CMAKE_PROJECT_NAME} LINK_PUBLIC ${JSONC_LIBRARIES})
-target_link_libraries(${CMAKE_PROJECT_NAME} LINK_PUBLIC ${LIBARCHIVE_LIBRARIES})
-
-target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${LIBCURL_INCLUDE_DIRS})
-target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${JSONC_INCLUDE_DIRS})
-target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${LIBARCHIVE_INCLUDE_DIRS})
-
-if (BUILD_STATIC)
- add_compile_definitions(CURL_STATICLIB)
-endif()
-
-target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Wconversion)
-
-install(TARGETS ${CMAKE_PROJECT_NAME})
+add_subdirectory(src) \ No newline at end of file
diff --git a/cmake/FindJsonC.cmake b/cmake/FindJsonC.cmake
index f3c90df..59cf00a 100644
--- a/cmake/FindJsonC.cmake
+++ b/cmake/FindJsonC.cmake
@@ -1,45 +1,63 @@
#
# JSONC_INCLUDE_DIRS
# JSONC_LIBRARIES
+# JSONC_CFLAGS
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
- pkg_check_modules(_JSONC QUIET json-c)
+ pkg_check_modules(_JSONC json-c)
+
+ if (BUILD_STATIC AND NOT _JSONC_FOUND)
+ message(FATAL_ERROR "Cannot find static build information")
+ endif()
endif()
-if(CMAKE_SIZEOF_VOID_P EQUAL 8)
- set(_lib_suffix 64)
+if (_JSONC_FOUND) # we can rely on pkg-config
+ if (NOT BUILD_STATIC)
+ set(JSONC_LIBRARIES ${_JSONC_LIBRARIES})
+ set(JSONC_INCLUDE_DIRS ${_JSONC_INCLUDE_DIRS})
+ set(JSONC_CFLAGS ${_JSONC_CFLAGS_OTHER})
+ else()
+ set(JSONC_LIBRARIES ${_JSONC_STATIC_LIBRARIES})
+ set(JSONC_INCLUDE_DIRS ${_JSONC_STATIC_INCLUDE_DIRS})
+ set(JSONC_CFLAGS ${_JSONC_STATIC_CFLAGS_OTHER})
+ endif()
else()
- set(_lib_suffix 32)
-endif()
+ if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ set(_lib_suffix 64)
+ else()
+ set(_lib_suffix 32)
+ endif()
-find_path(JSONC_INC
- NAMES json.h
- HINTS
- ENV jsoncPath${_lib_suffix}
- ENV jsoncPath
- ${_JSONC_INCLUDE_DIRS}
- PATHS
- /usr/include/json-c /usr/local/include/json-c)
+ find_path(JSONC_INC
+ NAMES json.h
+ HINTS
+ ENV jsoncPath${_lib_suffix}
+ ENV jsoncPath
+ ${_JSONC_INCLUDE_DIRS}
+ PATHS
+ /usr/include/json-c /usr/local/include/json-c)
-find_library(JSONC_LIB
- NAMES ${_JSONC_LIBRARIES} jsonc json-c
- HINTS
- ENV jsoncPath${_lib_suffix}
- ENV jsoncPath
- ${_JSONC_LIBRARY_DIRS}
- ${_JSONC_STATIC_LIBRARY_DIRS}
- PATHS
- /usr/lib /usr/local/lib)
+ find_library(JSONC_LIB
+ NAMES ${_JSONC_LIBRARIES} jsonc json-c
+ HINTS
+ ENV jsoncPath${_lib_suffix}
+ ENV jsoncPath
+ ${_JSONC_LIBRARY_DIRS}
+ ${_JSONC_STATIC_LIBRARY_DIRS}
+ PATHS
+ /usr/lib{_lib_suffix} /usr/local/lib{_lib_suffix}
+ /usr/lib /usr/local/lib)
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(JsonC DEFAULT_MSG JSONC_LIB JSONC_INC)
-mark_as_advanced(JSONC_INC JSONC_LIB)
+ include(FindPackageHandleStandardArgs)
+ find_package_handle_standard_args(JsonC DEFAULT_MSG JSONC_LIB JSONC_INC)
+ mark_as_advanced(JSONC_INC JSONC_LIB)
-if(JSONC_FOUND)
- set(JSONC_INCLUDE_DIRS ${JSONC_INC})
- set(JSONC_LIBRARIES ${JSONC_LIB})
- if (BUILD_STATIC)
- set(JSONC_LIBRARIES ${JSONC_LIBRARIES} ${_JSONC_STATIC_LIBRARY_DIRS})
+ if(JSONC_FOUND)
+ set(JSONC_INCLUDE_DIRS ${JSONC_INC})
+ set(JSONC_LIBRARIES ${JSONC_LIB})
+ if (BUILD_STATIC)
+ set(JSONC_LIBRARIES ${JSONC_LIBRARIES} ${_JSONC_STATIC_LIBRARIES})
+ endif()
endif()
endif() \ No newline at end of file
diff --git a/cmake/FindLibarchive.cmake b/cmake/FindLibarchive.cmake
index 37ac6bb..e34e991 100644
--- a/cmake/FindLibarchive.cmake
+++ b/cmake/FindLibarchive.cmake
@@ -1,45 +1,59 @@
#
# LIBARCHIVE_INCLUDE_DIRS
# LIBARCHIVE_LIBRARIES
+# LIBARCHIVE_CFLAGS
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
- pkg_check_modules(_LIBARCHIVE QUIET libarchive)
+ pkg_check_modules(_LIBARCHIVE libarchive)
+
+ if (BUILD_STATIC AND NOT _LIBARCHIVE_FOUND)
+ message(FATAL_ERROR "Cannot find static build information")
+ endif()
endif()
-if(CMAKE_SIZEOF_VOID_P EQUAL 8)
- set(_lib_suffix 64)
+if (_LIBARCHIVE_FOUND) # we can rely on pkg-config
+ if (NOT BUILD_STATIC)
+ set(LIBARCHIVE_LIBRARIES ${_LIBARCHIVE_LIBRARIES})
+ set(LIBARCHIVE_INCLUDE_DIRS ${_LIBARCHIVE_INCLUDE_DIRS})
+ set(LIBARCHIVE_CFLAGS ${_LIBARCHIVE_CFLAGS_OTHER})
+ else()
+ set(LIBARCHIVE_LIBRARIES ${_LIBARCHIVE_STATIC_LIBRARIES})
+ set(LIBARCHIVE_INCLUDE_DIRS ${_LIBARCHIVE_STATIC_INCLUDE_DIRS})
+ set(LIBARCHIVE_CFLAGS ${_LIBARCHIVE_STATIC_CFLAGS_OTHER})
+ endif()
else()
- set(_lib_suffix 32)
-endif()
+ if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ set(_lib_suffix 64)
+ else()
+ set(_lib_suffix 32)
+ endif()
-find_path(LIBARCHIVE_INC
- NAMES archive.h archive_entry.h
- HINTS
- ENV libarchivePath${_lib_suffix}
- ENV libarchivePath
- ${_LIBARCHIVE_INCLUDE_DIRS}
- PATHS
- /usr/include /usr/local/include)
+ find_path(LIBARCHIVE_INC
+ NAMES archive.h archive_entry.h
+ HINTS
+ ENV libarchivePath${_lib_suffix}
+ ENV libarchivePath
+ ${_LIBARCHIVE_INCLUDE_DIRS}
+ PATHS
+ /usr/include /usr/local/include)
-find_library(LIBARCHIVE_LIB
- NAMES ${_LIBARCHIVE_LIBRARIES} archive
- HINTS
- ENV libarchivePath${_lib_suffix}
- ENV libarchivePath
- ${_LIBARCHIVE_LIBRARY_DIRS}
- ${_LIBARCHIVE_STATIC_LIBRARY_DIRS}
- PATHS
- /usr/lib /usr/local/lib)
+ find_library(LIBARCHIVE_LIB
+ NAMES archive
+ HINTS
+ ENV libarchivePath${_lib_suffix}
+ ENV libarchivePath
+ PATHS
+ /usr/lib{_lib_suffix} /usr/local/lib{_lib_suffix}
+ /usr/lib /usr/local/lib)
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(Libarchive DEFAULT_MSG LIBARCHIVE_LIB LIBARCHIVE_INC)
-mark_as_advanced(LIBARCHIVE_INC LIBARCHIVE_LIB)
+ include(FindPackageHandleStandardArgs)
+ find_package_handle_standard_args(Libarchive DEFAULT_MSG LIBARCHIVE_LIB LIBARCHIVE_INC)
+ mark_as_advanced(LIBARCHIVE_INC LIBARCHIVE_LIB)
-if(LIBARCHIVE_FOUND)
- set(LIBARCHIVE_INCLUDE_DIRS ${LIBARCHIVE_INC})
- set(LIBARCHIVE_LIBRARIES ${LIBARCHIVE_LIB})
- if (BUILD_STATIC)
- set(LIBARCHIVE_LIBRARIES ${LIBARCHIVE_LIBRARIES} ${_LIBARCHIVE_STATIC_LIBRARIES})
+ if(LIBARCHIVE_FOUND)
+ set(LIBARCHIVE_INCLUDE_DIRS ${LIBARCHIVE_INC})
+ set(LIBARCHIVE_LIBRARIES ${LIBARCHIVE_LIB})
+ set(LIBARCHIVE_CFLAGS "")
endif()
endif() \ No newline at end of file
diff --git a/cmake/FindLibcurl.cmake b/cmake/FindLibcurl.cmake
index fbe6505..aa89105 100644
--- a/cmake/FindLibcurl.cmake
+++ b/cmake/FindLibcurl.cmake
@@ -1,46 +1,58 @@
#
# LIBCURL_INCLUDE_DIRS
# LIBCURL_LIBRARIES
+# LIBCURL_CFLAGS
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
- pkg_check_modules(_CURL REQUIRED QUIET libcurl)
+ pkg_check_modules(_CURL libcurl)
+
+ if (BUILD_STATIC AND NOT _CURL_FOUND)
+ message(FATAL_ERROR "Cannot find static build information")
+ endif()
endif()
-if(CMAKE_SIZEOF_VOID_P EQUAL 8)
- set(_lib_suffix 64)
+if (_CURL_FOUND) # we can rely on pkg-config
+ if (NOT BUILD_STATIC)
+ set(LIBCURL_LIBRARIES ${_CURL_LIBRARIES})
+ set(LIBCURL_INCLUDE_DIRS ${_CURL_INCLUDE_DIRS})
+ set(LIBCURL_CFLAGS ${_CURL_CFLAGS_OTHER})
+ else()
+ set(LIBCURL_LIBRARIES ${_CURL_STATIC_LIBRARIES})
+ set(LIBCURL_INCLUDE_DIRS ${_CURL_STATIC_INCLUDE_DIRS})
+ set(LIBCURL_CFLAGS ${_CURL_STATIC_CFLAGS_OTHER})
+ endif()
else()
- set(_lib_suffix 32)
-endif()
+ if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ set(_lib_suffix 64)
+ else()
+ set(_lib_suffix 32)
+ endif()
-find_path(CURL_INC
- NAMES curl/curl.h
- HINTS
- ENV curlPath${_lib_suffix}
- ENV curlPath
- ${_CURL_INCLUDE_DIRS}
- PATHS
- /usr/include /usr/local/include)
+ find_path(CURL_INC
+ NAMES curl/curl.h
+ HINTS
+ ENV curlPath${_lib_suffix}
+ ENV curlPath
+ PATHS
+ /usr/include /usr/local/include)
-find_library(CURL_LIB
- NAMES ${_CURL_LIBRARIES} curl libcurl
- HINTS
- ENV curlPath${_lib_suffix}
- ENV curlPath
- ${_CURL_LIBRARY_DIRS}
- ${_CURL_STATIC_LIBRARY_DIRS}
- PATHS
- /usr/lib /usr/local/lib)
+ find_library(CURL_LIB
+ NAMES curl
+ HINTS
+ ENV curlPath${_lib_suffix}
+ ENV curlPath
+ PATHS
+ /usr/lib{_lib_suffix} /usr/local/lib{_lib_suffix}
+ /usr/lib /usr/local/lib)
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(Libcurl DEFAULT_MSG CURL_LIB CURL_INC)
-mark_as_advanced(CURL_INC CURL_LIB)
+ include(FindPackageHandleStandardArgs)
+ find_package_handle_standard_args(Libcurl DEFAULT_MSG CURL_LIB CURL_INC)
+ mark_as_advanced(CURL_INC CURL_LIB)
-if(LIBCURL_FOUND)
- set(LIBCURL_INCLUDE_DIRS ${CURL_INC})
- set(LIBCURL_LIBRARIES ${CURL_LIB})
- if (BUILD_STATIC)
- set(LIBCURL_LIBRARIES ${LIBCURL_LIBRARIES} ${_CURL_STATIC_LIBRARIES})
- add_compile_definitions(CURL_STATICLIB)
+ if(LIBCURL_FOUND)
+ set(LIBCURL_INCLUDE_DIRS ${CURL_INC})
+ set(LIBCURL_LIBRARIES ${CURL_LIB})
+ set(LIBCURL_CFLAGS "")
endif()
endif() \ No newline at end of file
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..16d1b5e
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,61 @@
+set(C_STANDARD 99)
+
+set(SOURCES
+ ${CMAKE_CURRENT_SOURCE_DIR}/command.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/common.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/common.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/config.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/config.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/dxvk.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/dxvk.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/lutris.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/lutris.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/main.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/main.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/net.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/net.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/tar.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/tar.h
+)
+
+
+if (NOT WIN32)
+set(SOURCES
+ ${SOURCES}
+ ${CMAKE_CURRENT_SOURCE_DIR}/wine.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/wine.h
+)
+endif()
+
+find_package(Libcurl REQUIRED)
+find_package(JsonC REQUIRED)
+find_package(Libarchive REQUIRED)
+
+add_executable(${CMAKE_PROJECT_NAME} ${SOURCES})
+
+target_link_libraries(${CMAKE_PROJECT_NAME} LINK_PUBLIC ${JSONC_LIBRARIES})
+target_link_libraries(${CMAKE_PROJECT_NAME} LINK_PUBLIC ${LIBARCHIVE_LIBRARIES})
+target_link_libraries(${CMAKE_PROJECT_NAME} LINK_PUBLIC ${LIBCURL_LIBRARIES})
+
+target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${JSONC_INCLUDE_DIRS})
+target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${LIBARCHIVE_INCLUDE_DIRS})
+target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${LIBCURL_INCLUDE_DIRS})
+
+set(CFLAGS
+ -Wall -Wextra -pedantic
+ -Wconversion -Wshadow -Wstrict-aliasing
+ -Winit-self -Wcast-align -Wpointer-arith
+ -Wmissing-declarations -Wmissing-include-dirs
+ -Wno-unused-parameter -Wuninitialized
+ ${JSONC_CFLAGS}
+ ${LIBARCHIVE_CFLAGS}
+ ${LIBCURL_CFLAGS}
+)
+
+target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC ${CFLAGS})
+
+install(TARGETS ${CMAKE_PROJECT_NAME})
+
+if(BUILD_MOCK)
+ add_subdirectory(mock)
+endif() \ No newline at end of file
diff --git a/src/lutris.c b/src/lutris.c
index 0dd999b..b7c4da6 100644
--- a/src/lutris.c
+++ b/src/lutris.c
@@ -20,7 +20,7 @@ static const struct Flag lutris_flags[] = {
{ .name = "help", .variant = TWO, .func = lutris_help, .description = "show this message"}
};
-char* getpwd()
+static char* getpwd()
{
char* pwd = malloc(255);
pwd = getcwd(pwd, 255);
diff --git a/src/mock/CMakeLists.txt b/src/mock/CMakeLists.txt
new file mode 100644
index 0000000..c2634d5
--- /dev/null
+++ b/src/mock/CMakeLists.txt
@@ -0,0 +1,19 @@
+SET(CURL_SOURCES
+ ${CMAKE_CURRENT_SOURCE_DIR}/libcurl/libcurl.c
+)
+
+add_library(mockcurl STATIC ${CURL_SOURCES})
+target_include_directories(mockcurl PUBLIC ${LIBCURL_INCLUDE_DIRS})
+
+set(NAME ${CMAKE_PROJECT_NAME}_mock)
+add_executable(${NAME} ${SOURCES})
+
+target_link_libraries(${NAME} LINK_PUBLIC ${JSONC_LIBRARIES})
+target_link_libraries(${NAME} LINK_PUBLIC ${LIBARCHIVE_LIBRARIES})
+target_link_libraries(${NAME} LINK_PUBLIC mockcurl)
+
+target_include_directories(${NAME} PUBLIC ${JSONC_INCLUDE_DIRS})
+target_include_directories(${NAME} PUBLIC ${LIBARCHIVE_INCLUDE_DIRS})
+target_include_directories(${NAME} PUBLIC ${LIBCURL_INCLUDE_DIRS})
+
+target_compile_options(${NAME} PUBLIC ${CFLAGS}) \ No newline at end of file
diff --git a/src/mock/libcurl/libcurl.c b/src/mock/libcurl/libcurl.c
new file mode 100644
index 0000000..b4f332c
--- /dev/null
+++ b/src/mock/libcurl/libcurl.c
@@ -0,0 +1,93 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <curl/curl.h>
+
+char* url = NULL;
+void* data = NULL;
+
+#ifdef curl_easy_setopt
+#undef curl_easy_setopt
+#endif
+
+CURLcode curl_global_init(long flags)
+{
+ puts("[MOCK] curl_global_init(...)");
+ return CURLE_OK;
+}
+
+CURL* curl_easy_init()
+{
+ puts("[MOCK] curl_easy_init(...)");
+ return NULL;
+}
+
+CURLcode curl_easy_setopt(CURL *handle, CURLoption option, ...)
+{
+ puts("[MOCK] curl_easy_setopt(...)");
+
+ va_list arg;
+ va_start(arg, option);
+
+ switch (option)
+ {
+ case CURLOPT_URL:
+ url = va_arg(arg, char*);
+ printf("CURLOPT_URL\t%s\n", url);
+ break;
+
+ case CURLOPT_WRITEDATA:
+ data = va_arg(arg, void*);
+ printf("CURLOPT_WRITEDATA\t%p\n", data);
+ break;
+
+ default:
+ break;
+ }
+
+ va_end(arg);
+
+ return CURLE_OK;
+}
+
+CURLcode curl_easy_perform(CURL *easy_handle)
+{
+ puts("[MOCK] curl_easy_perform(...)");
+ return CURLE_OK;
+}
+
+#ifdef curl_easy_getinfo
+#undef curl_easy_getinfo
+#endif
+
+CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
+{
+ puts("[MOCK] curl_easy_getinfo(...)");
+
+ if (info == CURLINFO_RESPONSE_CODE)
+ {
+ va_list arg;
+ va_start(arg, info);
+ long* http_code = va_arg(arg, long*);
+ *http_code = 200;
+ va_end(arg);
+ }
+
+
+ return CURLE_OK;
+}
+
+const char* curl_easy_strerror(CURLcode error)
+{
+ puts("[MOCK] curl_easy_strerror(...)");
+ return "error";
+}
+
+void curl_easy_cleanup(CURL *handle)
+{
+ puts("[MOCK] curl_easy_cleanup(...)");
+}
+
+void curl_global_cleanup()
+{
+ puts("[MOCK] curl_global_cleanup(...)");
+} \ No newline at end of file
diff --git a/src/net.c b/src/net.c
index 2e3319d..bf03a07 100644
--- a/src/net.c
+++ b/src/net.c
@@ -54,8 +54,13 @@ struct MemoryStruct* downloadToRam(const char* URL, int progress)
if (chunk)
{
- // if we managed to allocate the chunk lets assume we can allocate at least 1 byte
chunk->memory = malloc(1);
+ if (!chunk->memory)
+ {
+ free(chunk);
+ return NULL;
+ }
+ chunk->memory[0] = 0;
chunk->size = 0;
curl_global_init(CURL_GLOBAL_ALL);
@@ -73,18 +78,22 @@ struct MemoryStruct* downloadToRam(const char* URL, int progress)
res = curl_easy_perform(curl_handle);
long http_code = 0;
- curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
+ curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
if(res != CURLE_OK) {
+ free(chunk->memory);
+ free(chunk);
puts(curl_easy_strerror(res));
- return NULL;
+ chunk = NULL;
}
else if (http_code != 200)
{
+ free(chunk->memory);
+ free(chunk);
#ifdef DEBUG
printf("HTTP Error %li\n", http_code);
#endif
- return NULL;
+ chunk = NULL;
}
curl_easy_cleanup(curl_handle);
@@ -119,9 +128,8 @@ void downloadToFile(const char* URL, const char* path, int progress)
struct json_object* fetchJSON(const char* URL)
{
- struct MemoryStruct* chunk = downloadToRam(URL, 0);
-
struct json_object* json = NULL;
+ struct MemoryStruct* chunk = downloadToRam(URL, 0);
if (chunk)
{
diff --git a/src/wine.c b/src/wine.c
index 88819b4..65cbd34 100644
--- a/src/wine.c
+++ b/src/wine.c
@@ -48,9 +48,9 @@ COMMAND(wine, download)
char* choice = argv[i];
- for (JSON_LENGTH_TYPE i = 0; i < json_object_array_length(versions); ++i)
+ for (JSON_LENGTH_TYPE j = 0; j < json_object_array_length(versions); ++j)
{
- value = json_object_array_get_idx(versions, i);
+ value = json_object_array_get_idx(versions, j);
json_object_object_get_ex(value, "version", &temp);
if (strcmp(json_object_get_string(temp), choice) == 0)
{