-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThree.cpp
641 lines (586 loc) · 20.8 KB
/
Three.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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
// Function declarations
void displayCommands();
void createFile(const std::string& filename);
void deleteFile(const std::string& filename);
void openFile(const std::string& filename);
void readFile(const std::string& filename);
void writeFile(const std::string& filename);
void appendToFile(const std::string& filename);
void renameFile(const std::string& oldname, const std::string& newname);
void copyFile(const std::string& source, const std::string& destination);
void moveFile(const std::string& source, const std::string& destination);
void changePermissions(const std::string& filename);
void getFileInformation(const std::string& filename);
void listFiles(const std::string& directory);
void navigateDirectory(const std::string& directory);
void createProcess(const std::string& processName);
void terminateProcess(const std::string& processName);
void suspendProcess(const std::string& processName);
void resumeProcess(const std::string& processName);
void getProcessInformation(const std::string& processName);
void scheduleProcesses();
void allocateMemory(const std::string& processName, int size);
void freeMemory(const std::string& processName);
void handleUserInput();
void displayOutput(const std::string& output);
void handleSystemInterrupts();
void establishNetworkConnection(const std::string& ipAddress);
void sendData(const std::string& data);
void receiveData();
void implementProtocol(const std::string& protocol);
void manageNetworkInterfaces();
void authenticateUser(const std::string& username, const std::string& password);
void authorizeUser(const std::string& username, const std::string& permission);
void encryptFile(const std::string& filename);
void decryptFile(const std::string& filename);
void configureSystem();
void manageUsers();
void installSoftware(const std::string& softwareName);
void backupSystem();
void restoreSystem();
void handleErrors();
void manageDateTime();
void managePower();
void manageSettings();
void handleGUI();
void handleCLI();
int main() {
std::string command;
std::cout << "Welcome to MyOS!\n";
displayCommands();
while (true) {
std::cout << "\nEnter a command: ";
std::getline(std::cin, command);
// Splitting the command into tokens
std::vector<std::string> tokens;
std::string token;
std::stringstream ss(command);
while (ss >> token) {
tokens.push_back(token);
}
if (tokens.empty()) {
continue;
}
if (tokens[0] == "help") {
displayCommands();
} else if (tokens[0] == "create") {
if (tokens.size() >= 2) {
createFile(tokens[1]);
} else {
std::cout << "Invalid command. Usage: create <filename>\n";
}
} else if (tokens[0] == "delete") {
if (tokens.size() >= 2) {
deleteFile(tokens[1]);
} else {
std::cout << "Invalid command. Usage: delete <filename>\n";
}
} else if (tokens[0] == "open") {
if (tokens.size() >= 2) {
openFile(tokens[1]);
} else {
std::cout << "Invalid command. Usage: open <filename>\n";
}
} else if (tokens[0] == "read") {
if (tokens.size() >= 2) {
readFile(tokens[1]);
} else {
std::cout << "Invalid command. Usage: read <filename>\n";
}
} else if (tokens[0] == "write") {
if (tokens.size() >= 2) {
writeFile(tokens[1]);
} else {
std::cout << "Invalid command. Usage: write <filename>\n";
}
} else if (tokens[0] == "append") {
if (tokens.size() >= 2) {
appendToFile(tokens[1]);
} else {
std::cout << "Invalid command. Usage: append <filename>\n";
}
} else if (tokens[0] == "rename") {
if (tokens.size() >= 3) {
renameFile(tokens[1], tokens[2]);
} else {
std::cout << "Invalid command. Usage: rename <oldname> <newname>\n";
}
} else if (tokens[0] == "copy") {
if (tokens.size() >= 3) {
copyFile(tokens[1], tokens[2]);
} else {
std::cout << "Invalid command. Usage: copy <source> <destination>\n";
}
} else if (tokens[0] == "move") {
if (tokens.size() >= 3) {
moveFile(tokens[1], tokens[2]);
} else {
std::cout << "Invalid command. Usage: move <source> <destination>\n";
}
} else if (tokens[0] == "permissions") {
if (tokens.size() >= 2) {
changePermissions(tokens[1]);
} else {
std::cout << "Invalid command. Usage: permissions <filename>\n";
}
} else if (tokens[0] == "info") {
if (tokens.size() >= 2) {
getFileInformation(tokens[1]);
} else {
std::cout << "Invalid command. Usage: info <filename>\n";
}
} else if (tokens[0] == "list") {
if (tokens.size() >= 2) {
listFiles(tokens[1]);
} else {
std::cout << "Invalid command. Usage: list <directory>\n";
}
} else if (tokens[0] == "navigate") {
if (tokens.size() >= 2) {
navigateDirectory(tokens[1]);
} else {
std::cout << "Invalid command. Usage: navigate <directory>\n";
}
} else if (tokens[0] == "createprocess") {
if (tokens.size() >= 2) {
createProcess(tokens[1]);
} else {
std::cout << "Invalid command. Usage: createprocess <processName>\n";
}
} else if (tokens[0] == "terminate") {
if (tokens.size() >= 2) {
terminateProcess(tokens[1]);
} else {
std::cout << "Invalid command. Usage: terminate <processName>\n";
}
} else if (tokens[0] == "suspend") {
if (tokens.size() >= 2) {
suspendProcess(tokens[1]);
} else {
std::cout << "Invalid command. Usage: suspend <processName>\n";
}
} else if (tokens[0] == "resume") {
if (tokens.size() >= 2) {
resumeProcess(tokens[1]);
} else {
std::cout << "Invalid command. Usage: resume <processName>\n";
}
} else if (tokens[0] == "getinfo") {
if (tokens.size() >= 2) {
getProcessInformation(tokens[1]);
} else {
std::cout << "Invalid command. Usage: getinfo <processName>\n";
}
} else if (tokens[0] == "schedule") {
scheduleProcesses();
} else if (tokens[0] == "allocate") {
if (tokens.size() >= 3) {
allocateMemory(tokens[1], std::stoi(tokens[2]));
} else {
std::cout << "Invalid command. Usage: allocate <processName> <size>\n";
}
} else if (tokens[0] == "free") {
if (tokens.size() >= 2) {
freeMemory(tokens[1]);
} else {
std::cout << "Invalid command. Usage: free <processName>\n";
}
} else if (tokens[0] == "input") {
handleUserInput();
} else if (tokens[0] == "output") {
if (tokens.size() >= 2) {
displayOutput(tokens[1]);
} else {
std::cout << "Invalid command. Usage: output <text>\n";
}
} else if (tokens[0] == "interrupts") {
handleSystemInterrupts();
} else if (tokens[0] == "connect") {
if (tokens.size() >= 2) {
establishNetworkConnection(tokens[1]);
} else {
std::cout << "Invalid command. Usage: connect <ipAddress>\n";
}
} else if (tokens[0] == "send") {
if (tokens.size() >= 2) {
sendData(tokens[1]);
} else {
std::cout << "Invalid command. Usage: send <data>\n";
}
} else if (tokens[0] == "receive") {
receiveData();
} else if (tokens[0] == "protocol") {
if (tokens.size() >= 2) {
implementProtocol(tokens[1]);
} else {
std::cout << "Invalid command. Usage: protocol <protocol>\n";
}
} else if (tokens[0] == "interfaces") {
manageNetworkInterfaces();
} else if (tokens[0] == "authenticate") {
if (tokens.size() >= 3) {
authenticateUser(tokens[1], tokens[2]);
} else {
std::cout << "Invalid command. Usage: authenticate <username> <password>\n";
}
} else if (tokens[0] == "authorize") {
if (tokens.size() >= 3) {
authorizeUser(tokens[1], tokens[2]);
} else {
std::cout << "Invalid command. Usage: authorize <username> <permission>\n";
}
} else if (tokens[0] == "encrypt") {
if (tokens.size() >= 2) {
encryptFile(tokens[1]);
} else {
std::cout << "Invalid command. Usage: encrypt <filename>\n";
}
} else if (tokens[0] == "decrypt") {
if (tokens.size() >= 2) {
decryptFile(tokens[1]);
} else {
std::cout << "Invalid command. Usage: decrypt <filename>\n";
}
} else if (tokens[0] == "configure") {
configureSystem();
} else if (tokens[0] == "users") {
manageUsers();
} else if (tokens[0] == "install") {
if (tokens.size() >= 2) {
installSoftware(tokens[1]);
} else {
std::cout << "Invalid command. Usage: install <softwareName>\n";
}
} else if (tokens[0] == "backup") {
backupSystem();
} else if (tokens[0] == "restore") {
restoreSystem();
} else if (tokens[0] == "errors") {
handleErrors();
} else if (tokens[0] == "datetime") {
manageDateTime();
} else if (tokens[0] == "power") {
managePower();
} else if (tokens[0] == "settings") {
manageSettings();
} else if (tokens[0] == "gui") {
handleGUI();
} else if (tokens[0] == "cli") {
handleCLI();
} else if (tokens[0] == "exit") {
break;
} else {
std::cout << "Invalid command. Type 'help' to see the list of available commands.\n";
}
}
return 0;
}
// Function definitions
void displayCommands() {
std::cout << "Available commands:\n"
<< "help\n"
<< "create <filename>\n"
<< "delete <filename>\n"
<< "open <filename>\n"
<< "read <filename>\n"
<< "write <filename>\n"
<< "append <filename>\n"
<< "rename <oldname> <newname>\n"
<< "copy <source> <destination>\n"
<< "move <source> <destination>\n"
<< "permissions <filename>\n"
<< "info <filename>\n"
<< "list <directory>\n"
<< "navigate <directory>\n"
<< "createprocess <processName>\n"
<< "terminate <processName>\n"
<< "suspend <processName>\n"
<< "resume <processName>\n"
<< "getinfo <processName>\n"
<< "schedule\n"
<< "allocate <processName> <size>\n"
<< "free <processName>\n"
<< "input\n"
<< "output <text>\n"
<< "interrupts\n"
<< "connect <ipAddress>\n"
<< "send <data>\n"
<< "receive\n"
<< "protocol <protocol>\n"
<< "interfaces\n"
<< "authenticate <username> <password>\n"
<< "authorize <username> <permission>\n"
<< "encrypt <filename>\n"
<< "decrypt <filename>\n"
<< "configure\n"
<< "users\n"
<< "install <softwareName>\n"
<< "backup\n"
<< "restore\n"
<< "errors\n"
<< "datetime\n"
<< "power\n"
<< "settings\n"
<< "gui\n"
<< "cli\n"
<< "exit\n";
}
void createFile(const std::string& filename) {
std::ofstream file(filename);
if (file) {
std::cout << "File created successfully.\n";
file.close();
} else {
std::cout << "Failed to create file.\n";
}
}
void deleteFile(const std::string& filename) {
if (std::remove(filename.c_str()) == 0) {
std::cout << "File deleted successfully.\n";
} else {
std::cout << "Failed to delete file.\n";
}
}
void openFile(const std::string& filename) {
std::ifstream file(filename);
if (file) {
std::cout << "File opened successfully.\n";
// Read and process the file contents if needed
file.close();
} else {
std::cout << "Failed to open file.\n";
}
}
void readFile(const std::string& filename) {
std::ifstream file(filename);
if (file) {
std::string line;
while (std::getline(file, line)) {
std::cout << line << '\n';
}
file.close();
} else {
std::cout << "Failed to read file.\n";
}
}
void writeFile(const std::string& filename) {
std::ofstream file(filename);
if (file) {
std::string line;
std::cout << "Enter text. Press Ctrl+D (Unix) or Ctrl+Z (Windows) to finish:\n";
while (std::getline(std::cin, line)) {
file << line << '\n';
}
file.close();
std::cout << "File written successfully.\n";
} else {
std::cout << "Failed to write file.\n";
}
}
void appendToFile(const std::string& filename) {
std::ofstream file(filename, std::ios_base::app);
if (file) {
std::string line;
std::cout << "Enter text to append. Press Ctrl+D (Unix) or Ctrl+Z (Windows) to finish:\n";
while (std::getline(std::cin, line)) {
file << line << '\n';
}
file.close();
std::cout << "File appended successfully.\n";
} else {
std::cout << "Failed to append to file.\n";
}
}
void renameFile(const std::string& oldname, const std::string& newname) {
if (std::rename(oldname.c_str(), newname.c_str()) == 0) {
std::cout << "File renamed successfully.\n";
} else {
std::cout << "Failed to rename file.\n";
}
}
void copyFile(const std::string& source, const std::string& destination) {
std::ifstream src(source, std::ios::binary);
std::ofstream dst(destination, std::ios::binary);
if (src && dst) {
dst << src.rdbuf();
std::cout << "File copied successfully.\n";
} else {
std::cout << "Failed to copy file.\n";
}
}
void moveFile(const std::string& source, const std::string& destination) {
if (std::rename(source.c_str(), destination.c_str()) == 0) {
std::cout << "File moved successfully.\n";
} else {
std::cout << "Failed to move file.\n";
}
}
void changePermissions(const std::string& filename) {
if (std::chmod(filename.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == 0) {
std::cout << "File permissions changed successfully.\n";
} else {
std::cout << "Failed to change file permissions.\n";
}
}
void getFileInformation(const std::string& filename) {
struct stat fileStat;
if (stat(filename.c_str(), &fileStat) == 0) {
std::cout << "File information:\n"
<< "Size: " << fileStat.st_size << " bytes\n"
<< "Creation time: " << ctime(&fileStat.st_ctime)
<< "Last access time: " << ctime(&fileStat.st_atime)
<< "Last modification time: " << ctime(&fileStat.st_mtime) << '\n';
} else {
std::cout << "Failed to get file information.\n";
}
}
void listFilesInDirectory(const std::string& directory) {
DIR* dir;
struct dirent* ent;
if ((dir = opendir(directory.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
std::cout << ent->d_name << '\n';
}
closedir(dir);
} else {
std::cout << "Failed to list files in directory.\n";
}
}
void navigateDirectories(const std::string& directory) {
if (chdir(directory.c_str()) == 0) {
std::cout << "Navigated to directory: " << directory << '\n';
} else {
std::cout << "Failed to navigate to directory.\n";
}
}
void createProcess(const std::string& processName) {
// Code to create a new process
std::cout << "Process created: " << processName << '\n';
}
void terminateProcess(const std::string& processName) {
// Code to terminate a process
std::cout << "Process terminated: " << processName << '\n';
}
void suspendProcess(const std::string& processName) {
// Code to suspend a process
std::cout << "Process suspended: " << processName << '\n';
}
void resumeProcess(const std::string& processName) {
// Code to resume a process
std::cout << "Process resumed: " << processName << '\n';
}
void getProcessInformation(const std::string& processName) {
// Code to get process information
std::cout << "Process information for: " << processName << '\n';
}
void scheduleProcesses() {
// Code to schedule processes
std::cout << "Processes scheduled.\n";
}
void allocateMemory(const std::string& processName, int size) {
// Code to allocate memory for a process
std::cout << "Memory allocated for process: " << processName << ", size: " << size << " bytes\n";
}
void freeMemory(const std::string& processName) {
// Code to free allocated memory for a process
std::cout << "Memory freed for process: " << processName << '\n';
}
void handleUserInput() {
// Code to handle user input
std::cout << "Waiting for user input...\n";
}
void displayOutput(const std::string& text) {
// Code to display output
std::cout << "Output: " << text << '\n';
}
void handleSystemInterrupts() {
// Code to handle system interrupts
std::cout << "System interrupts handled.\n";
}
void establishNetworkConnection(const std::string& ipAddress) {
// Code to establish network connection
std::cout << "Network connection established. IP Address: " << ipAddress << '\n';
}
void sendData(const std::string& data) {
// Code to send data over the network
std::cout << "Data sent: " << data << '\n';
}
void receiveData() {
// Code to receive data over the network
std::cout << "Data received.\n";
}
void implementNetworkProtocol(const std::string& protocol) {
// Code to implement network protocol
std::cout << "Network protocol implemented: " << protocol << '\n';
}
void manageNetworkInterfaces() {
// Code to manage network interfaces
std::cout << "Network interfaces managed.\n";
}
void authenticateUser(const std::string& username, const std::string& password) {
// Code to authenticate user
std::cout << "User authenticated: " << username << '\n';
}
void authorizeUser(const std::string& username, const std::string& permission) {
// Code to authorize user
std::cout << "User authorized: " << username << ", permission: " << permission << '\n';
}
void encryptFile(const std::string& filename) {
// Code to encrypt file
std::cout << "File encrypted: " << filename << '\n';
}
void decryptFile(const std::string& filename) {
// Code to decrypt file
std::cout << "File decrypted: " << filename << '\n';
}
void configureSystem() {
// Code to configure system
std::cout << "System configured.\n";
}
void manageUsers() {
// Code to manage users
std::cout << "Users managed.\n";
}
void installSoftware(const std::string& softwareName) {
// Code to install software
std::cout << "Software installed: " << softwareName << '\n';
}
void backupSystem() {
// Code to backup system
std::cout << "System backed up.\n";
}
void restoreSystem() {
// Code to restore system
std::cout << "System restored.\n";
}
void handleErrors() {
// Code to handle errors
std::cout << "Errors handled.\n";
}
void manageDateTime() {
// Code to manage date and time
std::cout << "Date and time managed.\n";
}
void managePower() {
// Code to manage power
std::cout << "Power managed.\n";
}
void manageSettings() {
// Code to manage system settings
std::cout << "System settings managed.\n";
}
void handleGUI() {
// Code to handle GUI
std::cout << "GUI handled.\n";
}
void handleCLI() {
// Code to handle CLI
std::cout << "CLI handled.\n";
}