summaryrefslogtreecommitdiff
path: root/support/wordexp/wordexp.c
blob: d4ad44a9f1ad6ca5a3eab0cbd58149cbc1fde846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

#include <config.h>

#include <sys/types.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "wordexp.h"
#include "glob.h"


static int
is_valid_variable_char(char c, int pos)
{
	if ( (pos && c >= '0' && c <= '9') ||
	      c == '_' ||
	     (c >= 'a' && c <= 'z') ||
	     (c >= 'A' && c <= 'Z'))
		return 1;
	return 0;
}

/*
 * @brief replace all names of $NAME ${NAME}
 * with the corresponding environment variable 
 * @ param in: the string to be checked
 * @ return the expanded string or a copy of the existing string
 * must be free() by the calling function 
*/
static char *
expand_variables(const char *in)
{
	char *var,*pos,*ret=strdup(in);
	char *val,*str;
	pos=ret;
	while ((var=strchr(pos, '$'))) {
		char *name,*begin=var+1;
		int npos=0,bpos=0,slen,elen;
		*var='\0';
		if (var[1] == '{') {
			begin++;
			while (begin[npos]) {
				bpos=npos;
				if (begin[npos++] == '}') 
					break;
			}
		} else {
			while (is_valid_variable_char(begin[npos],npos)) 
				npos++;	
			bpos=npos;
		}
		name=strdup(begin);
		name[bpos]='\0';		
		val=getenv(name);
		free(name);
		if (! val)
			val="";
		slen=strlen(ret)+strlen(val);
		elen=strlen(begin+npos);
		str=malloc(slen+elen+1);
		strcpy(str,ret);
		strcat(str,val);
		strcat(str,begin+npos);
		free(ret);
		ret=str;
		pos=ret+slen;
	}
	return ret;
}

/*
 * @brief minimal realization of wordexp according to IEEE standard
 * shall perform word expansion as described in the Shell
 * expansion of ´$NAME´ or ´${NAME}´
 * expansion of ´*´ and ´?´
 * @param words: pointer to a string containing one or more words to be expanded
 * but here only one word supported
 */
int 
wordexp(const char *words, wordexp_t *we, int flags)
{	
	int     i;
	int     error;	
	char   *words_expanded;
	glob_t  pglob;

	assert(we != NULL);
	assert(words != NULL);
 
	/* expansion of ´$NAME´ or ´${NAME}´ */
	words_expanded=expand_variables(words);

	/* expansion of ´*´, ´?´ */
	error=glob(words_expanded, 0, NULL, &pglob);
	if (!error)
	{
		/* copy the content of struct of glob into struct of wordexp */
		we->we_wordc = pglob.gl_pathc;
		we->we_offs = pglob.gl_offs;
		we->we_wordv = malloc(we->we_wordc * sizeof(char*));
		for (i=0; i<we->we_wordc; i++)
		{
			we->we_wordv[i] = strdup(pglob.gl_pathv[i]);
		}		
		globfree(&pglob);
		free(words_expanded);
	}
	else
	{
		we->we_wordc = 1;		
		we->we_wordv = malloc(sizeof(char*));	
		we->we_wordv[0] = words_expanded;
	}


	return error;	
}


void wordfree(wordexp_t *we)
{
	int i;

	for (i=0; i < we->we_wordc; i++)
	{
		free (we->we_wordv[i]);
	}
	
	free (we->we_wordv);
	we->we_wordc = 0;
}