全部博文(776)
分类: 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;
}