-
Notifications
You must be signed in to change notification settings - Fork 6
/
2016-SE-02.c
69 lines (57 loc) · 1.98 KB
/
2016-SE-02.c
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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <string.h>
#include <sys/wait.h>
const int BUFF_SIZE = 4096;
void write_safe(const char* str);
void write_safe(const char* str) {
if (write(1, str, strlen(str)) != (int)strlen(str)) {
err(1, "Failed writing");
}
}
int main(void) {
int bytes_read;
char buff[BUFF_SIZE];
const char *cmd = "Enter command:";
write_safe(cmd);
while ((bytes_read = read(1, buff, sizeof(buff))) > 0) {
buff[bytes_read - 1] = '\0';
const pid_t child = fork();
if (child == -1) {
err(3, "Failed fork");
}
if (strcmp(buff, "exit") == 0) {
exit(0);
}
if (child == 0) {
char command[BUFF_SIZE];
strcpy(command, "/bin/");
strcat(command, buff);
if (execl(command, buff, (char *)NULL) == -1) {
err(6, "Could not exec %s", buff);
}
}
int status;
if (wait(&status) == -1) {
err(4, "Could not wait for child process to finish");
}
if (!WIFEXITED(status)) {
errx(5, "Child process did not terminate normally");
}
if (WEXITSTATUS(status) != 0) {
errx(6, "Child process finished with exit code not 0");
}
write_safe(cmd);
}
if (bytes_read == -1) {
err(2, "Failed reading");
}
}
//Напишете програма на C, която реализира simple command prompt. Тя изпълнява в
// цикъл следната поредица действия:
//1. Извежда промпт на стандартния изход.
//2. Прочита име на команда.
//3. Изпълнява без параметри прочетената команда.
//Командите се търсят в директорията /bin. За край на програмата се смята въвеждането на exit.