-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpdracht1.cpp
More file actions
165 lines (138 loc) · 3.78 KB
/
Copy pathOpdracht1.cpp
File metadata and controls
165 lines (138 loc) · 3.78 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
#include "shell.hh"
int main()
{
std::string input;
int fd = syscall(SYS_open, "prompt.txt", O_RDONLY, 0755);
char byte[1];
std::string prompt = "";
while (syscall(SYS_read, fd, byte, 1))
prompt += byte;
while (true)
{
std::cout << prompt;
std::getline(std::cin, input);
if (input == "new_file")
new_file();
else if (input == "ls")
list();
else if (input == "src")
src();
else if (input == "find")
find();
else if (input == "seek")
seek();
else if (input == "exit")
return 0;
else if (input == "quit")
return 0;
else if (input == "error")
return 1;
if (std::cin.eof())
return 0;
}
}
void new_file()
{
const char *new_file[0];
std::string file_name;
std::cout << "Geef een bestandsnaam op: ";
getline(std::cin, file_name);
new_file[0] = file_name.c_str();
std::string file_content_chars = "";
std::string input;
int bytes = 0;
std::cout << "Geef inhoud van het bestand; " << std::endl;
while (getline(std::cin, input))
{
if (input != "<EOF>")
{
file_content_chars += input + "\n";
}
else
{
bytes = file_content_chars.length();
const char *file_content[] = {NULL};
file_content[0] = file_content_chars.c_str();
int fd = syscall(SYS_creat, new_file[0], 0755, NULL);
syscall(SYS_write, fd, file_content[0], bytes);
syscall(SYS_close, fd);
}
}
}
void list()
{
int pid1 = syscall(SYS_fork);
if (pid1 == 0) //child
{
const char *args[] = {"/bin/ls", "-l", "-a", NULL};
syscall(SYS_execve, args[0], args, NULL);
}
else
{
syscall(SYS_wait4, pid1, NULL, NULL, NULL);
}
}
void find()
{
std::string searchterm = "shell";
int fd[2];
syscall(SYS_pipe, &fd);
int pid1 = syscall(SYS_fork);
if (pid1 == 0) // child
{
syscall(SYS_close, fd[0]);
syscall(SYS_dup2, fd[1], 1);
const char *args[] = {"/user/bin/find", ".", NULL};
syscall(SYS_execve, args[0], args, NULL);
}
else // parent
{
int pid2 = syscall(SYS_fork);
if (pid2 == 0) // child 2
{
syscall(SYS_close, fd[1]);
syscall(SYS_dup2, fd[0], 0);
const char *args[] = {"/bin/grep", (char *)searchterm.c_str(), NULL};
syscall(SYS_execve, args[0], args, NULL);
}
else // parent 2
{
syscall(SYS_close, fd[0]);
syscall(SYS_close, fd[1]);
syscall(SYS_wait4, pid1, NULL, NULL, NULL);
syscall(SYS_wait4, pid2, NULL, NULL, NULL);
}
}
}
void seek()
{
const char *seek[0];
std::string file1_name = "seek.txt";
seek[0] = file1_name.c_str();
int fd1 = syscall(SYS_creat, seek[0], 0755, NULL);
int x = 0;
char character = 'x';
char *char_ptr = &character;
syscall(SYS_write, fd1, char_ptr, 1);
syscall(SYS_lseek, fd1, 5000000, 1);
syscall(SYS_write, fd1, char_ptr, 1);
syscall(SYS_close, fd1);
const char *loop[0];
std::string file2_name = "loop.txt";
loop[0] = file2_name.c_str();
int fd2 = syscall(SYS_creat, loop[0], 0755, NULL);
char empty_space = '\0';
char *empty_space_ptr = &empty_space;
syscall(SYS_write, fd2, char_ptr, 1);
for (int i = 0; i < 5000000; i++)
syscall(SYS_write, fd2, empty_space_ptr, 1);
syscall(SYS_write, fd2, char_ptr, 1);
syscall(SYS_close, fd2);
}
void src()
{
int fd = syscall(SYS_open, "shell.cc", O_RDONLY, 0755);
char byte[1];
while (syscall(SYS_read, fd, byte, 1))
std::cout << byte;
}