From cf3d50f855a0ce3adf24d05121bc4c4b5c1910dc Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sun, 18 Nov 2012 01:51:01 +0200 Subject: [PATCH 1/2] command.c: replace strndup with strdup Function strndup conforms to POSIX.1-2008 and MacOS doesn't have it. strndup doesn't make sense when a string is copied to the end. So replacing fixes incompatibility with MacOS and doesn't influence to execution. --- src/command.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/command.c b/src/command.c index a02c77e9..55b755f2 100644 --- a/src/command.c +++ b/src/command.c @@ -1010,7 +1010,7 @@ _cmd_help(const char * const inp, struct cmd_help_t help) } else if (strcmp(inp, "/help navigation") == 0) { cons_navigation_help(); } else { - char *cmd = strndup(inp+6, strlen(inp)-6); + char *cmd = strdup(inp + 6); char cmd_with_slash[1 + strlen(cmd) + 1]; sprintf(cmd_with_slash, "/%s", cmd); @@ -1193,7 +1193,7 @@ _cmd_msg(const char * const inp, struct cmd_help_t help) usr = strtok(NULL, " "); if ((usr != NULL) && (strlen(inp) > (5 + strlen(usr) + 1))) { // get message - msg = strndup(inp+5+strlen(usr)+1, strlen(inp)-(5+strlen(usr)+1)); + msg = strdup(inp + 5 + strlen(usr) + 1); if (msg != NULL) { jabber_send(msg, usr); @@ -1205,6 +1205,8 @@ _cmd_msg(const char * const inp, struct cmd_help_t help) } } else { + /* TODO: this should be error while sending message cause + * this is the case when strdup can't allocate memory */ cons_show("Usage: %s", help.usage); } } else { @@ -1612,7 +1614,7 @@ _update_presence(const jabber_presence_t presence, { char *msg; if (strlen(inp) > strlen(show) + 2) { - msg = strndup(inp+(strlen(show) + 2), strlen(inp)-(strlen(show) + 2)); + msg = strdup(inp + strlen(show) + 2); } else { msg = NULL; } From 2630c111bedc0fc392fb539a44730c1633d29990 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sun, 18 Nov 2012 04:31:32 +0200 Subject: [PATCH 2/2] use internal implementation of getline MacOS doesn't have function getline --- src/chat_log.c | 15 +++------------ src/common.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 2 ++ 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/chat_log.c b/src/chat_log.c index 43ba820a..e1aa6865 100644 --- a/src/chat_log.c +++ b/src/chat_log.c @@ -129,8 +129,7 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient, char *filename = _get_log_filename(recipient, login, log_date, FALSE); FILE *logp = fopen(filename, "r"); - char *line = NULL; - size_t read = 0; + char *line; if (logp != NULL) { GString *gs_header = g_string_new(""); g_string_append_printf(gs_header, "%d/%d/%d:", @@ -141,16 +140,8 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient, history = g_slist_append(history, header); g_string_free(gs_header, TRUE); - size_t length = getline(&line, &read, logp); - while (length != -1) { - char *copy = malloc(length); - copy = strncpy(copy, line, length); - copy[length -1] = '\0'; - history = g_slist_append(history, copy); - free(line); - line = NULL; - read = 0; - length = getline(&line, &read, logp); + while ((line = prof_getline(logp)) != NULL) { + history = g_slist_append(history, line); } fclose(logp); diff --git a/src/common.c b/src/common.c index 840a0803..cdfa944c 100644 --- a/src/common.c +++ b/src/common.c @@ -29,6 +29,10 @@ #include "common.h" +// assume malloc stores at most 8 bytes for size of allocated memory +// and page size is at least 4KB +#define READ_BUF_SIZE 4088 + // backwards compatibility for GLib version < 2.28 void p_slist_free_full(GSList *items, GDestroyNotify free_func) @@ -118,3 +122,48 @@ encode_xml(const char * const xml) return coded_msg3; } + +char * +prof_getline(FILE *stream) +{ + char *buf; + size_t buf_size; + char *result; + char *s = NULL; + size_t s_size = 1; + int need_exit = 0; + + buf = (char *)malloc(READ_BUF_SIZE); + + while (TRUE) { + result = fgets(buf, READ_BUF_SIZE, stream); + if (result == NULL) + break; + buf_size = strlen(buf); + if (buf[buf_size - 1] == '\n') { + buf_size--; + buf[buf_size] = '\0'; + need_exit = 1; + } + + result = (char *)realloc(s, s_size + buf_size); + if (result == NULL) { + if (s != NULL) { + free(s); + s = NULL; + } + break; + } + s = result; + + memcpy(s + s_size - 1, buf, buf_size); + s_size += buf_size; + s[s_size - 1] = '\0'; + + if (need_exit != 0 || feof(stream) != 0) + break; + } + + free(buf); + return s; +} diff --git a/src/common.h b/src/common.h index 7ee774c4..47de27fb 100644 --- a/src/common.h +++ b/src/common.h @@ -23,6 +23,7 @@ #ifndef COMMON_H #define COMMON_H +#include #include #if !GLIB_CHECK_VERSION(2,28,0) @@ -41,5 +42,6 @@ char * str_replace(const char *string, const char *substr, const char *replacement); int str_contains(char str[], int size, char ch); char* encode_xml(const char * const xml); +char * prof_getline(FILE *stream); #endif