-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cpp
More file actions
865 lines (737 loc) · 25.8 KB
/
Commands.cpp
File metadata and controls
865 lines (737 loc) · 25.8 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
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <sys/wait.h>
#include <iomanip>
#include "Commands.h"
#include <cstring>
#include <regex>
#include <cstddef>
#include <thread>
#include <fcntl.h>
#include <cmath>
#include <experimental/filesystem>
#include <sys/stat.h>
// TODO :: ADD FOR THE MAKE DANIEL MADE , REPLACE TO .H FOR CLION CMAKE
#include "helper.cpp"
#include "CommandLittleHelprs.cpp"
#include "executeHelper.cpp"
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
using namespace std;
//const std::string WHITESPACE = " \n\r\t\f\v";
// TODO : NEED TO ADD SPECIAL COMMANDS AS WELL - LATER
//const std::vector<std::string> RESERVED_COMMANDS = {"chprompt", "showpid", "pwd", "cd", "jobs", "fg",
// "quit", "kill", "alias", "unalias", "unsetenv", "watchproc"};
#if 0
#define FUNC_ENTRY() \
cout << __PRETTY_FUNCTION__ << " --> " << endl;
#define FUNC_EXIT() \
cout << __PRETTY_FUNCTION__ << " <-- " << endl;
#else
#define FUNC_ENTRY()
#define FUNC_EXIT()
#endif
// Useful 'side' function
// TODO: Add your implementation for classes in Commands.h
//Command::Command(const char *cmd_line) {
//};
//SmallShell::SmallShell() {
// // TODO: add your implementation
// //m_promptName = "smash";
//}
SmallShell::~SmallShell() {
// TODO: add your implementation
}
std::unique_ptr<Command> SmallShell::createCommandHandler(string args_cmd[], const char* og_cmd, int numOfArgs, bool is_bg_cmd, int is_pipe_cmd) {
if (is_pipe_cmd){
bool is_err = is_pipe_cmd != 1;
int pipe_place = findPipePlacement(args_cmd, numOfArgs);
return make_unique<PipeCommand>(args_cmd, numOfArgs, *this, pipe_place, is_err);
}
else if(args_cmd[0] == "chprompt"){
return make_unique<Chprompt>(args_cmd, numOfArgs, *this);
}
else if(args_cmd[0] == "showpid"){
return make_unique<Showpid>(args_cmd, numOfArgs, *this);
}
else if(args_cmd[0] == "cd"){
return make_unique<Cd>(args_cmd, numOfArgs, *this);
}
else if(args_cmd[0] == "pwd"){
return make_unique<Pwd>(args_cmd, numOfArgs, *this);
}
else if(args_cmd[0] == "alias"){
return build_alias(args_cmd,numOfArgs);
}
else if(args_cmd[0] == "unalias"){
return make_unique<UnAliasCommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "unsetenv") {
return make_unique<UnSetEnvCommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "watchproc") {
return make_unique<WatchProcCommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "jobs") {
return make_unique<JobsCommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "fg") {
return make_unique<ForegroundCommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "quit") {
return make_unique<QuitCommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "kill") {
return make_unique<KillCommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "du") {
return make_unique<DiskUsageCommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "whoami") {
return make_unique<WhoAmICommand>(args_cmd, numOfArgs, *this);
}
else if (args_cmd[0] == "netinfo"){
return make_unique<NetInfo>(args_cmd, numOfArgs, *this);
}
else {
return make_unique<ExternalCommand>(args_cmd, og_cmd, numOfArgs, *this, is_bg_cmd);
}
return nullptr;
}
std::unique_ptr<Command> SmallShell::CreateCommand(const char *cmd_line) {
std::size_t len = std::strlen(cmd_line); //get len of the cmd line
char* copy_cmd_line = new char[len + 1]; //copy the cmd line for usege
std::strcpy(copy_cmd_line, cmd_line);
char* char_args_cmd[COMMAND_MAX_ARGS];
string args_cmd[COMMAND_MAX_ARGS]; // Full command for non-alias command
_removeBackgroundSign(copy_cmd_line);
std::size_t numOfArgs = _parseCommandLine(copy_cmd_line, char_args_cmd); // Working without the & incase it's internal command
charPtrArrayToStringArray(char_args_cmd, args_cmd, numOfArgs); // Working with strings - easier
auto mapped_cmd = this->m_name_to_cmd.find(args_cmd[0]);
if(mapped_cmd != this->m_name_to_cmd.end()){
std::vector<std::string> alias_args_cmd_v = CreateCommandVectorAlias(args_cmd, numOfArgs);
std::string alias_args_cmd[COMMAND_MAX_ARGS];
std::string* tmp = vectorToStringArray(alias_args_cmd_v);
for (size_t i = 0; i < COMMAND_MAX_ARGS && i < alias_args_cmd_v.size(); ++i){
alias_args_cmd[i] = tmp[i];
}
delete[] tmp;
int start = _parseCommandLine(cmd_line, char_args_cmd);
return CreateCommandAndRedirect(alias_args_cmd, numOfArgs, cmd_line, start - 1);
}
else{
return CreateCommandAndRedirect(args_cmd, numOfArgs,cmd_line, 0);
}
}
void SmallShell::executeCommand(const char *cmd_line) {
// TODO: Add your implementation here
if(*cmd_line == '\0'){
return;
}
std::unique_ptr<Command> cmd = CreateCommand((cmd_line));
if (cmd != nullptr) {
this->getJobsList().removeFinishedJobs();
cmd->execute();
}
this->restoreStdOut();
}
// Implementing execute for all the commands:
void Chprompt::execute() {
string cmd_args[COMMAND_MAX_ARGS];
for(int i = 0; i < COMMAND_MAX_ARGS; ++i){
cmd_args[i] = this->getCmdArgs()[i];
}
if(cmd_args[1].empty()){ //if it's nullptr
m_small_shell.setPromptName("smash");
}
else {
this->m_small_shell.setPromptName(cmd_args[1]);
}
}
void Showpid::execute() {
std::cout << "smash pid is " << getpid() << std::endl;
}
void Pwd::execute() {
char* cwd = getcwd(nullptr, 0); // Dynamically allocates space for cwd
std::cout << cwd << std::endl;
}
void Cd::execute() {
std::string specialCommand = "-";
int numOfArgs = this->getNumOfArgs();
if(numOfArgs > 2){
std::cerr << "smash error: cd: too many arguments" << std::endl;
}
else if(numOfArgs == 2){
if(this->getCmdArgs()[1] == specialCommand){
if(this->m_small_shell.getlastWorkingDir() == "\n"){
std::cerr << "smash error: cd: OLDPWD not set" << std::endl;
}
else {
std::string currDir = getcwd(nullptr, 0); //dynamically allocate buffer for cwd
if (chdir(this->m_small_shell.getlastWorkingDir().c_str()) >= 0){ // make sure the syscall did not fail
this->m_small_shell.setlastWorkingDir(currDir);
}
else {
std::perror("smash error: chdir failed");
}
}
}
else{
std::string currDir = getcwd(nullptr, 0); //dynamically allocate buffer for cwd
this->m_small_shell.setlastWorkingDir(currDir);
if (chdir(this->getCmdArgs()[1].c_str()) < 0) {
std::perror("smash error: chdir failed");
}
}
}
}
void AliasCommand::execute() {
if(this->getCmdArgs()[1].empty()){ // printing all the alias's the user created
for (const string& alias : this->m_small_shell.getOrder()) {
std::cout << alias << "='" << this->m_small_shell.getMap()[alias] << "'" << std::endl;
}
return;
}
this->m_small_shell.addAlias(this->getCmd(), this->getAlias());
}
void UnAliasCommand::execute(){
int numOfArgs = this->getNumOfArgs();
if (numOfArgs < 2) {
std::cerr << "smash error: unalias: not enough arguments" << std::endl;
}
else {
removeAllAlias(numOfArgs);
}
}
void UnSetEnvCommand::execute() {
std::vector<char> environ_vector = environ_file_to_vector();
std::vector<std::string> env_names = extract_env_var_names(environ_vector);
// Now we have an array of the names of the vars
if (this->getNumOfArgs() > 1) {
for (int i = 1; i < this->getNumOfArgs(); i++) {
bool exists = std::find(env_names.begin(), env_names.end(), this->getCmdArgs()[i]) != env_names.end();
if (exists) {
remove_env_var(this->getCmdArgs()[i]);
}
else {
std::cerr << "smash error: unsetenv: " << this->getCmdArgs()[i] << " does not exist" << std::endl;
break;
}
}
}
else {
std::cerr << "smash error: unsetenv: not enough arguments" << std::endl;
}
}
double WatchProcCommand::calculateCpuUsege(int pid){
long hz = sysconf(_SC_CLK_TCK);
uint64_t total_cpu_time_0 = get_total_cpu_time();
uint64_t process_cpu_time_0;
try {
process_cpu_time_0 = get_process_cpu_time(pid);
}
catch (...) {
std::cerr << "smash error: watchproc: pid " << pid << " does not exist" << std::endl;
return -1;
}
struct timespec duration{};
duration.tv_sec = 1;
duration.tv_nsec = 0;
if(nanosleep(&duration, nullptr) == -1){
std::perror("smash error: nanosleep failed");
}
uint64_t total_cpu_time_1 = get_total_cpu_time();
uint64_t process_cpu_time_1 = get_process_cpu_time(pid);
uint64_t delta_total = total_cpu_time_1 - total_cpu_time_0;
uint64_t delta_process = process_cpu_time_1 - process_cpu_time_0;
return ((double(delta_process) / hz) / (double (delta_total) / hz)) * 100.0;
}
void WatchProcCommand::execute() {
if (this->getNumOfArgs() == 2) {
int pid;
try {
pid = atoi(this->getCmdArgs()[1].c_str());
}
catch(...){
std::cerr << "smash error: watchproc: invalid arguments" << std::endl;
}
double cpu_usage = calculateCpuUsege(pid);
if(cpu_usage == -1){
return;
}
double mem_mb = get_mem_mb(pid);
std::cout << "PID: " << pid
<< " | CPU Usage: " << fixed << std::setprecision(1)
<< cpu_usage << "% | Memory Usage: "
<< setprecision(1) << mem_mb <<" MB"
<< std::endl;
}
else {
std::cerr << "smash error: watchproc: invalid arguments" << std::endl;
}
}
void ExternalCommand::execute(){
int num_of_args = this->getNumOfArgs();
bool bg_cmd = this->getIsBgCmd();
bool complex = isComplexCommand(this->getCmdArgs(), num_of_args);
pid_t pid = fork();
if(pid < 0){
std::perror("smash error: fork failed");
return;
}
// Child process
if(pid == 0){
setpgrp(); // Cannot fail
if(!complex) {
std::vector<char*> args;
args.reserve(num_of_args + 1);
for (int i = 0; i < num_of_args; ++i) {
args.push_back(const_cast<char*>(this->getCmdArgs()[i].c_str()));
}
args.push_back(nullptr);
execvp(args[0], args.data());
std::perror("smash error: execvp failed");
}
else{
std::string cmd_line;
for(int i = 0; i < num_of_args - 1; i++){
cmd_line += this->getCmdArgs()[i] + " ";
}
cmd_line += this->getCmdArgs()[num_of_args - 1];
const char* bash = "/bin/bash";
const char* args[] = {"bash", "-c", cmd_line.c_str(), nullptr};
execvp(bash, const_cast<char* const*>((args)));
std::perror("smash error: execvp failed");
}
}
// Parent process
if(pid > 0){
this->m_shell.setFgProcPID(pid);
if (!bg_cmd) {
if(waitpid(pid, nullptr, 0 ) < 0){
std::perror("smash error: waitpid failed");
}
}
else{
this->m_shell.getJobsList().removeFinishedJobs();
this->m_shell.getJobsList().addJob(m_og_cmd, pid);
}
this->m_shell.setFgProcPID(0);
}
}
// Jobs related methods
void JobsList::addJob(const char* cmd, pid_t pid) {
int new_id = this->getMaxId() > 0 ? this->getMaxId() + 1 : 1;
JobEntry new_job = JobEntry(new_id, pid, cmd);
this->getJobsList().push_back(new_job);
this->setMaxId(new_id);
}
void JobsList::printJobsList() {
this->removeFinishedJobs();
for (const JobEntry& job : this->getJobsList()) {
std::cout <<"[" << job.getJobId() << "] " << job.getCommand() << std::endl;
}
}
void JobsCommand::execute() {
this->m_small_shell.getJobsList().printJobsList();
}
void JobsList::removeFinishedJobs() {
std::vector<JobEntry>& jobs_list = this->getJobsList();
for (auto job_it = jobs_list.begin(); job_it < jobs_list.end();) {
int ret = waitpid(job_it->getPid(), nullptr, WNOHANG);
if (ret > 0) { // TODO: CHECK IF IT CAN FAIL
jobs_list.erase(job_it);
if(job_it->getJobId() == this->getMaxId()){
updateMaxJobId();
}
}
else {
job_it++;
}
}
}
JobsList::JobEntry *JobsList::getJobById(int jobId) {
for (JobEntry& job : this->getJobsList()) {
if (job.getJobId() == jobId) {
return &job;
}
}
return nullptr;
}
void JobsList::removeJobById(int jobId) {
for (auto it = this->getJobsList().begin(); it < this->getJobsList().end(); ++it) {
if (it->getJobId() == jobId) {
this->getJobsList().erase(it);
}
}
}
JobsList::JobEntry* JobsList::getLastJob() {
return &*(this->getJobsList().end() - 1);
}
void ForegroundCommand::execute() {
if(this->getNumOfArgs() > 2){
std::cerr << "smash error: fg: invalid arguments" << std::endl;
return;
}
else if (this->getNumOfArgs() < 2) {
executeLastId();
return;
}
else {
executeFgByID();
return;
}
}
void QuitCommand::execute() {
if(this->getCmdArgs()[1] == "kill") {
std::cout << "smash: sending SIGKILL signal to " << this->m_small_shell.getJobsList().getJobsList().size()
<< " jobs:" << std::endl;
for(auto job : this->m_small_shell.getJobsList().getJobsList()){
std::cout << job.getPid() <<": " << job.getCommand() << std::endl;
if(kill(job.getPid(), SIGKILL)){
std::perror("smash error: kill failed");
}
}
}
exit(SIGKILL);
}
void KillCommand::execute() {
if(this->getNumOfArgs() != 3){
std::cerr << "smash error: kill: invalid arguments" << std::endl;
return;
}
if(this->getCmdArgs()[1][0] == '-'){
try{
int signum = atoi(const_cast<char*>(&this->getCmdArgs()[1][1]));
int jobId = stoi(this->getCmdArgs()[2]);
if(jobId <= 0){
std::cerr << "smash error: kill: invalid arguments" << std::endl;
return;
}
auto jobEntry = this->m_small_shell.getJobsList().getJobById(jobId);
if(jobEntry == nullptr){
std::cout << "smash error: kill: job-id " << jobId << " does not exist" << std::endl;
return;
}
int jobPid = jobEntry->getPid();
if(!kill(jobPid, signum)){
std::cout << "signal number " << signum << " was sent to pid " << jobPid << std::endl;
return;
}
else{
perror("smash error: kill failed");
}
} catch(...){
std::cerr << "smash error: kill: invalid arguments" << std::endl;
return;
}
}
std::cerr << "smash error: kill: invalid arguments" << std::endl;
}
void RedirectionCommand::execute() {
int flags = O_CREAT | O_WRONLY | (this->getMode() == ">>" ? O_APPEND : O_TRUNC);
int new_fd = open(this->getPath().c_str(), flags, 0666);
if (new_fd < 0) {
std::perror("smash error: open failed");
}
if (dup2(new_fd, STDOUT_FILENO) < 0) {
std::perror("smash error: open failed");
close(new_fd);
}
}
void PipeRedirectionOut() {
}
void SmallShell::restoreStdOut() {
fflush(stdout);
if (dup2(this->getSavedStdOut(), STDOUT_FILENO) < 0) {
std::perror("smash error: dup failed");
}
}
void SmallShell::restoreStdIn() {
fflush(stdin);
if (dup2(this->getSavedStdIn(), STDIN_FILENO) < 0) {
std::perror("smash error: dup failed");
}
}
void SmallShell::restoreStdErr() {
fflush((stderr));
if (dup2(this->getSavedStdErr(), STDERR_FILENO) < 0) {
std::perror("smash error: dup failed");
}
}
uintmax_t diskUsageWrapper(const std::experimental::filesystem::path& path){
struct stat sb;
// Checks for unreadable folder
if (lstat(path.c_str(), &sb) != 0) {
return 0;
}
// Checks if the path is a symlink - if so, ignore
if (S_ISLNK(sb.st_mode)) {
return 0;
}
uintmax_t disk_usage = sb.st_blocks;
if (S_ISDIR(sb.st_mode)) {
for (auto const& entry : std::experimental::filesystem::directory_iterator(path)) {
disk_usage += diskUsageWrapper(entry.path());
}
}
return disk_usage;
}
void DiskUsageCommand::execute() {
std::string path;
struct stat sb;
if(this->getNumOfArgs() == 1){
path = getcwd(nullptr, 0);
}
else {
path = this->getCmdArgs()[1];
}
if (stat(path.c_str(), &sb) != 0){
std::cerr << "smash error: du: directory " << path << " does not exist" << std::endl;
return;
}
if(this->getNumOfArgs() > 2){
std::cerr << "smash error: du: too many arguments" << std::endl;
return;
}
std::cout << "Total disk usage: " << std::ceil(diskUsageWrapper(path) / 2) <<" KB" << std::endl;
}
void WhoAmICommand::execute() {
uid_t user_id = getuid();
std::string content;
try {
readFileContent("/etc/passwd", content);
} catch (const std::exception& ex) {
std::perror("readFileContent failed");
return;
}
std::string user, path;
for (auto& line : splitLines(content)) {
size_t field_start = 0, field_end;
// username
field_end = line.find(':', field_start);
if (field_end == std::string::npos) continue;
std::string name = line.substr(field_start, field_end - field_start);
// skip password
field_start = field_end + 1;
field_end = line.find(':', field_start);
if (field_end == std::string::npos) continue;
// uid field
field_start = field_end + 1;
field_end = line.find(':', field_start);
if (field_end == std::string::npos) continue;
std::string uid_str = line.substr(field_start, field_end - field_start);
// match against current UID
if (static_cast<uid_t>(std::stoi(uid_str)) != user_id)
continue;
// skip gid and gecos (3 more colons)
// TODO: maybe till i < 2 ?
for (int i = 0; i < 3; ++i) {
field_start = field_end + 1;
field_end = line.find(':', field_start);
if (field_end == std::string::npos) break;
}
// home directory
if (field_start < line.size()) {
field_end = line.find(':', field_start);
if (field_end == std::string::npos)
path = line.substr(field_start);
else
path = line.substr(field_start, field_end - field_start);
}
user = name;
break;
}
if (!user.empty()) {
std::cout << user << " " << path << "\n";
}
}
void PipeCommand::execute() {
if (!this->getIsErr()) {
this->executePipeHandler(STDOUT_FILENO);
}
else {
this->executePipeHandler(STDERR_FILENO);
}
}
void PipeCommand::executePipeHandler(int fd){
int my_pipe[2];
pipe(my_pipe);
pid_t pid = fork();
if (pid == 0) {
close(my_pipe[0]);
std::string cmd_1_args;
for (int i = 0; i < getPipePlace(); i++) {
cmd_1_args += this->getCmdArgs()[i] + " ";
}
if (dup2(my_pipe[1], fd) < 0) {
std::perror("smash error: dup failed");
}
std::unique_ptr<Command> cmd_1 = this->m_small_shell.CreateCommand(cmd_1_args.c_str());
if (cmd_1 != nullptr) {
cmd_1->execute();
}
this->m_small_shell.restoreStdOut();
this->m_small_shell.restoreStdErr();
_exit(1);
}
else {
if (waitpid(pid, nullptr, 0 ) < 0) {
perror("smash error: waitpid failed");
}
close(my_pipe[1]);
std::string cmd_2_s;
for (int i = this->getPipePlace() + 1; i < this->getNumOfArgs(); i++) {
cmd_2_s += this->getCmdArgs()[i] + " ";
}
if (dup2(my_pipe[0], STDIN_FILENO) < 0) {
perror("smash error: dup failed");
}
std::unique_ptr<Command> cmd_2 = this->m_small_shell.CreateCommand(cmd_2_s.c_str());
cmd_2->execute();
this->m_small_shell.restoreStdIn();
}
}
void NetInfo::execute() {
if (this->getNumOfArgs() < 2) {
std::cerr << "smash error: netinfo: interface not specified" << std:: endl;
return;
}
// Creates a dummy socket
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("smash error: socket failed");
return;
}
// Struct ifreq will hold the information needed
struct ifreq ifr{};
strncpy(ifr.ifr_name, this->getCmdArgs()[1].c_str(), IFNAMSIZ);
// Get IP address
if (ioctl(fd, SIOCGIFADDR, &ifr) >= 0) {
struct sockaddr_in* ip = (struct sockaddr_in*)&ifr.ifr_addr;
std::cout << "IP Address: " << inet_ntoa(ip->sin_addr) << std::endl;
}
else {
perror("smash error: ioctl failed");
return;
}
// Get subnet mask
if (ioctl(fd, SIOCGIFNETMASK, &ifr) == 0) {
struct sockaddr_in* ip = (struct sockaddr_in*)&ifr.ifr_netmask;
std::cout << "Subnet Mask: " << inet_ntoa(ip->sin_addr) << std::endl;
}
else {
perror("smash error: ioctl failed");
return;
}
close(fd);
std::string gateway; // result buffer
int fd_gateway = open("/proc/net/route", O_RDONLY);
if (fd_gateway < 0) {
perror("smash error: open /proc/net/route");
} else {
// read the entire file into a stack buffer (it's small)
char buf[8192];
ssize_t tot = 0, n;
while ((n = read(fd_gateway, buf + tot, sizeof(buf) - tot - 1)) > 0) {
tot += n;
if (tot >= (ssize_t)sizeof(buf) - 1) break;
}
if (n < 0) perror("smash error: read /proc/net/route");
buf[tot] = '\0';
close(fd);
// walk line by line
char *line = buf, *next;
// skip header
if ((next = strchr(line, '\n'))) line = next + 1;
while (line && *line) {
char *p = line;
char *f_iface = p;
while (*p && *p!=' ' && *p!='\t') ++p;
bool hasIfaceField = (*p==' '||*p=='\t');
if (hasIfaceField) *p++ = '\0';
while (*p==' '||*p=='\t') ++p;
char *f_dest = p;
while (*p && *p!=' ' && *p!='\t') ++p;
bool hasDestField = (*p==' '||*p=='\t');
if (hasDestField) *p++ = '\0';
while (*p==' '||*p=='\t') ++p;
char *f_gate = p;
while (*p && *p!=' ' && *p!='\t' && *p!='\n') ++p;
char saved = *p;
*p = '\0';
// check for default route on our interface
if (hasIfaceField
&& strcmp(f_iface, this->getCmdArgs()[1].c_str()) == 0
&& strcmp(f_dest, "00000000") == 0)
{
// parse f_gate as little-endian hex
unsigned long gw = 0;
for (char* h = f_gate; *h; ++h) {
char c = *h;
unsigned digit = (c>='0'&&c<='9' ? c-'0'
: (c>='A'&&c<='F' ? c-'A'+10
: (c>='a'&&c<='f' ? c-'a'+10 : 0)));
gw = (gw << 4) | digit;
}
// extract bytes
unsigned b0 = (gw ) & 0xFF;
unsigned b1 = (gw >> 8) & 0xFF;
unsigned b2 = (gw >> 16) & 0xFF;
unsigned b3 = (gw >> 24) & 0xFF;
// format "d.d.d.d" manually
char tmp[16];
int len = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
b0, b1, b2, b3);
gateway.assign(tmp, len);
break;
}
// restore and advance to next line
*p = saved;
line = strchr(line, '\n');
if (line) line++;
}
}
std::cout << "Default Gateway: " << gateway <<std::endl;
std::string content;
try{
readFileContent("/etc/resolv.conf", content);
} catch (...) {
std::perror("smash error: read failed");
return;
}
std::vector<std::string> dns;
for (auto& line : splitLines(content)) {
// skip empty lines or comments
if (line.empty() || line[0] == '#')
continue;
// look for “nameserver” at the start
if (line.rfind("nameserver", 0) != 0)
continue;
// find first space/tab after the keyword
size_t key_end = line.find_first_of(" \t", /*start=*/0 + strlen("nameserver"));
if (key_end == std::string::npos)
continue;
// skip any additional whitespace to the IP
size_t ip_start = line.find_first_not_of(" \t", key_end);
if (ip_start == std::string::npos)
continue;
// find end of IP token
size_t ip_end = line.find_first_of(" \t", ip_start);
std::string ip = (ip_end == std::string::npos
? line.substr(ip_start)
: line.substr(ip_start, ip_end - ip_start));
dns.push_back(ip);
}
// print comma-separated, with label
for (size_t i = 0; i + 1 < dns.size(); ++i) {
std::cout << "DNS Servers: " << dns[i] << ", ";
}
std::cout << "DNS Servers: " << dns.back() << std::endl;
}