diff options
author | Michael Vetter <jubalh@iodoru.org> | 2021-03-30 17:38:13 +0200 |
---|---|---|
committer | Michael Vetter <jubalh@iodoru.org> | 2021-03-30 17:38:13 +0200 |
commit | 1ec606540eb0f474f3d968d3566a7c56d778a367 (patch) | |
tree | bfe33ebccfc5f2e020a2bcf4908edbbfcc5791f4 /src | |
parent | 3c1e4bac3ae640bc4c451d7d4a1c06146e13a15a (diff) | |
download | profani-tty-1ec606540eb0f474f3d968d3566a7c56d778a367.tar.gz |
Get rid of asprintf and _GNU_SOURCE define
_GNU_SOURCE was even in some files where it was not needed at all (http*). Let's replace asprintf() with g_strdup_printf().
Diffstat (limited to 'src')
-rw-r--r-- | src/command/cmd_ac.c | 20 | ||||
-rw-r--r-- | src/command/cmd_defs.c | 16 | ||||
-rw-r--r-- | src/command/cmd_funcs.c | 8 | ||||
-rw-r--r-- | src/common.c | 7 | ||||
-rw-r--r-- | src/database.c | 41 | ||||
-rw-r--r-- | src/tools/aesgcm_download.c | 2 | ||||
-rw-r--r-- | src/tools/autocomplete.c | 7 | ||||
-rw-r--r-- | src/tools/http_common.c | 2 | ||||
-rw-r--r-- | src/tools/http_download.c | 2 | ||||
-rw-r--r-- | src/tools/http_upload.c | 87 | ||||
-rw-r--r-- | src/ui/mucwin.c | 1 | ||||
-rw-r--r-- | src/xmpp/stanza.c | 8 |
12 files changed, 97 insertions, 104 deletions
diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index cd684f19..f74ca847 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -36,8 +36,6 @@ #include "config.h" -#define _GNU_SOURCE 1 - #include <stdlib.h> #include <string.h> #include <assert.h> @@ -1558,13 +1556,15 @@ cmd_ac_complete_filepath(const char* const input, char* const startstr, gboolean // expand ~ to $HOME if (inpcp[0] == '~' && inpcp[1] == '/') { - if (asprintf(&tmp, "%s/%sfoo", getenv("HOME"), inpcp + 2) == -1) { + tmp = g_strdup_printf("%s/%sfoo", getenv("HOME"), inpcp + 2); + if (!tmp) { free(inpcp); return NULL; } output_off = strlen(getenv("HOME")) + 1; } else { - if (asprintf(&tmp, "%sfoo", inpcp) == -1) { + tmp = g_strdup_printf("%sfoo", inpcp); + if (!tmp) { free(inpcp); return NULL; } @@ -1596,25 +1596,29 @@ cmd_ac_complete_filepath(const char* const input, char* const startstr, gboolean char* acstring; if (output_off) { - if (asprintf(&tmp, "%s/%s", directory, dir->d_name) == -1) { + tmp = g_strdup_printf("%s/%s", directory, dir->d_name); + if(!tmp) { free(directory); free(foofile); return NULL; } - if (asprintf(&acstring, "~/%s", tmp + output_off) == -1) { + acstring = g_strdup_printf("~/%s", tmp + output_off); + if (!acstring) { free(directory); free(foofile); return NULL; } free(tmp); } else if (strcmp(directory, "/") == 0) { - if (asprintf(&acstring, "/%s", dir->d_name) == -1) { + acstring = g_strdup_printf("/%s", dir->d_name); + if (!acstring) { free(directory); free(foofile); return NULL; } } else { - if (asprintf(&acstring, "%s/%s", directory, dir->d_name) == -1) { + acstring = g_strdup_printf("%s/%s", directory, dir->d_name); + if (!acstring) { free(directory); free(foofile); return NULL; diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index c756c169..730169ba 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -34,8 +34,6 @@ * */ -#define _GNU_SOURCE 1 - #include "config.h" #include <assert.h> @@ -2894,11 +2892,11 @@ command_mangen(void) mkdir_recursive("docs"); - char* header = NULL; GDateTime* now = g_date_time_new_now_local(); gchar* date = g_date_time_format(now, "%F"); - if (asprintf(&header, ".TH man 1 \"%s\" \"" PACKAGE_VERSION "\" \"Profanity XMPP client\"\n", date) == -1) { - // TODO: error + gchar *header = g_strdup_printf(".TH man 1 \"%s\" \"" PACKAGE_VERSION "\" \"Profanity XMPP client\"\n", date); + if (!header) { + log_error("command_mangen(): could not allocate memory"); return; } g_date_time_unref(now); @@ -2908,9 +2906,9 @@ command_mangen(void) while (curr) { Command* pcmd = curr->data; - char* filename = NULL; - if (asprintf(&filename, "docs/profanity-%s.1", &pcmd->cmd[1]) == -1) { - // TODO: error + gchar* filename = g_strdup_printf("docs/profanity-%s.1", &pcmd->cmd[1]); + if (!filename) { + log_error("command_mangen(): could not allocate memory"); return; } FILE* manpage = fopen(filename, "w"); @@ -2955,6 +2953,6 @@ command_mangen(void) printf("\nProcessed %d commands.\n\n", g_list_length(cmds)); - free(header); + g_free(header); g_list_free(cmds); } diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 115db27f..8f463a6c 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -35,8 +35,6 @@ * */ -#define _GNU_SOURCE 1 - #include "config.h" #include <string.h> @@ -141,10 +139,10 @@ cmd_process_input(ProfWin* window, char* inp) char* question_mark = strchr(command, '?'); if (question_mark) { *question_mark = '\0'; - char* fakeinp; - if (asprintf(&fakeinp, "/help %s", command + 1)) { + gchar* fakeinp = g_strdup_printf("/help %s", command + 1); + if (fakeinp) { result = _cmd_execute(window, "/help", fakeinp); - free(fakeinp); + g_free(fakeinp); } } else { result = _cmd_execute(window, command, inp); diff --git a/src/common.c b/src/common.c index 6defa7c1..eca444e6 100644 --- a/src/common.c +++ b/src/common.c @@ -34,8 +34,6 @@ * */ -#define _GNU_SOURCE 1 - #include "config.h" #include <errno.h> @@ -529,13 +527,14 @@ _unique_filename(const char* filename) unsigned int i = 0; while (g_file_test(unique, G_FILE_TEST_EXISTS)) { - free(unique); + g_free(unique); if (i > 1000) { // Give up after 1000 attempts. return NULL; } - if (asprintf(&unique, "%s.%u", filename, i) < 0) { + unique = g_strdup_printf("%s.%u", filename, i); + if (!unique) { return NULL; } diff --git a/src/database.c b/src/database.c index 7c4dc12d..5a213d7d 100644 --- a/src/database.c +++ b/src/database.c @@ -35,8 +35,6 @@ #include "config.h" -#define _GNU_SOURCE 1 - #include <sys/stat.h> #include <sqlite3.h> #include <glib.h> @@ -213,13 +211,14 @@ GSList* log_database_get_previous_chat(const gchar* const contact_barejid) { sqlite3_stmt* stmt = NULL; - char* query; + gchar* query; const char* jid = connection_get_fulljid(); Jid* myjid = jid_create(jid); if (!myjid) return NULL; - if (asprintf(&query, "SELECT * FROM (SELECT `message`, `timestamp`, `from_jid`, `type` from `ChatLogs` WHERE (`from_jid` = '%s' AND `to_jid` = '%s') OR (`from_jid` = '%s' AND `to_jid` = '%s') ORDER BY `timestamp` DESC LIMIT 10) ORDER BY `timestamp` ASC;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid) == -1) { + query = g_strdup_printf("SELECT * FROM (SELECT `message`, `timestamp`, `from_jid`, `type` from `ChatLogs` WHERE (`from_jid` = '%s' AND `to_jid` = '%s') OR (`from_jid` = '%s' AND `to_jid` = '%s') ORDER BY `timestamp` DESC LIMIT 10) ORDER BY `timestamp` ASC;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid); + if (!query) { log_error("log_database_get_previous_chat(): SQL query. could not allocate memory"); return NULL; } @@ -251,7 +250,7 @@ log_database_get_previous_chat(const gchar* const contact_barejid) history = g_slist_append(history, msg); } sqlite3_finalize(stmt); - free(query); + g_free(query); return history; } @@ -314,7 +313,7 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji } char* err_msg; - char* query; + gchar* query; gchar* date_fmt; if (message->timestamp) { @@ -331,20 +330,20 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji char* escaped_message = str_replace(message->plain, "'", "''"); - if (asprintf(&query, "INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `archive_id`, `replace_id`, `type`, `encryption`) SELECT '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' WHERE NOT EXISTS (SELECT 1 FROM `ChatLogs` WHERE `archive_id` = '%s')", - from_jid->barejid, - from_jid->resourcepart ? from_jid->resourcepart : "", - to_jid->barejid, - to_jid->resourcepart ? to_jid->resourcepart : "", - escaped_message ? escaped_message : "", - date_fmt ? date_fmt : "", - message->id ? message->id : "", - message->stanzaid ? message->stanzaid : "", - message->replace_id ? message->replace_id : "", - type ? type : "", - enc ? enc : "", - message->stanzaid ? message->stanzaid : "") - == -1) { + query = g_strdup_printf("INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `archive_id`, `replace_id`, `type`, `encryption`) SELECT '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' WHERE NOT EXISTS (SELECT 1 FROM `ChatLogs` WHERE `archive_id` = '%s')", + from_jid->barejid, + from_jid->resourcepart ? from_jid->resourcepart : "", + to_jid->barejid, + to_jid->resourcepart ? to_jid->resourcepart : "", + escaped_message ? escaped_message : "", + date_fmt ? date_fmt : "", + message->id ? message->id : "", + message->stanzaid ? message->stanzaid : "", + message->replace_id ? message->replace_id : "", + type ? type : "", + enc ? enc : "", + message->stanzaid ? message->stanzaid : ""); + if (!query) { log_error("log_database_add(): SQL query. could not allocate memory"); return; } @@ -359,5 +358,5 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji log_error("Unknown SQLite error"); } } - free(query); + g_free(query); } diff --git a/src/tools/aesgcm_download.c b/src/tools/aesgcm_download.c index 96f8d7e8..864da6b7 100644 --- a/src/tools/aesgcm_download.c +++ b/src/tools/aesgcm_download.c @@ -34,8 +34,6 @@ * */ -#define _GNU_SOURCE 1 - #include "config.h" #include <stdlib.h> diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c index 9a809984..e1efdba9 100644 --- a/src/tools/autocomplete.c +++ b/src/tools/autocomplete.c @@ -35,7 +35,6 @@ #include "config.h" -#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -315,11 +314,13 @@ _autocomplete_param_common(const char* const input, char* command, autocomplete_ char* result = NULL; int len; - len = asprintf(&command_cpy, "%s ", command); - if (len == -1) { + command_cpy = g_strdup_printf("%s ", command); + if (!command_cpy) { return NULL; } + len = strlen(command_cpy); + if (strncmp(input, command_cpy, len) == 0) { int inp_len = strlen(input); char prefix[inp_len]; diff --git a/src/tools/http_common.c b/src/tools/http_common.c index cd24c57e..faad257f 100644 --- a/src/tools/http_common.c +++ b/src/tools/http_common.c @@ -35,8 +35,6 @@ #include "config.h" -#define _GNU_SOURCE 1 - #include <stdlib.h> #include <stdio.h> #include <string.h> diff --git a/src/tools/http_download.c b/src/tools/http_download.c index 2ff49b61..d4df5f6b 100644 --- a/src/tools/http_download.c +++ b/src/tools/http_download.c @@ -34,8 +34,6 @@ * */ -#define _GNU_SOURCE 1 - #include "config.h" #include <stdlib.h> diff --git a/src/tools/http_upload.c b/src/tools/http_upload.c index c30b74fa..17eca188 100644 --- a/src/tools/http_upload.c +++ b/src/tools/http_upload.c @@ -33,8 +33,6 @@ * */ -#define _GNU_SOURCE 1 - #include "config.h" #include <stdlib.h> @@ -92,12 +90,12 @@ _xferinfo(void* userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultot ulperc = (100 * ulnow) / ultotal; } - char* msg; - if (asprintf(&msg, "Uploading '%s': %d%%", upload->filename, ulperc) == -1) { - msg = strdup(FALLBACK_MSG); + gchar* msg = g_strdup_printf("Uploading '%s': %d%%", upload->filename, ulperc); + if (!msg) { + msg = g_strdup(FALLBACK_MSG); } win_update_entry_message(upload->window, upload->put_url, msg); - free(msg); + g_free(msg); pthread_mutex_unlock(&lock); @@ -165,11 +163,11 @@ http_file_put(void* userdata) FILE* fh = NULL; char* err = NULL; - char* content_type_header; + gchar* content_type_header; // Optional headers - char* auth_header = NULL; - char* cookie_header = NULL; - char* expires_header = NULL; + gchar* auth_header = NULL; + gchar* cookie_header = NULL; + gchar* expires_header = NULL; CURL* curl; CURLcode res; @@ -178,12 +176,12 @@ http_file_put(void* userdata) upload->bytes_sent = 0; pthread_mutex_lock(&lock); - char* msg; - if (asprintf(&msg, "Uploading '%s': 0%%", upload->filename) == -1) { - msg = strdup(FALLBACK_MSG); + gchar* msg = g_strdup_printf("Uploading '%s': 0%%", upload->filename); + if (!msg) { + msg = g_strdup(FALLBACK_MSG); } win_print_http_transfer(upload->window, msg, upload->put_url); - free(msg); + g_free(msg); char* cert_path = prefs_get_string(PREF_TLS_CERTPATH); pthread_mutex_unlock(&lock); @@ -195,28 +193,32 @@ http_file_put(void* userdata) curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); struct curl_slist* headers = NULL; - if (asprintf(&content_type_header, "Content-Type: %s", upload->mime_type) == -1) { - content_type_header = strdup(FALLBACK_CONTENTTYPE_HEADER); + content_type_header = g_strdup_printf("Content-Type: %s", upload->mime_type); + if (content_type_header) { + content_type_header = g_strdup(FALLBACK_CONTENTTYPE_HEADER); } headers = curl_slist_append(headers, content_type_header); headers = curl_slist_append(headers, "Expect:"); // Optional headers if (upload->authorization) { - if (asprintf(&auth_header, "Authorization: %s", upload->authorization) == -1) { - auth_header = strdup(FALLBACK_MSG); + auth_header = g_strdup_printf("Authorization: %s", upload->authorization); + if (!auth_header) { + auth_header = g_strdup(FALLBACK_MSG); } headers = curl_slist_append(headers, auth_header); } if (upload->cookie) { - if (asprintf(&cookie_header, "Cookie: %s", upload->cookie) == -1) { - cookie_header = strdup(FALLBACK_MSG); + cookie_header = g_strdup_printf("Cookie: %s", upload->cookie); + if (!cookie_header) { + cookie_header = g_strdup(FALLBACK_MSG); } headers = curl_slist_append(headers, cookie_header); } if (upload->expires) { - if (asprintf(&expires_header, "Expires: %s", upload->expires) == -1) { - expires_header = strdup(FALLBACK_MSG); + expires_header = g_strdup_printf("Expires: %s", upload->expires); + if (!expires_header) { + expires_header = g_strdup(FALLBACK_MSG); } headers = curl_slist_append(headers, expires_header); } @@ -262,9 +264,7 @@ http_file_put(void* userdata) // XEP-0363 specifies 201 but prosody returns 200 if (http_code != 200 && http_code != 201) { - if (asprintf(&err, "Server returned %lu", http_code) == -1) { - err = NULL; - } + err = g_strdup_printf("Server returned %lu", http_code); } #if 0 @@ -281,47 +281,50 @@ http_file_put(void* userdata) if (fh) { fclose(fh); } - free(content_type_header); free(output.buffer); - free(auth_header); - free(cookie_header); - free(expires_header); + g_free(content_type_header); + g_free(auth_header); + g_free(cookie_header); + g_free(expires_header); pthread_mutex_lock(&lock); g_free(cert_path); if (err) { - char* msg; + gchar* msg; if (upload->cancel) { - if (asprintf(&msg, "Uploading '%s' failed: Upload was canceled", upload->filename) == -1) { - msg = strdup(FALLBACK_MSG); + msg = g_strdup_printf("Uploading '%s' failed: Upload was canceled", upload->filename); + if (!msg) { + msg = g_strdup(FALLBACK_MSG); } } else { - if (asprintf(&msg, "Uploading '%s' failed: %s", upload->filename, err) == -1) { - msg = strdup(FALLBACK_MSG); + msg = g_strdup_printf("Uploading '%s' failed: %s", upload->filename, err); + if (!msg) { + msg = g_strdup(FALLBACK_MSG); } win_update_entry_message(upload->window, upload->put_url, msg); } cons_show_error(msg); - free(msg); + g_free(msg); free(err); } else { if (!upload->cancel) { - if (asprintf(&msg, "Uploading '%s': 100%%", upload->filename) == -1) { - msg = strdup(FALLBACK_MSG); + msg = g_strdup_printf("Uploading '%s': 100%%", upload->filename); + if (!msg) { + msg = g_strdup(FALLBACK_MSG); } win_update_entry_message(upload->window, upload->put_url, msg); win_mark_received(upload->window, upload->put_url); - free(msg); + g_free(msg); char* url = NULL; if (format_alt_url(upload->get_url, upload->alt_scheme, upload->alt_fragment, &url) != 0) { - char* msg; - if (asprintf(&msg, "Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url) == -1) { - msg = strdup(FALLBACK_MSG); + gchar* msg = g_strdup_printf("Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url); + if (!msg) { + msg = g_strdup(FALLBACK_MSG); } cons_show_error(msg); - free(msg); + g_free(msg); } else { switch (upload->window->type) { case WIN_CHAT: diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c index aa692ff6..bd67b53f 100644 --- a/src/ui/mucwin.c +++ b/src/ui/mucwin.c @@ -37,7 +37,6 @@ #include "config.h" #include "ui.h" -#define _GNU_SOURCE 1 #include <string.h> #include <assert.h> diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 96a99960..235a7dee 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -33,8 +33,6 @@ * */ -#define _GNU_SOURCE 1 - #include "config.h" #ifdef HAVE_GIT_VERSION @@ -242,10 +240,10 @@ stanza_create_http_upload_request(xmpp_ctx_t* ctx, const char* const id, xmpp_stanza_set_attribute(request, STANZA_ATTR_FILENAME, basename(filename_cpy)); free(filename_cpy); - char* filesize = NULL; - if (asprintf(&filesize, "%jd", (intmax_t)(upload->filesize)) != -1) { + gchar* filesize = g_strdup_printf("%jd", (intmax_t)(upload->filesize)); + if (filesize) { xmpp_stanza_set_attribute(request, STANZA_ATTR_SIZE, filesize); - free(filesize); + g_free(filesize); } xmpp_stanza_set_attribute(request, STANZA_ATTR_CONTENTTYPE, upload->mime_type); |