Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3844246
  • 博文数量: 146
  • 博客积分: 3918
  • 博客等级: 少校
  • 技术积分: 8584
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-17 13:52
个人简介

个人微薄: weibo.com/manuscola

文章分类

全部博文(146)

文章存档

2016年(3)

2015年(2)

2014年(5)

2013年(42)

2012年(31)

2011年(58)

2010年(5)

分类: C/C++

2012-06-09 11:13:53

     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 (i = 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 (i = 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 (i = 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。

参考文献:



阅读(8986) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

GFree_Wind2012-06-13 13:46:07

Bean_lee: 呵呵,我写的这些东西比较小众,小技巧。
在新公司里压力比较大,要学的东西比较多,目前还处在打杂阶段。.....
主要做什么领域的事情呢?趋势还是挺有名气的。

Bean_lee2012-06-13 12:26:53

GFree_Wind: 新学了一招!.....
呵呵,我写的这些东西比较小众,小技巧。
在新公司里压力比较大,要学的东西比较多,目前还处在打杂阶段。

GFree_Wind2012-06-13 12:03:34

新学了一招!