diff options
author | Dave Davenport <qball@gmpclient.org> | 2015-11-27 13:01:25 +0100 |
---|---|---|
committer | Dave Davenport <qball@gmpclient.org> | 2015-11-27 13:01:25 +0100 |
commit | 2da1207b7df28bb0f9583debef75789e07c0777f (patch) | |
tree | cdafdeeb8b89f2b7c654fbc76e7e4629bb19a2b9 | |
parent | e196df01fa8b27c36e61933f89f8638945cb48e4 (diff) |
Fix issue #271 expand path.
-rw-r--r-- | include/rofi.h | 9 | ||||
-rw-r--r-- | source/dialogs/script.c | 2 | ||||
-rw-r--r-- | source/helper.c | 24 |
3 files changed, 34 insertions, 1 deletions
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 `~<user>` + * + * @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 <sys/types.h> #include <sys/file.h> #include <sys/stat.h> +#include <pwd.h> #include <ctype.h> #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; +} |