Less idiotic includes
[minishell-2.git] / Sources / opsys.cpp
blobddbe198ac5b1d8f8942ccc38d22061429ad1a70b
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
8 using std::cin;
9 using std::cout;
10 using std::cerr;
12 OpSys OS; // Global OpSys variable. Should be accessible to all modules.
13 bool exit_program = false;
15 void signal_handler(int s)
17 if (s==SIGINT)
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.
23 if (s==SIGHUP)
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)
46 string cmd = "";
47 for(unsigned short int i = 1; i < command.size(); i++)
49 cmd += command[i];
51 if (chdir(cmd.c_str()))
53 cerr << "miniShell: cd: " << cmd << ": Arquivo ou diretório não encontrado\n";
55 else {
56 get_cwd();
57 cwd_changed = true;
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);
68 out.shrink_to_fit();
69 return out;
72 short OpSys::simple_command(vector<string> tokens)
74 int status;
75 if(is_verbose)
77 cerr << "execvp(\"" << tokens[0] << "\",\"";
78 for(unsigned int i = 0; i < tokens.size()-1; i++)
79 cerr << tokens[i] << ' ';
80 cerr << tokens.back();
81 cerr << "\");\n";
85 pid_t pid = fork();
86 if (pid == 0)
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
93 else if (pid < 0)
95 printf("Couldn't fork a process.");
96 return 0;
98 waitpid(-1, &status, WUNTRACED);
99 return 1;
102 OpSys::OpSys()
104 cwd = get_cwd();
105 if(cwd.empty())
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
109 struct passwd pwent;
110 struct passwd *pwent_ptr;
111 char buffer[1024];
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
117 char hostname[64];
118 gethostname(hostname, 64);
119 this->hostname = hostname;