aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2020-08-01 21:38:07 +0200
committerJan200101 <sentrycraft123@gmail.com>2020-08-01 21:38:07 +0200
commitc11dd0eae09df4982459a34d764910d0501f2ae2 (patch)
tree940934202f436d67f94728c0704a19ac62f5c299
parent1db300d8815b5f997dd6f992e4ca416537018c2f (diff)
downloadpolecat-c11dd0eae09df4982459a34d764910d0501f2ae2.tar.gz
polecat-c11dd0eae09df4982459a34d764910d0501f2ae2.zip
Improve code structure […]
- create a dedicated "usage" macro to reuse - add various checks in the case no memory is available - fix cwd fetching check - correctly free json_objects to remove all existing leaks
-rw-r--r--src/common.h1
-rw-r--r--src/dxvk.c4
-rw-r--r--src/main.c2
-rw-r--r--src/net.c68
-rw-r--r--src/net.h2
-rw-r--r--src/tar.c8
-rw-r--r--src/wine.c20
7 files changed, 60 insertions, 45 deletions
diff --git a/src/common.h b/src/common.h
index 8b9d95b..54882df 100644
--- a/src/common.h
+++ b/src/common.h
@@ -11,6 +11,7 @@
#define USER_AGENT NAME "/" VERSION
+#define USAGE_STR "Usage: " NAME
struct MemoryStruct {
uint8_t* memory;
diff --git a/src/dxvk.c b/src/dxvk.c
index 817f195..f0ebc86 100644
--- a/src/dxvk.c
+++ b/src/dxvk.c
@@ -62,7 +62,7 @@ int dxvk_install(int argc, char** argv)
}
else
{
- puts("Usage: " NAME " dxvk download <ID>\n\nIDs are obtained via `" NAME " dxvk list' ");
+ puts(USAGE_STR " dxvk download <ID>\n\nIDs are obtained via `" NAME " dxvk list' ");
}
return 0;
}
@@ -90,7 +90,7 @@ int dxvk_list(int argc, char** argv)
int dxvk_help(int argc, char** argv)
{
- puts("usage: " NAME " dxvk <command>\n\nList of commands:");
+ puts(USAGE_STR " dxvk <command>\n\nList of commands:");
print_help(dxvk_commands, ARRAY_LEN(dxvk_commands));
diff --git a/src/main.c b/src/main.c
index a3c1e8e..d56ac28 100644
--- a/src/main.c
+++ b/src/main.c
@@ -50,7 +50,7 @@ int main_info(int argc, char** argv)
int main_help(int argc, char** argv)
{
- puts("usage: " NAME " <command>\n\nList of commands:");
+ puts(USAGE_STR " <command>\n\nList of commands:");
print_help(main_commands, ARRAY_LEN(main_commands));
diff --git a/src/net.c b/src/net.c
index f7dc481..a1c574a 100644
--- a/src/net.c
+++ b/src/net.c
@@ -35,42 +35,45 @@ struct MemoryStruct* downloadToRam(const char* URL)
struct MemoryStruct* chunk = malloc(sizeof(struct MemoryStruct));
- chunk->memory = malloc(1);
- chunk->size = 0;
+ if (chunk)
+ {
+ chunk->memory = malloc(1);
+ chunk->size = 0;
- curl_global_init(CURL_GLOBAL_ALL);
+ curl_global_init(CURL_GLOBAL_ALL);
- curl_handle = curl_easy_init();
+ curl_handle = curl_easy_init();
- curl_easy_setopt(curl_handle, CURLOPT_URL, URL);
- curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, memoryCallback);
- curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)chunk);
- curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, USER_AGENT);
- curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
+ curl_easy_setopt(curl_handle, CURLOPT_URL, URL);
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, memoryCallback);
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)chunk);
+ curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, USER_AGENT);
+ curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
- res = curl_easy_perform(curl_handle);
+ res = curl_easy_perform(curl_handle);
- long http_code = 0;
- curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
+ long http_code = 0;
+ curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
- if(res != CURLE_OK) {
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- return NULL;
- }
- else if (http_code != 200)
- {
- fprintf(stderr, "Server didn't respond as expected [HTTP Error %li]\n", http_code);
- return NULL;
- }
+ if(res != CURLE_OK) {
+ fprintf(stderr, "curl_easy_perform() failed: %s\n",
+ curl_easy_strerror(res));
+ return NULL;
+ }
+ else if (http_code != 200)
+ {
+ fprintf(stderr, "Server didn't respond as expected [HTTP Error %li]\n", http_code);
+ return NULL;
+ }
- curl_easy_cleanup(curl_handle);
- curl_global_cleanup();
+ curl_easy_cleanup(curl_handle);
+ curl_global_cleanup();
+ }
return chunk;
}
-int downloadFile(const char* URL, const char* path)
+void downloadFile(const char* URL, const char* path)
{
struct MemoryStruct* chunk = downloadToRam(URL);
@@ -83,18 +86,21 @@ int downloadFile(const char* URL, const char* path)
free(chunk->memory);
free(chunk);
}
-
- return 0;
}
struct json_object* fetchJSON(const char* URL)
{
struct MemoryStruct* chunk = downloadToRam(URL);
- struct json_object* json = json_tokener_parse((char*)chunk->memory);
+ if (chunk)
+ {
+ struct json_object* json = json_tokener_parse((char*)chunk->memory);
- free(chunk->memory);
- free(chunk);
+ free(chunk->memory);
+ free(chunk);
+
+ return json;
+ }
- return json;
+ return NULL;
} \ No newline at end of file
diff --git a/src/net.h b/src/net.h
index 2b203fa..1ad4f90 100644
--- a/src/net.h
+++ b/src/net.h
@@ -4,7 +4,7 @@
#include <json.h>
size_t WriteMemoryCallback(void*, size_t, size_t, void*);
-int downloadFile(const char*, const char*);
+void downloadFile(const char*, const char*);
struct json_object* fetchJSON(const char*);
#endif \ No newline at end of file
diff --git a/src/tar.c b/src/tar.c
index b2f4ba5..8894676 100644
--- a/src/tar.c
+++ b/src/tar.c
@@ -28,7 +28,7 @@ static int copy_data(struct archive* ar, struct archive* aw)
void extract(const char* filename, const char* outputdir)
{
char cwd[256];
- getcwd(cwd, sizeof(cwd));
+ void* err = getcwd(cwd, sizeof(cwd));
if (chdir(outputdir) < 0)
{
@@ -50,10 +50,12 @@ void extract(const char* filename, const char* outputdir)
a = archive_read_new();
archive_read_support_format_all(a);
archive_read_support_filter_all(a);
+
ext = archive_write_disk_new();
archive_write_disk_set_options(ext, flags);
archive_write_disk_set_standard_lookup(ext);
- if ((r = archive_read_open_filename(a, filename, 10240))) return;
+
+ if ((r = archive_read_open_filename(a, filename, 0x4000))) return;
for (;;)
{
@@ -98,5 +100,5 @@ void extract(const char* filename, const char* outputdir)
archive_write_close(ext);
archive_write_free(ext);
- if (cwd != NULL) chdir(cwd);
+ if (err) chdir(cwd);
}
diff --git a/src/wine.c b/src/wine.c
index fe63644..3f50eee 100644
--- a/src/wine.c
+++ b/src/wine.c
@@ -78,11 +78,13 @@ int wine_download(int argc, char** argv)
extract(downloadpath, datadir);
fprintf(stderr, "Done\n");
}
+
+ json_object_put(runner);
}
}
else
{
- puts("Usage: " NAME " wine download <ID>\n\nIDs are obtained via `" NAME " wine list' ");
+ puts(USAGE_STR " wine download <ID>\n\nIDs are obtained via `" NAME " wine list' ");
}
return 0;
}
@@ -104,6 +106,8 @@ int wine_list(int argc, char** argv)
json_object_object_get_ex(value, "version", &val);
printf(" [%zu]\t%s\n", i, json_object_get_string(val));
}
+
+ json_object_put(runner);
}
return 0;
@@ -129,10 +133,8 @@ int wine_run(int argc, char** argv)
return system(winepath);
}
- else
- {
- fprintf(stderr, "Specify a what wine version to run.\nUse `" NAME " wine list-installed' to list available versions\n");
- }
+
+ fprintf(stderr, "Specify a what wine version to run.\nUse `" NAME " wine list-installed' to list available versions\n");
return 0;
}
@@ -150,7 +152,11 @@ int wine_installed(int argc, char** argv)
{
while ((ent = readdir (dir)) != NULL)
{
- if (ent->d_name[0] != '.' && ent->d_type == 4)
+ /*
+ * WARNING: crusty
+ * d_type is only specified on glibc (including musl) and BSD
+ */
+ if (ent->d_name[0] != '.' && ent->d_type == DT_DIR)
{
fprintf(stderr, " - %s\n", ent->d_name);
}
@@ -163,7 +169,7 @@ int wine_installed(int argc, char** argv)
int wine_help(int argc, char** argv)
{
- puts("usage: " NAME " wine <command>\n\nList of commands:");
+ puts(USAGE_STR " wine <command>\n\nList of commands:");
print_help(wine_commands, ARRAY_LEN(wine_commands));