-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls_r.h
More file actions
79 lines (70 loc) · 1.65 KB
/
Copy pathls_r.h
File metadata and controls
79 lines (70 loc) · 1.65 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
74
75
76
77
78
#include<bits/stdc++.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
using namespace std;
void filewrite(char *c,char * f)
{
FILE *fptr;
fptr = fopen(f, "a");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
// printf("Enter a sentence:\n");
// gets(sentence);
fprintf(fptr,"%s\n",c);
fclose(fptr);
}
void ls_r(char *dir,char *file_name)
{
DIR *dp;
struct dirent *entry;
string s=dir;
unordered_map< string, list< string > > adj;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
//adj[s]
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/* Found a directory, but ignore . and .. */
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
//printf("%*s%s/\n",depth,"",entry->d_name);
/* Recurse at a new indent level */
adj[s].push_back(entry->d_name);
ls_r(entry->d_name,file_name);
//filewrite(entry->d_name,file_name);
}
else
{
adj[s].push_back(entry->d_name);
//filewrite(entry->d_name,file_name);
//printf("%*s%s\n",depth,"",entry->d_name);
}
}
chdir("..");
closedir(dp);
for (auto itr1=adj.begin();itr1!=adj.end();itr1++) {
string vertex = itr1->first;
list<string> adjv = itr1->second;
list<string>::iterator itr = adjv.begin();
filewrite((char*)vertex.c_str(),file_name);
while (itr != adjv.end()) {
string t=*itr;
filewrite((char*)t.c_str(),file_name);
itr++;
}
//cout << endl;
}
}