-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.txt
145 lines (102 loc) · 2.09 KB
/
Functions.txt
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
int commandExecution( {command format} )
{
pid_t pid;
pid = fork();
if(pid < 0)
{
cerr << "fork failed" << endl;
exit(0);
}
else if( pid == 0)
{
}
}
int execute( argv)
int redirectToFile( const string & filename)
{
FILE * inFile = fopen (filename.c_str(), "r");
if(inFile == NULL)
{
}
else
{
}
}
//#include <signal.h>
//#include <cstring>
//#include <iostream>
void kill( string *cmds)
{
if(cmds[1].empty())
{
cerr << "Must pass ID number of process to kill" << endl;
return;
}
if(kill(atoi(cmds[1].c_str()), SIGKILL) != 0)
{
cerr << "Error, Process could not be killed" << endl;
}
}
//#include <unistd.h>
//#include <cstring>
//#include <iostream>
void cd( string *cmds)
{
if(!cmds[1].empty())
{
//absolute path given
if(cmds[1][0] == '/')
{
if(chdir(cmds[1].c_str()) != 0)
cerr << "Error, invalid directory: " << cmds[1] << endl;
}
//dosen't start with '/' so relitive path given
else
{
string workingdir = get_current_dir_name();
if(chdir((workingdir + "/" + cmds[1]).c_str()) != 0)
cerr << "Error, invalid directory: " << cmds[1] << endl;
}
}
//only cd given as argument so change to home directory
else
if(chdir(getenv("HOME")) != 0)
cerr << "Error, couldn't change directory to: " << getenv("HOME") << endl;
}
void set( string *cmds)
{
if(cmds[1].empty())
{
cerr << "Error, No enviroment specified" << endl;
return;
}
if(strncmp(cmds[1].c_str(), "HOME=", 5) == 0)
{
if(setenv("HOME", &cmds[1][5],1) == -1)
cerr << "Error, could not set HOME" << endl;
}
if(strncmp(cmds[1].c_str(), "PATH=", 5) == 0)
{
if(setenv("PATH", &cmds[1][5],1) == -1)
cerr << "Error, could not set PATH << endl;
}
}
//Basic Zombie reaping with sigaction()
//#include <signal.h>
//#include <sys/wait.h>
void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
void reaper()
{
struct sigaction sa;
sa.sa_handler = sigchld_handler; //reaps all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if(sigaction(SIGCHLD, &sa, NULL) == -1
{
perror("sigaction");
exit(1);
}
}