-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopydir.h
106 lines (96 loc) · 2.8 KB
/
copydir.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include<bits/stdc++.h>
//#include"copy.h"
using namespace std;
////////////////MAKE DIRECTORY///////////////////////////////////////
char * mkdir1(char *s,char *d)
{
char * bname = basename(s);
//printf("bname=%s\n",bname);
//char *folderadd= bname;
char * str="/";
char * str4 = (char *) malloc(1 + strlen(s)+ strlen(d) );
strcpy(str4,d);
strcat(str4,str);
strcat(str4,bname);
// const char * e=(const char *)d + (const char *)str3 + (const char*)folderadd;
//string e=d+str3+folderadd;
//cout<<e<<endl;
//struct dirent *myfile;
struct stat mystat;
stat(s,&mystat);
int fileMode = mystat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
struct stat st = {0};
if (stat(str4, &st) == -1)
{
mkdir(str4,fileMode);
}
return str4;
}
char *convert(char * a,char *b)
{
char * str3 = (char *) malloc(1 + strlen(a)+ strlen(b) );
strcpy(str3,a);
char *str="/";
strcat(str3,str);
strcat(str3,b);
return str3;
}
void copydir(char *dir, int depth,char *wd)
{
char * g="hello";
printf("hello %s\n",g);
printf("wd=%s\n",wd);
DIR *dp,*desti;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL)
{
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
dp=opendir(dir);
desti=opendir(wd);
chdir(wd);
chdir(dir);
//entry1=readdir(desti);
//cout<<"outside"<<endl;
while((entry = readdir(dp)) != NULL)
{
// cout<<"hello"<<endl;
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
continue;
//cout<<endl;
char *h=convert(dir,entry->d_name);
cout<<"helllllll "<<h<<endl;
//char *h1=convert(wd,entry1->d_name);
//cout<<"wd "<<wd<<endl;
char * r=mkdir1(h,wd);
copydir(h,depth+4,r);
//rmdir(entry->d_name);//remove directory sub
}
else
{
int b=copyfile1(entry->d_name,wd);//copying file
if(b==0)
{printf("Sorry U Entered Wrong Path");
break;
}
// remove(entry->d_name);
}
}
//rmdir(entry->d_name);
chdir("..");//for new
chdir("..");
closedir(desti);
closedir(dp);
}
//////////////////////////folder add done///////////////////////////////////////////////////////////////////////