From 2da1207b7df28bb0f9583debef75789e07c0777f Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Fri, 27 Nov 2015 13:01:25 +0100 Subject: [PATCH] Fix issue #271 expand path. --- include/rofi.h | 9 +++++++++ source/dialogs/script.c | 2 +- source/helper.c | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/rofi.h b/include/rofi.h index b46f6ce1..f15e903b 100644 --- a/include/rofi.h +++ b/include/rofi.h @@ -339,4 +339,13 @@ struct _Mode #define color_cyan_bold "\033[1;36m" int show_error_message ( const char *msg, int markup ); + +/** + * @param input The path to expand + * + * Expand path, both `~` and `~` + * + * @returns path + */ +char *rofi_expand_path ( const char *input ); #endif diff --git a/source/dialogs/script.c b/source/dialogs/script.c index 7a1f41f9..41bd9f19 100644 --- a/source/dialogs/script.c +++ b/source/dialogs/script.c @@ -185,7 +185,7 @@ Mode *script_switcher_parse_setup ( const char *str ) g_strlcpy ( sw->name, token, 32 ); } else if ( index == 1 ) { - sw->ed = (void *) g_strdup ( token ); + sw->ed = (void *) rofi_expand_path ( token ); } index++; } diff --git a/source/helper.c b/source/helper.c index 28582395..5a09f41e 100644 --- a/source/helper.c +++ b/source/helper.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include "helper.h" #include "x11-helper.h" @@ -661,3 +662,26 @@ int is_not_ascii ( const char * str ) } return 0; } + +char *rofi_expand_path ( const char *input ) +{ + char **str = g_strsplit ( input, G_DIR_SEPARATOR_S, -1 ); + for ( unsigned int i = 0; str && str[i]; i++ ) { + // Replace ~ with current user homedir. + if ( str[i][0] == '~' && str[i][1] == '\0' ) { + g_free ( str[i] ); + str[i] = g_strdup ( g_get_home_dir () ); + } + // If other user, ask getpwnam. + else if ( str[i][0] == '~' ) { + struct passwd *p = getpwnam ( &( str[i][1] ) ); + if ( p != NULL ) { + g_free ( str[i] ); + str[i] = g_strdup ( p->pw_dir ); + } + } + } + char *retv = g_build_filenamev ( str ); + g_strfreev ( str ); + return retv; +}