Shell Word Expansion
Jan. 9th, 2013 12:50 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
This is how you perform word expansions using wordexp() system interface:
https://gist.github.com/4492253
alexo@kuha:~/wordexp$ make cc -o wordexp wordexp.c ./wordexp echo hello world 0: 'echo' 1: 'hello' 2: 'world' "\$HOME = $HOME" '$PWD'\ =\ $PWD 0: '$HOME = /home/alexo' 1: '$PWD = /home/alexo/wordexp' alexo@kuha:~/wordexp$
https://gist.github.com/4492253
#define _XOPEN_SOURCE 700 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <wordexp.h> int main() { while (!feof(stdin)) { char *words = NULL; size_t len = 0; char *newline; wordexp_t res; char **list; int i, n; getline(&words, &len, stdin); newline = strchr(words, '\n'); if (newline) *newline = '\0'; if (wordexp(words, &res, WRDE_SHOWERR)) exit(EXIT_FAILURE); list = res.we_wordv; n = res.we_wordc; for (i = 0; i < n; i++) printf("%d: '%s'\n", i, list[i]); wordfree(&res); free(words); } return 0; }