Search
lxdream.org :: lxdream/src/lxpaths.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/lxpaths.c
changeset 1205:a486ac64f34b
prev1056:d0896e6530d6
next1283:2cbafe321d6f
author nkeynes
date Mon Jan 30 20:11:08 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Replace wordexp() with a hand-coded env-var substitution. More portable, and
avoids bugs with some wordexp() implementations
file annotate diff log raw
1.1 --- a/src/lxpaths.c Sun Jun 28 12:07:21 2009 +0000
1.2 +++ b/src/lxpaths.c Mon Jan 30 20:11:08 2012 +1000
1.3 @@ -18,7 +18,7 @@
1.4
1.5 #include <ctype.h>
1.6 #include <unistd.h>
1.7 -#include <wordexp.h>
1.8 +#include <stdlib.h>
1.9 #include <glib/gstrfuncs.h>
1.10 #include <glib/gutils.h>
1.11
1.12 @@ -72,37 +72,78 @@
1.13
1.14 gchar *get_expanded_path( const gchar *input )
1.15 {
1.16 - wordexp_t we;
1.17 - if( input == NULL ) {
1.18 + char result[PATH_MAX];
1.19 +
1.20 + char *d, *e;
1.21 + const char *s;
1.22 + d = result;
1.23 + e = result+sizeof(result)-1;
1.24 + s = input;
1.25 +
1.26 + if( input == NULL )
1.27 return NULL;
1.28 +
1.29 + while( *s ) {
1.30 + if( d == e ) {
1.31 + return g_strdup(input); /* expansion too long */
1.32 + }
1.33 + char c = *s++;
1.34 + if( c == '$' ) {
1.35 + if( *s == '{' ) {
1.36 + s++;
1.37 + const char *q = s;
1.38 + while( *q != '}' ) {
1.39 + if( ! *q ) {
1.40 + return g_strdup(input); /* unterminated variable */
1.41 + }
1.42 + q++;
1.43 + }
1.44 + char *tmp = g_strndup(s, (q-s));
1.45 + s = q+1;
1.46 + char *value = getenv(tmp);
1.47 + g_free(tmp);
1.48 + if( value != NULL ) {
1.49 + int len = strlen(value);
1.50 + if( d + len > e )
1.51 + return g_strdup(input);
1.52 + strcpy(d, value);
1.53 + d+=len;
1.54 + } /* Else, empty string */
1.55 + } else {
1.56 + const char *q = s;
1.57 + while( isalnum(*q) || *q == '_' ) {
1.58 + q++;
1.59 + }
1.60 + if( q == s ) {
1.61 + *d++ = '$';
1.62 + } else {
1.63 + char *tmp = g_strndup(s,q-s);
1.64 + s = q;
1.65 + char *value = getenv(tmp);
1.66 + g_free(tmp);
1.67 + if( value != NULL ) {
1.68 + int len = strlen(value);
1.69 + if( d + len > e )
1.70 + return g_strdup(input);
1.71 + strcpy(d, value);
1.72 + d += len;
1.73 + }
1.74 + }
1.75 + }
1.76 + } else if( c == '\\' ) {
1.77 + c = *s++;
1.78 + if( c ) {
1.79 + *d++ = c;
1.80 + } else {
1.81 + *d++ = '\\';
1.82 + }
1.83 + } else {
1.84 + *d++ = c;
1.85 + }
1.86 }
1.87 - memset(&we,0,sizeof(we));
1.88 - int result = wordexp(input, &we, WRDE_NOCMD);
1.89 - if( result != 0 || we.we_wordc == 0 ) {
1.90 - /* On failure, return the original input unchanged */
1.91 - return g_strdup(input);
1.92 - } else {
1.93 - /* On success, concatenate all 'words' together into a single
1.94 - * space-separated string
1.95 - */
1.96 - int length = we.we_wordc, i;
1.97 - gchar *result, *p;
1.98 -
1.99 - for( i=0; i<we.we_wordc; i++ ) {
1.100 - length += strlen(we.we_wordv[i]);
1.101 - }
1.102 - p = result = g_malloc(length);
1.103 - for( i=0; i<we.we_wordc; i++ ) {
1.104 - if( i != 0 )
1.105 - *p++ = ' ';
1.106 - strcpy( p, we.we_wordv[i] );
1.107 - p += strlen(p);
1.108 - }
1.109 - wordfree(&we);
1.110 - return result;
1.111 - }
1.112 + *d = '\0';
1.113 + return g_strdup(result);
1.114 }
1.115 -
1.116 gchar *get_absolute_path( const gchar *in_path )
1.117 {
1.118 char tmp[PATH_MAX];
.