注意,we_wodv这个指针数组,指向解析出来的每个字符串,为了防止内存泄露,执行完wordexp函数之后,需要执行wordfree来释放这些空间。既然牵扯到申请空间,所以wordexp这个函数有一种失败是空间不足,WRDE_NOSPACE。注意,纵然返回错误码是WRDE_NOSPACE,我们也需要执行wordfree,因为有可能已经分配了部分地址空间。
前面介绍的功能看好像这个函数平平无奇,不过就是把字符串拆分一下,基本就是按照单词查分而已,其实不然,这个函数功能还比较强大。
1 函数支持通配符扩展:
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <wordexp.h>
- int
- main(int argc, char **argv)
- {
- wordexp_t p;
- char **w;
- int i;
- wordexp("ls -al *.c", &p, 0);
- w = p.we_wordv;
- for (i = 0; i < p.we_wordc; i++)
- printf("%s\n", w[i]);
- wordfree(&p);
- exit(EXIT_SUCCESS);
- }
点击(此处)折叠或打开
- [beanl@localhost wordexp]$ gcc -o test test.c
- [beanl@localhost wordexp]$ ll
- total 12
- -rwxrwxr-x 1 beanl beanl 5095 Jun 9 11:31 test
- -rw-rw-r-- 1 beanl beanl 0 Jun 9 11:06 test2.c
- -rw-rw-r-- 1 beanl beanl 0 Jun 9 11:06 test3.c
- -rw-rw-r-- 1 beanl beanl 415 Jun 9 11:31 test.c
- [beanl@localhost wordexp]$ ./test
- ls
- -al
- test.c
- test2.c
- test3.c
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <wordexp.h>
- int
- main(int argc, char **argv)
- {
- wordexp_t p;
- char **w;
- int i;
- wordexp("ls -al *[0-9].c", &p, 0);
- w = p.we_wordv;
- for (i = 0; i < p.we_wordc; i++)
- printf("%s\n", w[i]);
- wordfree(&p);
- exit(EXIT_SUCCESS);
- }
点击(此处)折叠或打开
- [beanl@localhost wordexp]$ ll
- total 12
- -rwxrwxr-x 1 beanl beanl 5099 Jun 9 11:34 test
- -rw-rw-r-- 1 beanl beanl 0 Jun 9 11:06 test2.c
- -rw-rw-r-- 1 beanl beanl 0 Jun 9 11:06 test3.c
- -rw-rw-r-- 1 beanl beanl 420 Jun 9 11:34 test.c
- [beanl@localhost wordexp]$ ./test
- ls
- -al
- test2.c
- test3.c
- [beanl@localhost wordexp]$
2 解析变量,进行替换
例如,我的环境变量SHELL为:declare -x SHELL="/bin/bash"
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <wordexp.h>
- int
- main(int argc, char **argv)
- {
- wordexp_t p;
- char **w;
- int i;
- wordexp("ls -al $SHELL", &p, 0);
- w = p.we_wordv;
- for (i = 0; i < p.we_wordc; i++)
- printf("%s\n", w[i]);
- wordfree(&p);
- exit(EXIT_SUCCESS);
- }
点击(此处)折叠或打开
- [beanl@localhost wordexp]$ ./test
- ls
- -al
- /bin/bash
- [beanl@localhost wordexp]$
当然,这个函数的功能还不限于此,我只是抛砖引玉,对这个函数感兴趣的筒子,可以参考GNU C LIB。
参考文献: