wordexp函数学习

来源:互联网 发布:mac终端更改系统时间 编辑:程序博客网 时间:2024/06/06 05:36
   GNU C LIB 库中有模式匹配这一章,除了威力非凡的正则表达式, Shell-Style Word Expansion 也是其中一节,介绍了另外一种模式匹配的思路。简单的说就是将输入的字符串,按照单词解析成一个wordexp_t类型的变量。这个变量本质是个矢量,她有we_wordc成员,表示有矢量成员的个数,换言之,就是解析成了几个单词。

    注意,we_wodv这个指针数组,指向解析出来的每个字符串,为了防止内存泄露,执行完wordexp函数之后,需要执行wordfree来释放这些空间。既然牵扯到申请空间,所以wordexp这个函数有一种失败是空间不足,WRDE_NOSPACE。注意,纵然返回错误码是WRDE_NOSPACE,我们也需要执行wordfree,因为有可能已经分配了部分地址空间。



       前面介绍的功能看好像这个函数平平无奇,不过就是把字符串拆分一下,基本就是按照单词查分而已,其实不然,这个函数功能还比较强大。

      1 函数支持通配符扩展:

点击(此处)折叠或打开

  1.        #include <stdio.h>
  2.        #include <stdlib.h>
  3.        #include <wordexp.h>

  4.        int
  5.        main(int argc, char **argv)
  6.        {
  7.            wordexp_t p;
  8.            char **w;
  9.            int i;

  10.            wordexp("ls -al *.c", &p, 0);
  11.            w = p.we_wordv;
  12.            for (= 0; i < p.we_wordc; i++)
  13.                printf("%s\n", w[i]);
  14.            wordfree(&p);
  15.            exit(EXIT_SUCCESS);
  16.        }

点击(此处)折叠或打开

  1. [beanl@localhost wordexp]$ gcc -o test test.c
  2. [beanl@localhost wordexp]$ ll
  3. total 12
  4. -rwxrwxr-x 1 beanl beanl 5095 Jun 9 11:31 test
  5. -rw-rw-r-- 1 beanl beanl 0 Jun 9 11:06 test2.c
  6. -rw-rw-r-- 1 beanl beanl 0 Jun 9 11:06 test3.c
  7. -rw-rw-r-- 1 beanl beanl 415 Jun 9 11:31 test.c
  8. [beanl@localhost wordexp]$ ./test
  9. ls
  10. -al
  11. test.c
  12. test2.c
  13. test3.c
 看到了,我们用一个*.c,这个wordexp函数就解析出来了所有.c文件。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2.        #include <stdlib.h>
  3.        #include <wordexp.h>

  4.        int
  5.        main(int argc, char **argv)
  6.        {
  7.            wordexp_t p;
  8.            char **w;
  9.            int i;

  10.            wordexp("ls -al *[0-9].c", &p, 0);
  11.            w = p.we_wordv;
  12.            for (= 0; i < p.we_wordc; i++)
  13.                printf("%s\n", w[i]);
  14.            wordfree(&p);
  15.            exit(EXIT_SUCCESS);
  16.        }

点击(此处)折叠或打开

  1. [beanl@localhost wordexp]$ ll
  2. total 12
  3. -rwxrwxr-x 1 beanl beanl 5099 Jun 9 11:34 test
  4. -rw-rw-r-- 1 beanl beanl 0 Jun 9 11:06 test2.c
  5. -rw-rw-r-- 1 beanl beanl 0 Jun 9 11:06 test3.c
  6. -rw-rw-r-- 1 beanl beanl 420 Jun 9 11:34 test.c
  7. [beanl@localhost wordexp]$ ./test
  8. ls
  9. -al
  10. test2.c
  11. test3.c
  12. [beanl@localhost wordexp]$
    看到了用正则表达式 *[0-9].c,只解析了test2.c和 test3.c。

 2 解析变量,进行替换
    例如,我的环境变量SHELL为:declare -x SHELL="/bin/bash"

点击(此处)折叠或打开

  1. #include <stdio.h>
  2.        #include <stdlib.h>
  3.        #include <wordexp.h>

  4.        int
  5.        main(int argc, char **argv)
  6.        {
  7.            wordexp_t p;
  8.            char **w;
  9.            int i;

  10.            wordexp("ls -al $SHELL", &p, 0);
  11.            w = p.we_wordv;
  12.            for (= 0; i < p.we_wordc; i++)
  13.                printf("%s\n", w[i]);
  14.            wordfree(&p);
  15.            exit(EXIT_SUCCESS);
  16.        }

点击(此处)折叠或打开

  1. [beanl@localhost wordexp]$ ./test
  2. ls
  3. -al
  4. /bin/bash
  5. [beanl@localhost wordexp]$

    当然,这个函数的功能还不限于此,我只是抛砖引玉,对这个函数感兴趣的筒子,可以参考GNU C LIB。

参考文献:
1 http://www.gnu.org/software/libc/manual/html_node/Word-Expansion.html#Word-Expansion