-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallfile.cpp
More file actions
73 lines (58 loc) · 1.27 KB
/
allfile.cpp
File metadata and controls
73 lines (58 loc) · 1.27 KB
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
73
#include <iostream>
#include <cstdlib>
#include <queue>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <cstring>
bool isfile(std::string path)
{
struct stat buf;
stat(path.c_str(),&buf);
return S_ISREG(buf.st_mode);
}
bool isdir(std::string path)
{
struct stat buf;
stat(path.c_str(),&buf);
return S_ISDIR(buf.st_mode);
}
int main(int argc,char** argv)
{
std::queue<std::string> dirs;
char* abspath=realpath(argv[1],NULL);
printf("%s\n",abspath);
dirs.push(std::string(abspath));
free(abspath);
std::string current;
struct dirent* dir_entry;
int c=0;
while(!dirs.empty())
{
current=dirs.front();
dirs.pop();
DIR* dir=opendir(current.c_str());
if(dir!=NULL)
{
//printf("%s\n",current.c_str());
while((dir_entry=readdir(dir))!=NULL)
{
//std::cout << dir_entry->d_name;
if(!(strcmp(dir_entry->d_name,".")==0 || strcmp(dir_entry->d_name,"..")==0))
{
//std::cout << current << "/" << dir_entry->d_name << std::endl;
std::string stuff=current + "/"+dir_entry->d_name;
if(isfile(stuff))
c++;
else if(isdir(stuff))
dirs.push(stuff);
}
}
closedir(dir);
}
else
printf("Couldn't open %s\n",current.c_str());
}
printf("%d Files\n",c);
}