16 is_valid_variable_char(char c, int pos)
18 if ( (pos && c >= '0' && c <= '9') ||
20 (c >= 'a' && c <= 'z') ||
21 (c >= 'A' && c <= 'Z'))
27 * @brief replace all names of $NAME ${NAME}
28 * with the corresponding environment variable
29 * @ param in: the string to be checked
30 * @ return the expanded string or a copy of the existing string
31 * must be free() by the calling function
34 expand_variables(const char *in)
36 char *var,*pos,*ret=strdup(in);
39 while ((var=strchr(pos, '$'))) {
40 char *name,*begin=var+1;
41 int npos=0,bpos=0,slen,elen;
47 if (begin[npos++] == '}')
51 while (is_valid_variable_char(begin[npos],npos))
61 slen=strlen(ret)+strlen(val);
62 elen=strlen(begin+npos);
63 str=malloc(slen+elen+1);
66 strcat(str,begin+npos);
75 * @brief minimal realization of wordexp according to IEEE standard
76 * shall perform word expansion as described in the Shell
77 * expansion of ´$NAME´ or ´${NAME}´
78 * expansion of ´*´ and ´?´
79 * @param words: pointer to a string containing one or more words to be expanded
80 * but here only one word supported
83 wordexp(const char *words, wordexp_t *we, int flags)
88 #ifdef HAVE_API_WIN32_BASE
93 assert(words != NULL);
95 /* expansion of ´$NAME´ or ´${NAME}´ */
96 words_expanded=expand_variables(words);
98 #ifdef HAVE_API_WIN32_BASE
99 /* expansion of ´*´, ´?´ */
100 error=glob(words_expanded, 0, NULL, &pglob);
103 /* copy the content of struct of glob into struct of wordexp */
104 we->we_wordc = pglob.gl_pathc;
105 we->we_offs = pglob.gl_offs;
106 we->we_wordv = malloc(we->we_wordc * sizeof(char*));
107 for (i=0; i<we->we_wordc; i++)
109 we->we_wordv[i] = strdup(pglob.gl_pathv[i]);
112 free(words_expanded);
118 we->we_wordv = malloc(sizeof(char*));
119 we->we_wordv[0] = words_expanded;
120 #ifdef HAVE_API_WIN32_BASE
129 void wordfree(wordexp_t *we)
133 for (i=0; i < we->we_wordc; i++)
135 free (we->we_wordv[i]);