-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandExecution.cpp
290 lines (251 loc) · 6.8 KB
/
commandExecution.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
///////////////////////////////////RAVI KUMAR HOODA/////////2018201041////////////////////////////
#include"commandExecution.h"
#include<iostream>
#include<bits/stdc++.h>
#include<cstring>
#include<string>
using namespace std;
string copyFiles(string src,string dest)
{
ifstream fs;
ofstream ft;
char ch;
string mesg;
fs.open(src);
if(!fs)
{
mesg="Error in opening source file..!!";
}
ft.open(dest);
if(!ft)
{
mesg="Error in opening target file..!!";
fs.close();
}
while(fs.eof()==0)
{
fs>>ch;
ft<<ch;
}
fs.close();
ft.close();
return mesg;
}
string moveFiles(string src,string dest)
{
ifstream fs;
ofstream ft;
char ch;
string mesg;
fs.open(src);
if(!fs)
{
mesg="Error in opening source file..!!";
}
ft.open(dest);
if(!ft)
{
mesg="Error in opening target file..!!";
fs.close();
}
while(fs.eof()==0)
{
fs>>ch;
ft<<ch;
}
fs.close();
ft.close();
return mesg;
}
string createdirectory(string src)
{
string mesg;
if (mkdir(src.c_str(),0777) != 0)
{
mesg="directory not created successfully";
}
return mesg;
}
string renameFiles(string src,string dest)
{
ifstream fsin;
ofstream ftop;
char ch;
string mesg;
fsin.open(src);
if(!fsin)
{
mesg="Error in opening source file..!!";
}
ftop.open(dest);
if(!ftop)
{
mesg="Error in opening target file..!!";
fsin.close();
}
while(fsin.eof()==0)
{
fsin>>ch;
ftop<<ch;
}
fsin.close();
ftop.close();
return mesg;
}
string deletefile(string src)
{ string mesg;
if( remove(src.c_str()) != 0 )
mesg="Error deleting file" ;
else
mesg= "File successfully deleted" ;
return mesg;
}
string delete_dir(string src)
{ string mesg;
if (rmdir(src.c_str()) != 0)
mesg= "Error deleting folder";
else
mesg= "Folder successfully deleted" ;
return mesg;
}
string createfile(string src)
{
ofstream ftsrc;
char ch;
string mesg;
ftsrc.open(src);
if(!ftsrc)
{
mesg="Error in creating file..!";
}
else
{
mesg="File Created Successfully";
}
ftsrc.close();
return mesg;
}
void setCommandModeStatus(vector<char>&commandBuffer,string mesg)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
//cout<<"\033["<<w.ws_row-4<<";25H";
cout<<"\u001b["<<1<<"K";
//cout<<"\u001b["<<w.ws_row-7<<"A";
cout<<"\033["<<w.ws_row<<";1H";
cout<<"\u001b[33m";
cout<<"Enter Command:";
cout<<"\u001b[0m";
cout<<mesg;
cout<<"\033["<<w.ws_row<<";"<<15+mesg.length()<<"H";
commandBuffer.clear();
}
vector<string> searchFile(string dir,vector<string>&search1,string file)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
char buffer[256];
string basePath = getcwd(buffer, 256);
if((dp = opendir(dir.c_str())) == NULL)
{
//return search1;
}
chdir(dir.c_str());
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;
search1=searchFile(dir+"/"+string((entry->d_name)),search1,file);
}
else
{
if(string(entry->d_name)==file)
//search1.push_back(dir);
///////////////////////*
{vector<string> tokens;
stringstream check1(dir);
string rawcode;
while(getline(check1, rawcode, '/'))
{
tokens.push_back(rawcode);
}
search1.push_back(tokens[tokens.size()-1]);}
//////////////////
//search1.push_back(dir+"/"+string(entry->d_name));
}
}
chdir(basePath.c_str());
closedir(dp);
return search1;
}
void snap_shot(string dir,ofstream &of)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
queue<string>pathQueue;
if((dp = opendir(dir.c_str())) == NULL) {
// fprintf(stderr,"cannot open directory: %s\n", string(dir));
return;
}
chdir(dir.c_str());
while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if(string(entry->d_name)=="." ||(string(entry->d_name)) == "..")
continue;
//cout<<string(entry->d_name)<<" ";
of<<string(entry->d_name)<<" ";
pathQueue.push(string(entry->d_name));
}
else
{//cout<<string(entry->d_name)<<" ";
of<<string(entry->d_name)<<" ";
}
}
while(!pathQueue.empty())
{
// cout<<endl<<endl<<" ./"<<pathQueue.front()<<":"<<endl<<" ";
of<<endl<<endl<<" ./"<<pathQueue.front()<<":"<<endl<<" ";
snap_shot(string(dir)+"/"+pathQueue.front(),of);
chdir((dir+"/"+pathQueue.front()).c_str());
pathQueue.pop();
}
closedir(dp);
}
void rec_dir_copy(string dir,string dest)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
char buffer[256];
string basePath = getcwd(buffer, 256);
if((dp = opendir(dir.c_str())) == NULL)
{
}
chdir(dir.c_str());
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;
cout<<dir+"/"+string(entry->d_name)<<dest+"/"+string(entry->d_name);
createdirectory(dest+"/"+string(entry->d_name));
rec_dir_copy(dir+"/"+string(entry->d_name),dest+"/"+string(entry->d_name));
}
else
{
copyFiles(dir+"/"+string(entry->d_name),dest+"/"+string(entry->d_name));
}
}
chdir(basePath.c_str());
closedir(dp);
}