[PATCH] replaced stdndup() with the inlined equivalent

Lubomir I. Ivanov neolit123 at gmail.com
Sun Aug 26 14:20:48 PDT 2012


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

don't kill the OS incompatibility messenger.

1)
http://stackoverflow.com/questions/6062822/whats-wrong-with-strndup
stdndup() is POSIX 2008, but apparently not available on OSX and Windows
it could be made potentially application global (e.g. a local "stdndup.h")

2)
free() memory at pointer "current_dir", once we are done.

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
 gtk-gui.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/gtk-gui.c b/gtk-gui.c
index a536c15..cdfb252 100644
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -98,7 +98,7 @@ void report_error(GError* error)
 	{
 		return;
 	}
-	
+
 	if (error_info_bar == NULL)
 	{
 		error_count = 1;
@@ -108,11 +108,11 @@ void report_error(GError* error)
 		g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
 		gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
 		                              GTK_MESSAGE_ERROR);
-		
+
 		error_label = gtk_label_new(error->message);
 		GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
 		gtk_container_add(GTK_CONTAINER(container), error_label);
-		
+
 		gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
 		gtk_widget_show_all(main_vbox);
 	}
@@ -151,7 +151,7 @@ static void file_open(GtkWidget *w, gpointer data)
 		GSList *filenames, *fn_glist;
 		char *filename;
 		filenames = fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
-		
+
 		GError *error = NULL;
 		while(filenames != NULL) {
 			filename = filenames->data;
@@ -162,7 +162,7 @@ static void file_open(GtkWidget *w, gpointer data)
 				g_error_free(error);
 				error = NULL;
 			}
-			
+
 			g_free(filename);
 			filenames = g_slist_next(filenames);
 		}
@@ -175,6 +175,8 @@ static void file_open(GtkWidget *w, gpointer data)
 /* return the path and the file component contained in the full path */
 static char *path_and_file(char *pathin, char **fileout) {
 	char *slash = pathin, *next;
+	char *result;
+  size_t len, n;
 
 	if (! pathin) {
 		*fileout = strdup("");
@@ -185,7 +187,19 @@ static char *path_and_file(char *pathin, char **fileout) {
 	if (pathin != slash)
 		slash++;
 	*fileout = strdup(slash);
-        return strndup(pathin, slash - pathin);
+
+	/* strndup(pathin, slash - pathin) */
+	n = slash - pathin;
+	len = strlen(pathin);
+	if (n < len)
+		len = n;
+
+	result = (char *)malloc(len + 1);
+	if (!result)
+		return 0;
+
+	result[len] = '\0';
+	return (char *)memcpy(result, pathin, len);
 }
 
 static void file_save_as(GtkWidget *w, gpointer data)
@@ -206,6 +220,9 @@ static void file_save_as(GtkWidget *w, gpointer data)
 	current_dir = path_and_file(existing_filename, &current_file);
 	gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), current_dir);
 	gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), current_file);
+
+	free(current_dir);
+
 	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
 		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
 	}
@@ -1276,4 +1293,4 @@ void set_filename(const char *filename)
 	existing_filename = NULL;
 	if (filename)
 		existing_filename = strdup(filename);
-}
+}
\ No newline at end of file
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list