-
Notifications
You must be signed in to change notification settings - Fork 2
/
getFileName.h
72 lines (69 loc) · 1.54 KB
/
getFileName.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef getFileName_h
#define getFileName_h
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
struct node * next;
char fileName[256];
} _fileNameNode;
void freefileNameList(_fileNameNode *head){
_fileNameNode * pCur;
while(pCur = head){
head = head->next;
free(pCur);
}
}
_fileNameNode * getFileNameByDir(const char*dir){
_fileNameNode * head = NULL,* pCurrent,* pLast;
DIR *dp;
struct dirent *entry;
struct stat statbuf;
/*打开目录 */
if((dp=opendir(dir))==NULL){
perror("opendir error");
return NULL;
}
/*进入目录*/
chdir(dir);
while((entry = readdir(dp))!=NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
/*是目录*/
}else{
/*返回非隐藏文件*/
if(entry->d_name[0]!= '.'){
/*doing something*/
//printf("%s\n %d \n",entry->d_name,sizeof(entry->d_name));
pCurrent = (_fileNameNode *)malloc(sizeof(_fileNameNode));
pCurrent->next = NULL;
strcpy(pCurrent->fileName,entry->d_name);
if(head==NULL){
head = pLast = pCurrent;
}else{
pLast->next = pCurrent;
pLast = pCurrent;
}
}
}
}
chdir("..");
closedir(dp);
return head;
}
/*
int main(void){
_fileNameNode * pFile,* pCur;
pCur = pFile = getFileNameByDir("lib");
while(pCur){
printf("%s\n",pCur->fileName);
pCur = pCur->next;
}
freefileNameList(pFile);
pFile = pCur = NULL;
return 0;
}
*/
#endif