-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_dir.c
93 lines (78 loc) · 2.16 KB
/
list_dir.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <vector>
#include <stdlib.h>
#define FILEPATH_MAX 250
using namespace std;
void get_dir(char *dir, vector<char*> &vec)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
char *curr_path = (char *)malloc(FILEPATH_MAX);
getcwd(curr_path,FILEPATH_MAX);
vec.push_back(curr_path);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
char *tmp = (char*)malloc(strlen(entry->d_name)+1);
memset(tmp, 0x00, strlen(entry->d_name)+1);
strcpy(tmp, entry->d_name);
get_dir(entry->d_name, vec);
}
}
chdir("..");
closedir(dp);
}
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
printf("%d%s%s/\n",depth,"",entry->d_name);
printdir(entry->d_name,depth+4);
}
else printf("%d%s%s\n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
int main(int argc, char* argv[])
{
char *topdir, pwd[2]=".";
if (argc != 2)
topdir=pwd;
else
topdir=argv[1];
printf("Directory scan of %s\n",topdir);
vector<char*> vec;
vector<char*>::iterator iter;
get_dir(topdir, vec);
for(iter = vec.begin(); iter != vec.end(); iter++){
printf("%s\n", *iter);
}
printf("done.\n");
return 0;
}