Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4194236
  • 博文数量: 776
  • 博客积分: 13014
  • 博客等级: 上将
  • 技术积分: 10391
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-22 17:00
文章分类

全部博文(776)

文章存档

2015年(55)

2014年(43)

2013年(147)

2012年(20)

2011年(82)

2010年(429)

分类: LINUX

2010-02-22 22:46:44

// 通用Makefile
// 函数编写:王晨曦
// linux下获取目录信息的函数

/* 获取目录信息函数 */
char ** dirGetInfo(const char *pathname)
{
    char **filenames;
    char no[20];
    DIR *dir;
    struct dirent *ent;
    int n = 0;

    filenames = (char **)malloc(sizeof(char*));
    filenames[0] = NULL;

    dir = opendir(pathname);
    if (!dir)
    {
        return filenames;
    }

    while ((ent = readdir(dir)))
    {
        filenames = (char**)realloc(filenames, sizeof(char*)*(n + 1));
        filenames[n] = (char*)malloc(strlen(ent->d_name) * sizeof(char) + 20 * sizeof(char));
        sprintf(no, "   [%2d] ", n - 2);
        strcpy(filenames[n], no);
        strcat(filenames[n], ent->d_name);
        n++;
    }  // end while

    closedir(dir);

    free(filenames[0]);
    filenames[0] = strdup("                     ");
    free(filenames[1]);
    filenames[1] = strdup("--------------------------------");
    filenames = (char **)realloc(filenames, sizeof(char*)*(n + 1));
    filenames[n++] = strdup("   [ q] 退出");
    filenames = (char **)realloc(filenames, sizeof(char*)*(n + 1));
    filenames[n++] = strdup("--------------------------------");
    filenames = (char **)realloc(filenames, sizeof(char*)*(n + 1));
    filenames[n] = NULL;

    return filenames;
}

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