-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
596 lines (494 loc) · 15.6 KB
/
utils.cpp
File metadata and controls
596 lines (494 loc) · 15.6 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
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
#include "headers.h"
using namespace std;
void appendToHistory(const string& command) {
static const size_t maxHistorySize = 20;
static const string historyFile = "/Users/priyanshusharma/Documents/AOS_Assignment2/history.txt";
deque<string> history;
ifstream inFile(historyFile);
string line;
while (getline(inFile, line)) {
history.push_back(line);
}
inFile.close();
if (!history.empty() && history.back() == command) {
return;
}
if (history.size() >= maxHistorySize) {
history.pop_front();
}
history.push_back(command);
std::ofstream outFile(historyFile);
for (const auto& entry : history) {
outFile << entry << std::endl;
}
outFile.close();
}
void printHistory(size_t num) {
static const string historyFile = "/Users/priyanshusharma/Documents/AOS_Assignment2/history.txt";
ifstream inFile(historyFile);
string line;
deque<string> history;
while (getline(inFile, line)) {
history.push_back(line);
}
inFile.close();
size_t count = 0;
for (auto it = history.rbegin(); it != history.rend(); ++it) {
if (count >= num) break;
cout << *it << endl;
++count;
}
}
string getUserName() {
struct passwd *pw = getpwuid(getuid());
return string(pw->pw_name);
}
string getCWD() {
char *cwd = getcwd(NULL, 0);
string currentDir(cwd);
free(cwd);
return currentDir;
}
string getSysName() {
struct utsname sys_info;
if (uname(&sys_info) == 0) {
return string(sys_info.nodename);
} else {
return string();
}
}
string removeSpaces(string input) {
size_t start = 0;
while (start < input.size() && (isspace(input[start]) || input[start] == '\n')) {
start++;
}
size_t end = input.size();
while (end > start && (isspace(input[end - 1]) || input[end - 1] == '\n')) {
end--;
}
return input.substr(start, end - start);
}
bool searchFile(const string& filename, const string& startDir) {
queue<string> dirs;
dirs.push(startDir);
while (!dirs.empty()) {
string currentDir = dirs.front();
dirs.pop();
DIR* dir = opendir(currentDir.c_str());
if (!dir) {
perror(("search: cannot access " + currentDir).c_str());
continue;
}
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
string entryName = entry->d_name;
if (entryName == "." || entryName == "..") {
continue;
}
string fullPath = currentDir + "/" + entryName;
if (entryName == filename) {
closedir(dir);
return true;
}
if (entry->d_type == DT_DIR) {
dirs.push(fullPath);
}
}
closedir(dir);
}
return false;
}
void listDirectory(const string& path, bool showAll, bool showLong) {
DIR* dir = opendir(path.c_str());
if (!dir) {
perror(("ls: cannot access " + path).c_str());
return;
}
struct dirent* entry;
vector<string> files;
while ((entry = readdir(dir)) != nullptr) {
if (!showAll && entry->d_name[0] == '.') {
continue;
}
files.push_back(entry->d_name);
}
closedir(dir);
if (showLong) {
struct stat fileStat;
for (const auto& file : files) {
string fullPath = path + "/" + file;
if (stat(fullPath.c_str(), &fileStat) < 0) {
perror(("stat error for " + file).c_str());
continue;
}
// File type and permissions
cout << ((S_ISDIR(fileStat.st_mode)) ? "d" : "-");
cout << ((fileStat.st_mode & S_IRUSR) ? "r" : "-");
cout << ((fileStat.st_mode & S_IWUSR) ? "w" : "-");
cout << ((fileStat.st_mode & S_IXUSR) ? "x" : "-");
cout << ((fileStat.st_mode & S_IRGRP) ? "r" : "-");
cout << ((fileStat.st_mode & S_IWGRP) ? "w" : "-");
cout << ((fileStat.st_mode & S_IXGRP) ? "x" : "-");
cout << ((fileStat.st_mode & S_IROTH) ? "r" : "-");
cout << ((fileStat.st_mode & S_IWOTH) ? "w" : "-");
cout << ((fileStat.st_mode & S_IXOTH) ? "x" : "-");
cout << " " << fileStat.st_nlink;
cout << " " << setw(8) << fileStat.st_uid;
cout << " " << setw(8) << fileStat.st_gid;
cout << " " << setw(8) << fileStat.st_size;
cout << " " << ctime(&fileStat.st_mtime);
cout << " " << file << endl;
}
} else {
for (const auto& file : files) {
cout << file << " ";
}
cout << endl;
}
}
void processEchoCommand(string& command) {
regex rgx(R"((\"[^\"]*\")|(\S+))");
smatch match;
string toPrint = "";
string::const_iterator searchStart(command.cbegin());
while (regex_search(searchStart, command.cend(), match, rgx)) {
string part = match[1];
if (part.front() == '"' && part.back() == '"') {
part = part.substr(1, part.size() - 2);
}
toPrint += part + " ";
searchStart = match.suffix().first;
}
if (!toPrint.empty()) {
toPrint.pop_back();
}
cout << toPrint << endl;
}
string getMemoryUsage(pid_t pid) {
proc_taskinfo taskInfo;
int result = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskInfo, sizeof(taskInfo));
if (result <= 0) {
return "Unable to get memory usage";
}
long long memoryUsageKB = taskInfo.pti_virtual_size / 1024;
return to_string(memoryUsageKB) + " KB";
}
string getExecutablePath(pid_t pid) {
char path[PATH_MAX];
int result = proc_pidpath(pid, path, sizeof(path));
if (result <= 0) {
return "Unable to get executable path";
}
return string(path);
}
string getProcessStatus(pid_t pid) {
struct proc_bsdinfo proc;
std::string status;
if (proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, sizeof(proc)) > 0) {
cout << PROC_PIDTBSDINFO << endl;
switch (proc.pbi_status) {
case 1:
status = "R";
break;
case 2:
status = "S";
break;
case 4:
status = "Z";
break;
case 8:
status = "T";
break;
default:
status = "Unknown";
}
pid_t foreground = tcgetpgrp(STDIN_FILENO);
if (foreground == proc.pbi_pgid) {
status += "+";
}
} else {
status = "Unable to get process status";
}
return status;
}
void printProcessInfo(pid_t pid) {
proc_taskinfo taskInfo;
int result = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskInfo, sizeof(taskInfo));
if (result <= 0) {
perror("Failed to get process info");
return;
}
string status = getProcessStatus(pid);
string memoryUsage = getMemoryUsage(pid);
string executablePath = getExecutablePath(pid);
cout << "PID: " << pid << endl;
cout << "STATUS: " << status << endl;
cout << "MEMORY USAGE:" << memoryUsage << endl;
cout << "EXECUTABLE PATH: " << executablePath << endl;
}
void bringToForeground(pid_t pid) {
if (kill(pid, SIGCONT) == -1) {
perror("Failed to bring process to foreground");
return;
}
int status;
waitpid(pid, &status, WUNTRACED);
if (WIFSTOPPED(status)) {
printf("Process %d stopped\n", pid);
} else if (WIFEXITED(status)) {
printf("Process %d exited\n", pid);
}
}
void executeCommand(vector<string> tokens) {
int background = 0;
// Check if the last token is "&"
if (tokens.back() == "&") {
background = 1; // Set the background flag
tokens.pop_back(); // Remove the "&" from the tokens
}
// Convert vector of tokens to array of C strings
vector<char*> args;
for (auto &token : tokens) {
args.push_back(&token[0]);
}
args.push_back(nullptr);
pid_t pid = fork();
if (pid == -1) {
perror("Fork failed");
exit(1);
} else if (pid == 0) {
// Child process: Execute the command
if (execvp(args[0], args.data()) == -1) {
perror("Command execution failed");
exit(1);
}
} else {
// Parent process
if (!background) {
// Wait for the child process to complete if not running in the background
waitpid(pid, NULL, 0);
} else {
cout << "Process running in background priyanshu with PID: " << pid << endl;
}
}
}
void processCommand(string command) {
static string previousDir = getCWD();
vector<string> tokens;
string token;
stringstream ss(command);
while (ss >> token) {
tokens.push_back(token);
}
if (tokens.empty()) {
return;
}
bool runInBackground = false;
if (tokens.back() == "&") {
runInBackground = true;
tokens.pop_back();
}
bool commandSuccess = false;
if (tokens[0] == "cd") {
string newDir;
if (tokens.size() == 1 || tokens[1] == "~") {
newDir = "/Users/priyanshusharma/Documents/AOS_Assignment2";
} else if (tokens[1] == "-") {
newDir = previousDir;
} else {
newDir = tokens[1];
}
string currentDir = getCWD();
if (chdir(newDir.c_str()) != 0) {
perror("chdir failed");
} else {
previousDir = currentDir;
commandSuccess = true;
}
} else if (tokens[0] == "ls") {
bool showAll = false;
bool showLong = false;
vector<string> directories;
for (size_t i = 1; i < tokens.size(); ++i) {
if (tokens[i] == "-a") {
showAll = true;
} else if (tokens[i] == "-l") {
showLong = true;
} else if(tokens[i] == "-la" || tokens[i] == "-al"){
showAll = true;
showLong = true;
} else {
directories.push_back(tokens[i]);
}
}
if (directories.empty()) {
directories.push_back(".");
}
for (const auto& dir : directories) {
cout << dir << ":\n";
listDirectory(dir, showAll, showLong);
}
commandSuccess = true;
} else if (tokens[0] == "echo") {
processEchoCommand(command);
commandSuccess = true;
} else if (tokens[0] == "pwd") {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != nullptr) {
cout << cwd << endl;
commandSuccess = true;
} else {
perror("pwd failed");
}
} else if (tokens[0] == "search") {
if (tokens.size() > 1) {
bool found = searchFile(tokens[1], ".");
if (found) {
cout << "True" << endl;
} else {
cout << "False" << endl;
}
commandSuccess = true;
} else {
cerr << "search: missing argument\n";
}
} else if (tokens[0] == "history") {
if (tokens.size() == 1) {
printHistory(10);
} else if (tokens.size() == 2) {
size_t num = std::stoul(tokens[1]);
printHistory(num);
} else {
cerr << "history: invalid arguments\n";
}
commandSuccess = true;
} else if (tokens[0] == "pinfo") {
if (tokens.size() == 1) {
pid_t pid = getpid(); // Get current process ID
printProcessInfo(pid);
} else if (tokens.size() == 2) {
pid_t pid = stoi(tokens[1]);
printProcessInfo(pid);
} else {
cerr << "pinfo: invalid arguments\n";
}
commandSuccess = true;
} else if(tokens[0] == "fg"){
if(tokens.size() == 2) {
pid_t pid = stoi(tokens[1]);
bringToForeground(pid);
} else {
cerr << "fg: invalid arguments\n";
}
} else {
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
} else if (pid == 0) {
int inFd = -1, outFd = -1;
setsid();
auto it = find(tokens.begin(), tokens.end(), "<");
if (it != tokens.end()) {
string inputFile = *(it + 1);
inFd = open(inputFile.c_str(), O_RDONLY);
if (inFd < 0) {
perror("Failed to open input file");
exit(EXIT_FAILURE);
}
dup2(inFd, STDIN_FILENO);
close(inFd);
tokens.erase(it, it + 2);
}
it = find(tokens.begin(), tokens.end(), ">");
if (it != tokens.end()) {
string outputFile = *(it + 1);
outFd = open(outputFile.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (outFd < 0) {
perror("Failed to open output file");
exit(EXIT_FAILURE);
}
dup2(outFd, STDOUT_FILENO);
close(outFd);
tokens.erase(it, it + 2);
}
vector<char*> args;
for (const auto& arg : tokens) {
args.push_back(const_cast<char*>(arg.c_str()));
}
args.push_back(nullptr);
if (execvp(args[0], args.data()) == -1) {
perror("Command execution failed");
exit(EXIT_FAILURE);
}
} else {
if (runInBackground) {
cout << "Started background process with PID: " << pid << endl;
} else {
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
commandSuccess = true;
}
}
}
}
if (commandSuccess) {
appendToHistory(command);
}
}
CommandNode* parseCommandTree(const string& input) {
stringstream ss(input);
string segment;
CommandNode* root = nullptr;
CommandNode* current = nullptr;
while (getline(ss, segment, '|')) {
CommandNode* node = new CommandNode();
stringstream segmentStream(segment);
segmentStream >> node->command;
string arg;
while (segmentStream >> arg) {
node->args.push_back(arg);
}
if (root == nullptr) {
root = node;
} else {
current->next = node;
}
current = node;
}
return root;
}
void executeCommandTree(CommandNode* root) {
if (!root) return;
int pipefd[2];
pid_t pid;
int in_fd = 0;
while (root != nullptr) {
pipe(pipefd);
if ((pid = fork()) == -1) {
perror("Fork failed");
return;
} else if (pid == 0) {
dup2(in_fd, 0);
if (root->next != nullptr) {
dup2(pipefd[1], 1);
}
close(pipefd[0]);
vector<char*> args;
args.push_back(const_cast<char*>(root->command.c_str()));
for (const string& arg : root->args) {
args.push_back(const_cast<char*>(arg.c_str()));
}
args.push_back(nullptr);
if (execvp(args[0], args.data()) == -1) {
perror("Command execution failed");
exit(EXIT_FAILURE);
}
} else {
wait(nullptr);
close(pipefd[1]);
in_fd = pipefd[0];
root = root->next;
}
}
}