forked from JavierOramas/SO-Shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
executable file
·47 lines (38 loc) · 1.07 KB
/
shell.c
File metadata and controls
executable file
·47 lines (38 loc) · 1.07 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
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
int main(){
while(1){
int id = fork();
int stat;
if( id == 0){
printf("$ ");
char command[2048];
char temp;
int number = 0;
int index = 0;
do{
scanf("%c", &temp);
if(temp != '\0' && temp != '\n')
command[index] = temp;
if(temp == ' ' && command[index-1] != '\\')
number++;
index++;
}while(temp != '\n' && temp != '\0');
char *arguments[number+1];
char *commands = strtok(command, " ");
int i =0;
while( commands != NULL ) {
arguments[i] = commands;
i++;
commands = strtok(NULL, " ");
}
arguments[number+1] = NULL;
execvp(arguments[0],arguments);
break;
}
waitpid(id,&stat, 0);
}
}