-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
46 lines (38 loc) · 889 Bytes
/
shell.c
File metadata and controls
46 lines (38 loc) · 889 Bytes
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
/*
* Simple shell
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAXINPUT 4096
#define PROMPT "> "
int
main()
{
char buf[MAXINPUT];
int status;
pid_t pid;
printf(PROMPT);
while (fgets(buf, MAXINPUT, stdin) != NULL) {
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0; // Won't it allways end with '\n' ?
if ( (pid = fork()) < 0) {
perror("fork");
exit(1);
}
else if (pid == 0) { // Child
execlp(buf, buf, (char*)0);
fprintf(stderr, "Couldn't execute %s\n", buf);
exit(127);
}
if ((pid = waitpid(pid, &status, 0)) < 0) {
perror("waitpid");
return 1;
}
printf(PROMPT);
}
return 0;
}