aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-12-09 08:25:37 +0100
committerMichael Vetter <jubalh@iodoru.org>2020-12-09 08:44:26 +0100
commita2291b36c45ebaf67065ab600a862f64c5ec0281 (patch)
treebf1a2f738d614b246d3b72778d977c60d6fcaccd /src
parent7a319df6c80c472adcf416c8737e7b156854352b (diff)
downloadprofani-tty-a2291b36c45ebaf67065ab600a862f64c5ec0281.tar.gz
Expand ~ in unique_filename_from_url()
unique_filename_from_url() is used for `/url save`. It doesn't recognize ~ by itself, we need to expand it first. Mentioned in https://github.com/profanity-im/profanity/pull/1375#pullrequestreview-547892462
Diffstat (limited to 'src')
-rw-r--r--src/common.c30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/common.c b/src/common.c
index 10be280a..327b3586 100644
--- a/src/common.c
+++ b/src/common.c
@@ -629,18 +629,40 @@ _basename_from_url(const char* url)
}
gchar*
+get_expanded_path(const char *path)
+{
+ GString* exp_path = g_string_new("");
+ gchar *result;
+
+ if (strlen(path) >= 2 && path[0] == '~' && path[1] == '/') {
+ g_string_printf(exp_path, "%s/%s", getenv("HOME"), path+2);
+ } else {
+ g_string_printf(exp_path, "%s", path+2);
+ }
+
+ result = exp_path->str;
+ g_string_free(exp_path, FALSE);
+
+ return result;
+}
+
+gchar*
unique_filename_from_url(const char* url, const char* path)
{
+ gchar *realpath;
+
// Default to './' as path when none has been provided.
if (path == NULL) {
- path = "./";
+ realpath = "./";
+ } else {
+ realpath = get_expanded_path(path);
}
// Resolves paths such as './../.' for path.
- GFile* target = g_file_new_for_commandline_arg(path);
+ GFile* target = g_file_new_for_commandline_arg(realpath);
gchar* filename = NULL;
- if (_has_directory_suffix(path) || g_file_test(path, G_FILE_TEST_IS_DIR)) {
+ if (_has_directory_suffix(realpath) || g_file_test(realpath, G_FILE_TEST_IS_DIR)) {
// The target should be used as a directory. Assume that the basename
// should be derived from the URL.
char* basename = _basename_from_url(url);
@@ -654,11 +676,13 @@ unique_filename_from_url(const char* url, const char* path)
gchar* unique_filename = _unique_filename(filename);
if (unique_filename == NULL) {
g_free(filename);
+ g_free(realpath);
return NULL;
}
g_object_unref(target);
g_free(filename);
+ g_free(realpath);
return unique_filename;
}