1 #include "Headers/opsys.h"
3 #include <sys/param.h> // MAXPATHLEN
4 #include <sys/wait.h> // waitpid
5 #include <unistd.h> // getcwd, fork, chdir, geuid, gethostname, execvp
6 #include <pwd.h> // struct passwd, getpwuid_r
12 OpSys OS
; // Global OpSys variable. Should be accessible to all modules.
13 bool exit_program
= false;
15 void signal_handler(int s
)
19 cout
<< "\nSIGINT (Ctrl+C) received. Code " << s
<< ". Exiting.\n";
20 exit_program
= true; // We'll do this instead of an exit(0) in order to allow for cleanup.
25 cout
<< "\nSIGHUP received. Code " << s
<< ". Exiting.\n";
26 exit_program
= true; // We'll do this instead of an exit(0) in order to allow for cleanup.
30 SignalHandler::SignalHandler()
32 signal_action
.sa_handler
= signal_handler
;
33 sigemptyset(&signal_action
.sa_mask
);
34 signal_action
.sa_flags
= 0;
35 sigaction(SIGINT
, &signal_action
, NULL
);
38 string
OpSys::get_cwd()
40 char temp
[MAXPATHLEN
];
41 return getcwd(temp
, sizeof(temp
)) ? std::string( temp
) : std::string("");
44 void OpSys::change_dir(vector
<string
> command
)
47 for(unsigned short int i
= 1; i
< command
.size(); i
++)
51 if (chdir(cmd
.c_str()))
53 cerr
<< "miniShell: cd: " << cmd
<< ": Arquivo ou diretório não encontrado\n";
61 vector
<const char*> make_argv(vector
<string
>const& in
)
63 vector
<const char*> out
;
64 out
.reserve(in
.size() + 1);
65 for (const auto& s
: in
)
66 out
.push_back(s
.data());
67 out
.push_back(nullptr);
72 short OpSys::simple_command(vector
<string
> tokens
)
77 cerr
<< "execvp(\"" << tokens
[0] << "\",\"";
78 for(unsigned int i
= 0; i
< tokens
.size()-1; i
++)
79 cerr
<< tokens
[i
] << ' ';
80 cerr
<< tokens
.back();
88 //printf("execlp(%s, %s, NULL);", bincmd.c_str(), command.c_str());
89 execvp(tokens
[0].c_str(), const_cast<char* const *>(make_argv(tokens
).data()));
90 cerr
<< tokens
[0] << ": command not found.\n";
91 return 0; // TODO: exit with cleanup
95 printf("Couldn't fork a process.");
98 waitpid(-1, &status
, WUNTRACED
);
106 cerr
<< RED_ANSI
<< "Could not get the current working directory.\n" << RESET_ANSI
;
108 uid_t uid
= geteuid(); // Gets the effective ID of the user that started miniSHELL
110 struct passwd
*pwent_ptr
;
113 // Looks for the UDI on the password databank and saves the result on pwent
114 getpwuid_r(uid
, &pwent
, buffer
, sizeof buffer
, &pwent_ptr
);
115 username
= pwent
.pw_name
; // Saves username
118 gethostname(hostname
, 64);
119 this->hostname
= hostname
;