Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2969117
  • 博文数量: 272
  • 博客积分: 5544
  • 博客等级: 大校
  • 技术积分: 5496
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 00:48
个人简介

  每个人都要有一个骨灰级的爱好,不为金钱,而纯粹是为了在这个领域享受追寻真理的快乐。

文章分类

全部博文(272)

文章存档

2015年(2)

2014年(5)

2013年(25)

2012年(58)

2011年(182)

分类: LINUX

2012-05-29 17:44:21


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

  4. int find_path(const char *path) {
  5.     if (path == NULL)    return -1;
  6.     char *temp = strdup(path);
  7.     char *p;
  8.     
  9.     /* 如果返回不是NULL,把找到最后一个'/'字符的地址
  10.     赋值给指针p,然后把该位置截断为0 */
  11.     if ((p = strrchr(temp, '/')) != NULL)
  12.         *p = '\0';
  13.     /* 取得文件名 */
  14.     printf("filename: %s\n", p+1);
  15. #if 0
  16.     /* for循环处理原理和strrchr一样 */
  17.     char *pos = temp;
  18.     int i = 0;
  19.     for ( ; *pos != '\0'; ++ pos) {
  20.         if (*pos == '/') {
  21.             p = pos;
  22.             i++;
  23.         }
  24.     }
  25.     if(i)    *p = '\0';
  26. #endif

  27.     printf("PATH is %s\n", temp);
  28.     free(temp);
  29.     return 0;
  30. }

  31. int mkdir_r(const char *path) {
  32.         if (path == NULL) {
  33.                 return -1;
  34.         }
  35.         char *temp = strdup(path);
  36.         char *pos = temp;

  37.         /* 去掉开头的 './''/' */
  38.         if (strncmp(temp, "/", 1) == 0) {
  39.                 pos += 1;
  40.         } else if (strncmp(temp, "./", 2) == 0) {
  41.                 pos += 2;
  42.         }
  43.         
  44.         /* 循环创建目录 */
  45.         for ( ; *pos != '\0'; ++ pos) {
  46.                 if (*pos == '/') {
  47.                         *pos = '\0';
  48.                         mkdir(temp, 0644);
  49.                         printf("for %s\n", temp);
  50.                         *pos = '/';
  51.                 }
  52.         }
  53.         
  54.         /* 如果最后一级目录不是以'/'结尾,
  55.         遇到'\0'就中止循环,
  56.         不会创建最后一级目录 */
  57.         if (*(pos - 1) != '/') {
  58.                 printf("if %s\n", temp);
  59.                 mkdir(temp, 0644);
  60.         }
  61.         free(temp);
  62.         return 0;
  63. }

  64. int main() {
  65. //        mkdir_r("./a/b/c/d");
  66.         find_path("/test/abc/foo.sh");
  67.         return 0;
  68. }

阅读(6009) | 评论(0) | 转发(1) |
0

上一篇:ICMP类型统计代码

下一篇:IP->ID范围统计

给主人留下些什么吧!~~