[tepl] utils: replace_home_dir_with_tilde(): fix bug if $HOME ends with a slash
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] utils: replace_home_dir_with_tilde(): fix bug if $HOME ends with a slash
- Date: Mon, 11 Jan 2021 14:28:22 +0000 (UTC)
commit 8fc0eb83f96098a4ba83fda44d1b67e26153add0
Author: Sébastien Wilmet <sw swilmet be>
Date: Mon Jan 11 15:11:51 2021 +0100
utils: replace_home_dir_with_tilde(): fix bug if $HOME ends with a slash
Fixes https://gitlab.gnome.org/GNOME/tepl/-/issues/19
tepl/tepl-utils.c | 78 +++++++++++++++++++++++++++++++++++++------------------
1 file changed, 53 insertions(+), 25 deletions(-)
---
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index 480e888..d1add27 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -301,6 +301,40 @@ tepl_utils_get_file_shortname (const gchar *filename)
return g_strndup (filename, get_extension_position (filename));
}
+static gchar *
+get_home_dir_without_trailing_slash (void)
+{
+ const gchar *home_dir;
+ gchar *utf8_home_dir;
+ gsize length;
+
+ home_dir = g_get_home_dir ();
+ if (home_dir == NULL)
+ {
+ return NULL;
+ }
+
+ utf8_home_dir = g_filename_to_utf8 (home_dir, -1, NULL, NULL, NULL);
+ if (utf8_home_dir == NULL)
+ {
+ return NULL;
+ }
+
+ length = strlen (utf8_home_dir);
+ if (length == 0)
+ {
+ g_free (utf8_home_dir);
+ return NULL;
+ }
+
+ if (utf8_home_dir[length - 1] == '/')
+ {
+ utf8_home_dir[length - 1] = '\0';
+ }
+
+ return utf8_home_dir;
+}
+
/**
* tepl_utils_replace_home_dir_with_tilde:
* @filename: the filename.
@@ -311,48 +345,42 @@ tepl_utils_get_file_shortname (const gchar *filename)
* Returns: the new filename. Free with g_free().
* Since: 4.4
*/
-/* This function comes from gedit. */
gchar *
tepl_utils_replace_home_dir_with_tilde (const gchar *filename)
{
- gchar *tmp;
- gchar *home;
+ gchar *home_dir_without_trailing_slash;
+ gchar *home_dir_with_trailing_slash;
+ gchar *ret;
g_return_val_if_fail (filename != NULL, NULL);
- /* Note that g_get_home_dir returns a const string */
- tmp = (gchar *) g_get_home_dir ();
-
- if (tmp == NULL)
+ home_dir_without_trailing_slash = get_home_dir_without_trailing_slash ();
+ if (home_dir_without_trailing_slash == NULL)
{
return g_strdup (filename);
}
- home = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL);
- if (home == NULL)
- {
- return g_strdup (filename);
- }
+ home_dir_with_trailing_slash = g_strdup_printf ("%s/", home_dir_without_trailing_slash);
- if (g_str_equal (filename, home))
+ if (g_str_equal (filename, home_dir_without_trailing_slash) ||
+ g_str_equal (filename, home_dir_with_trailing_slash))
{
- g_free (home);
- return g_strdup ("~");
+ ret = g_strdup ("~");
+ goto out;
}
- tmp = home;
- home = g_strdup_printf ("%s/", tmp);
- g_free (tmp);
-
- if (g_str_has_prefix (filename, home))
+ if (g_str_has_prefix (filename, home_dir_with_trailing_slash))
{
- gchar *res = g_strdup_printf ("~/%s", filename + strlen (home));
- g_free (home);
- return res;
+ ret = g_strdup_printf ("~/%s", filename + strlen (home_dir_with_trailing_slash));
+ goto out;
}
- g_free (home);
- return g_strdup (filename);
+ ret = g_strdup (filename);
+
+out:
+ g_free (home_dir_without_trailing_slash);
+ g_free (home_dir_with_trailing_slash);
+ return ret;
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]