1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-18 13:54:36 -05:00

Fix issue #271 expand path.

This commit is contained in:
Dave Davenport 2015-11-27 13:01:25 +01:00
parent e196df01fa
commit 2da1207b7d
3 changed files with 34 additions and 1 deletions

View file

@ -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

View file

@ -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++;
}

View file

@ -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;
}