-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShell.cpp
663 lines (532 loc) · 15.4 KB
/
Shell.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
Ajay Bechara - 1741054
Akshay Gopani - 1741063
*/
/*
How to run code
Compile : g++ main.cpp -pthread
run : a
*/
#include<windows.h>
#include<tchar.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<strsafe.h>
#include<iostream>
#include<bits/stdc++.h>
#include<unistd.h>
#include<pthread.h>
#define BUFSIZE 1024
using namespace std;
TCHAR buffer[BUFSIZE];
void DisplayErrorBox(LPTSTR lpszFunction);
int listfilesanddirectory();
void createmptyfile(string name);
void diskspaceinfo();
void pwd();
void mkdir(string input);
void cd(string input);
void rm(string filename);
void rmdir(string dirname);
void cp(string existfile,string newfile);
void pwdSilent();
int getFileList(TCHAR *path,TCHAR *target);
void readFile(string name);
#pragma comment(lib, "User32.lib");
#define BUFFERSIZE 4096
void getinput()
{
string S, T;
string commandsT[]={"touch","pwd","ls","mkdir","cd","rm","cp","rmdir","diskspace","help","countFiles","cat","exit"};
int commandCount = sizeof(commandsT)/sizeof(commandsT[0]);
vector<string> commands(commandsT , commandsT+commandCount );
cout << "Available Commands : \n";
for(int i=0 ; i<commands.size() ; i++){
cout << commands[i] << " .. ";
}
cout << "\n\n";
while(1){
pwdSilent();
cout<< "\n\n" << buffer << " >>> ";
getline(cin, S);
stringstream X(S);
int cnt=0;
vector<string> str;
while (getline(X, T, ' ')) {
cnt++;
str.push_back(T);
}
int choice=0;
for(int i=0; i<commands.size(); i++)
{
if((str[0].compare(commands[i]))==0)
{
choice=i+1;
break;
}
}
switch(choice)
{
case 1: // touch
createmptyfile(str[1]);
break;
case 2: // pwd
pwd();
break;
case 3:
listfilesanddirectory();
break;
case 4:
cout<<str[1]<<endl;
mkdir(str[1]);
break;
case 5:
cout<<str[1]<<endl;
cd(str[1]);
break;
case 6:
cout<<str[1]<<endl;
rm(str[1]);
break;
case 7:
cout<<str[1]<<endl;
cp(str[1],str[2]);
break;
case 8:
cout<<str[1]<<endl;
rmdir(str[1]);
break;
case 9:
diskspaceinfo();
break;
case 10:
cout << "\nAvailable Commands : \n";
for(int i=0 ; i<commands.size() ; i++){
cout << commands[i] << " .. ";
}
cout << "\n\n";
break;
case 11:
TCHAR path[1024];
TCHAR filename[1024];
memset(path,0,1024);
memset(filename , 0 , 1024);
strcpy(path, str[1].c_str());
strcpy( filename , str[2].c_str());
cout << "Count = " << getFileList(path,filename) << endl;
break;
case 12:
readFile(str[1]);
break;
case 13:
exit(0);
break;
default:
cout << "Command " << str[0] << " Not Found" << endl;
break;
cout << endl << endl;
}
}
}
int getFileList(TCHAR *path,TCHAR *target){
DWORD count = 0;
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
TCHAR szDir[MAX_PATH];
size_t length_of_arg;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
// Check that the input path plus 3 is not longer than MAX_PATH.
// Three characters are for the "\*" plus NULL appended below.
StringCchLength(path, MAX_PATH, &length_of_arg);
if (length_of_arg > (MAX_PATH - 3))
{
_tprintf(TEXT("\nDirectory path is too long.\n"));
return (-1);
}
// _tprintf(TEXT("\nTarget directory is %s\n\n"), path);
// Prepare string for use with FindFile functions. First, copy the
// string to a buffer, then append '\*' to the directory name.
StringCchCopy(szDir, MAX_PATH, path);
StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
// Find the first file in the directory.
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
_tprintf(TEXT("Not Able to Find First File"));
return -1;
}
// List all the files in the directory with some info about them.
TCHAR dot[5] = ".";
TCHAR dDot[5] = "..";
do
{
if( (strcmp(dot,ffd.cFileName) == 0) || (strcmp(dDot,ffd.cFileName) == 0) ){
// Not want to print
}else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ){
// _tprintf(TEXT("'%s' "), ffd.cFileName);
TCHAR buf[255];
memset(buf,0,255);
StringCchCatNA(
buf,
255,
path,
strlen(path)
);
StringCchCatNA(
buf,
255,
"/",
strlen("/")
);
StringCchCatNA(
buf,
255,
ffd.cFileName,
strlen(ffd.cFileName)
);
count += getFileList(buf,target);
}else{
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
if( strcmp(target,ffd.cFileName) == 0 )
count++;
}
}
while ( FindNextFile(hFind, &ffd) != 0);
dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES)
{
_tprintf(TEXT("Not Able to Find Files"));
}
FindClose(hFind);
return count;
}
void createmptyfile(string name)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE hTempFile = INVALID_HANDLE_VALUE;
BOOL fSuccess = FALSE;
DWORD dwRetVal = 0;
UINT uRetVal = 0;
DWORD dwBytesRead = 0;
DWORD dwBytesWritten = 0;
TCHAR szTempFileName[MAX_PATH];
TCHAR lpTempPathBuffer[MAX_PATH];
char chBuffer[BUFSIZE];
LPCTSTR errMsg;
TCHAR* str=new TCHAR[name.size()];
str[name.size()]=0;
copy(name.begin(),name.end(),str);
cout<<str<<endl;
if(strlen(str)>255)
{
cout<<"File name too long"<<endl;
}
cout<<str<<endl;
// Opens the existing file.
hTempFile = CreateFile(str, // file name
GENERIC_WRITE | GENERIC_READ, // open for write
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL,// normal file
NULL);
if (hTempFile == INVALID_HANDLE_VALUE)
{
cout<<"error"<<endl;
}
else
{
CloseHandle(hTempFile);
}
}
void pwd()
{
DWORD retval;
bool success;
retval = GetCurrentDirectory(BUFSIZE,buffer);
if (retval == 0)
{
// Handle an error condition.
printf ("GetFullPathName failed (%d)\n", GetLastError());
}
else
{
/* string s(buffer);
_tprintf(TEXT("The full path name is: %s\n"), s);
string lastDirectory;
string token;
istringstream tokenStream(s);
char delimiter = '\\';
while (getline(tokenStream, token, delimiter))
{
lastDirectory = token;
_tprintf(TEXT("The full path name is: %s\n"), token);
}
*/
_tprintf(TEXT("The full path name is: %s\n"), buffer);
}
}
void pwdSilent()
{
DWORD retval;
bool success;
// char buffer[BUFSIZE];
retval = GetCurrentDirectory(BUFSIZE,buffer);
}
void mkdir(string input)
{
BOOL success;
char* c= new TCHAR[input.length() + 1];
strcpy(c,input.c_str());
//cout<<c<<endl;
success=CreateDirectoryA(c,NULL);
//cout<<success<<endl;
if(success)
{
cout<<"directory created successfully\n"<<endl;
}
else
{
cout<<"failed to create directory"<<endl;
}
}
void cd(string input)
{
BOOL success;
char* c= new TCHAR[input.length() + 1];
strcpy(c,input.c_str());
//cout<<c<<endl;
success=SetCurrentDirectory(c);
// pwd();
//cout<<success<<endl;
if(success)
{
cout<<"directory changed successfully\n"<<endl;
}
else
{
cout<<"failed to change directory"<<endl;
}
// char *args[]={"C:\Users\shree\Desktop\os\Shell\getcd",NULL};
//execvp(args[0],args);
}
int listfilesanddirectory()
{
pwdSilent();
string input=buffer;
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
TCHAR szDir[MAX_PATH];
size_t length_of_arg;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
TCHAR* argv=new TCHAR[input.size()];
argv[input.size()]=0;
copy(input.begin(),input.end(),argv);
StringCchLength(argv, MAX_PATH, &length_of_arg);
if (length_of_arg > (MAX_PATH - 3))
{
_tprintf(TEXT("\nDirectory path is too long.\n"));
return (-1);
}
// _tprintf(TEXT("\nTarget directory is %s\n\n"),argv);
StringCchCopy(szDir, MAX_PATH, argv);
StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
// Find the first file in the directory.
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
_tprintf(TEXT("Not Able to Find First File"));
return dwError;
}
// List all the files in the directory with some info about them.
TCHAR dot[5] = ".";
TCHAR dDot[5] = "..";
int totalfile=0;
int directory = 0;
int files = 0;
do
{
if( (strcmp(dot,ffd.cFileName) == 0) || (strcmp(dDot,ffd.cFileName) == 0) ){
// Not want to print
}else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
_tprintf(TEXT("'%s' "), ffd.cFileName);
directory++;
}
else
{
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
// _tprintf(TEXT("%s %ld bytes "), ffd.cFileName, filesize.QuadPart);
_tprintf(TEXT("%s "), ffd.cFileName);
files++;
}
totalfile++;
}
while (FindNextFile(hFind, &ffd) != 0);
cout<<" "<<endl;
cout<<"\nTotal Files : " << files << " & " << "Total Directories " << directory << endl;
dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES)
{
_tprintf(TEXT("Not Able to Find Files"));
}
FindClose(hFind);
return dwError;
}
void filesize(string filename)
{
TCHAR* argv=new TCHAR[filename.size()];
argv[filename.size()]=0;
copy(filename.begin(),filename.end(),argv);
HANDLE hFile;
DWORD dwBytesRead = 0;
hFile = CreateFile(argv, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
NULL); // no attr. template
DWORD filesize=GetFileSize(hFile,NULL);
cout<<filesize<<endl;
}
void rm(string filename)
{
BOOL success;
char* c= new TCHAR[filename.length() + 1];
strcpy(c,filename.c_str());
success=DeleteFileA(c);
if(success==1)
{
cout<<"filedeleted"<<endl;
}
else
cout<<"-------error---------"<<endl;
}
void cp(string existfile,string newfile)
{
char* e= new TCHAR[existfile.length() + 1];
strcpy(e,existfile.c_str());
char* n= new TCHAR[newfile.length() + 1];
strcpy(n,newfile.c_str());
BOOL success;
success=CopyFile(e,n,FALSE); //if new file is already exist then it will overwrite if FALSE last param
if(success==1)
{
cout<<"filecopied"<<endl;
}
else
cout<<"-------error---------"<<endl;
}
void rmdir(string dirname)
{
BOOL success;
char* dir= new TCHAR[dirname.length() + 1];
strcpy(dir,dirname.c_str());
success=RemoveDirectoryA(dir);
if(success==1)
{
cout<<"directory deleted"<<endl;
}
else
cout<<"-------error---------"<<endl;
}
void diskspaceinfo()
{
BOOL success;
// LPCSTR lpRootPathName1="C:";
LPCSTR lpRootPathName1="C:";
LPDWORD lpSectorsPerCluster;
LPDWORD lpBytesPerSector;
LPDWORD lpNumberOfFreeClusters;
LPDWORD lpTotalNumberOfClusters;
cout<<lpRootPathName1<<endl;
// cout<<lpRootPathName2<<endl;
success=GetDiskFreeSpaceA(
lpRootPathName1,
lpSectorsPerCluster,
lpBytesPerSector,
lpNumberOfFreeClusters,
lpTotalNumberOfClusters
);
if(success)
{
cout<<"dsdsdsd"<<endl;
cout<<lpRootPathName1<<endl;
cout<<lpSectorsPerCluster<<endl;
cout<<lpBytesPerSector<<endl;
cout<<lpNumberOfFreeClusters<<endl;
cout<<lpTotalNumberOfClusters<<endl;
}
/* success=GetDiskFreeSpaceA(
lpRootPathName1,
lpSectorsPerCluster,
lpBytesPerSector,
lpNumberOfFreeClusters,
lpTotalNumberOfClusters
);
if(success)
{
cout<<lpRootPathName2<<endl;
cout<<lpSectorsPerCluster<<endl;
cout<<lpBytesPerSector<<endl;
cout<<lpNumberOfFreeClusters<<endl;
cout<<lpTotalNumberOfClusters<<endl;
}*/
}
void readFile(string name){
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE hTempFile = INVALID_HANDLE_VALUE;
BOOL fSuccess = FALSE;
DWORD lpNumberOfBytesRead=0;
OVERLAPPED ol = {0};
TCHAR szTempFileName[MAX_PATH];
TCHAR lpTempPathBuffer[MAX_PATH];
char chbuffer[BUFFERSIZE];
TCHAR* str=new TCHAR[name.size()];
str[name.size()]=0;
copy(name.begin(),name.end(),str);
if(strlen(str)>255)
{
cout<<"File name too long"<<endl;
}
hTempFile = CreateFile(str, // file name
GENERIC_READ, // open for write
0, // do not share
NULL, // default security
OPEN_EXISTING, // overwrite existing
FILE_ATTRIBUTE_NORMAL,// normal file
NULL);
if (hTempFile == INVALID_HANDLE_VALUE)
{
cout<<"error"<<endl;
}
else
{
while(1)
{
memset(chbuffer,0,BUFFERSIZE);
fSuccess= ReadFile(hTempFile,chbuffer,BUFFERSIZE,&lpNumberOfBytesRead,NULL);
if(fSuccess && lpNumberOfBytesRead==0)
{
break;
}
else
{
cout<<chbuffer<<endl;
}
// memset(chbuffer,0,sizeof(chbuffer));
}
CloseHandle(hTempFile);
}
}
int main()
{
getinput();
return 0;
}