-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.cpp
More file actions
46 lines (37 loc) · 1.2 KB
/
copy.cpp
File metadata and controls
46 lines (37 loc) · 1.2 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
#include <unistd.h>
#include <iostream>
#include <vector>
#include <fcntl.h>
#include <sys/stat.h>
using namespace std;
void copyutil(string destination, vector<string> filelist){
for(string a : filelist){
struct stat s;
int sourcedescriptor = open(a.c_str(), O_RDONLY);
string destinationlocation;
stat(a.c_str(), &s);
if(destination[destination.length()-1]=='/')
destinationlocation = destination + a;
else
destinationlocation = destination + "/" + a;
int destinationdescriptor = open(destinationlocation.c_str(), O_CREAT|O_TRUNC |O_WRONLY, S_IRUSR|S_IWUSR);
int buffer[1024];
int countread;
while(countread=read(sourcedescriptor, buffer, sizeof(buffer))){
write(destinationdescriptor, buffer, countread);
}
chmod(destinationlocation.c_str(), s.st_mode);
}
}
void copy(int n, char const * argv[]){
string destinationfolder = argv[n-1];
vector <string> filelist;
for(int i=1; i<(n-1); i++)
filelist.push_back(argv[i]);
copyutil(destinationfolder, filelist);
}
int main(int argc, char const *argv[])
{
copy(argc, argv);
return 0;
}