-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
77 lines (76 loc) · 1.7 KB
/
shell.c
File metadata and controls
77 lines (76 loc) · 1.7 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
//Path length<100
#include <stdio.h>
#include<unistd.h>
#include <stdlib.h>
#include <string.h>
#include "display.c"
#include "operation.c"
int main(void)
{
int shell_id=getpid();
int *procid=(int *)malloc(100*sizeof(int));
int *tempid=(int *)malloc(100*sizeof(int));
char *procname[100];
char *tempname[100];
for(int i=0;i<100;i++)
{
procname[i]=(char *)malloc(100*sizeof(char));
tempname[i]=(char *)malloc(100*sizeof(char));
}
char *path,*command,*temp_path,*home_path;
int home;
path=(char *)malloc(200 * sizeof(char));
temp_path=(char *)malloc(200 * sizeof(char));
home_path=(char *)malloc(200 * sizeof(char));
command=(char *)malloc(100 * sizeof(char));
//Check whether this returns null for error
getcwd(path,200);
home=strlen(path);
strcpy(home_path,path);
home_path[home]='\0';
while(1)
{
int args;
int status;
char tokens[10][100];
getcwd(path,200);
if(path==NULL)
{
perror("Could not retrieve the current path");
exit(0);
}
strcpy(temp_path,path);
temp_path[home]='\0';
if(home>strlen(path)||strcmp(temp_path,home_path))
{
disp(path,0);
}
else
{
disp(&path[home],1);
}
fgets(command,100,stdin);
operate(command,home,procid,procname,shell_id,home_path);
int j=0;
for(int i=1;i<=procid[0];i++)
{
if(waitpid((pid_t)procid[i],&status,WNOHANG)!=0)
{
printf("Process %s with id %d has exited\n",procname[i],procid[i]);
}
else
{
j++;
tempid[j]=procid[i];
tempname[j]=procname[j];
}
}
tempid[0]=j;
for(int i=0;i<=j;i++)
{
procid[i]=tempid[i];
procname[i]=tempname[i];
}
}
return 0;
}