[personal profile] codedot
This is how you perform word expansions using wordexp() system interface:

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

Profile

Anton Salikhmetov

November 2018

S M T W T F S
    123
45678 910
11121314151617
18192021222324
252627282930 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 27th, 2025 04:43 pm
Powered by Dreamwidth Studios