Add initial efforts
[minishell-2.git] / Sources / opsys.cpp
blob1eeb5d92f4472541cf741a780339024e6099825f
1 #include "Headers/opsys.h"
3 OpSys OS; // Global OpSys variable. Should be accessible to all modules.
5 void signal_handler(int s)
7 if (s==SIGINT)
9 cout << "\nSIGINT (Ctrl+C) received. Code " << s << ". Exiting.\n";
10 exit(s);
13 if (s==SIGHUP)
15 cout << "\nSIGHUP received. Code " << s << ". Exiting.\n";
16 exit(s);
20 SignalHandler::SignalHandler()
22 signal_action.sa_handler = signal_handler;
23 sigemptyset(&signal_action.sa_mask);
24 signal_action.sa_flags = 0;
25 sigaction(SIGINT, &signal_action, NULL);
28 string OpSys::get_cwd()
30 char temp[MAXPATHLEN];
31 return getcwd(temp, sizeof(temp)) ? std::string( temp ) : std::string("");
34 void OpSys::change_dir(vector<string> command)
36 string cmd = "";
37 for(unsigned short int i = 1; i < command.size(); i++)
39 cmd += command[i];
41 if (chdir(cmd.c_str()))
43 cerr << "miniShell: cd: " << cmd << ": Arquivo ou diretório não encontrado\n";
45 else {
46 get_cwd();
47 cwd_changed = true;
51 OpSys::OpSys()
53 cwd = get_cwd();
54 if(cwd.empty())
55 cerr << RED_ANSI << "Could not get the current working directory.\n" << RESET_ANSI;
57 uid_t uid = geteuid(); // Gets the effective ID of the user that started miniSHELL
58 struct passwd pwent;
59 struct passwd *pwent_ptr;
60 char buffer[1024];
62 // Looks for the UDI on the password databank and saves the result on pwent
63 getpwuid_r(uid, &pwent, buffer, sizeof buffer, &pwent_ptr);
64 username = pwent.pw_name; // Saves username
66 char hostname[64];
67 gethostname(hostname, 64);
68 this->hostname = hostname;