-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.c
169 lines (153 loc) · 2.89 KB
/
shell.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
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
166
167
168
169
#include "global.h"
#include "cd.h"
#include "pwd.h"
#include "ls.h"
#include "echo.h"
#include "pinfo.h"
char home[5000];
void Ctoken(char* string, char** output, char* omitter)
{
char* token;
token=strtok(string,omitter);
int lp=0;
while((token)!=NULL)
{
output[lp]=token;
token=strtok(NULL,omitter);
lp=lp+1;
}
}
int main(void)
{
struct utsname buffer;
register struct passwd *pw;
register uid_t uid;
getcwd(home,sizeof(home));
int len_home = strlen(home);
while(1)
{
int background =0;
int temp;
char p[1032];
char command[500];
char* storeColon[100]={NULL};
if (uname(&buffer) != 0)
{
perror("uname");
exit(EXIT_FAILURE);
}
//char *args[]= {"./initial","1" , "2",NULL};
int pid;
uid = geteuid();
pw = getpwuid(uid);
if(pw)
{
printf("<%s@%s:",pw->pw_name,buffer.nodename);
getcwd(p,sizeof(p));
if(!strcmp(p,home))
{
printf("~>");
}
else
{
if(strstr(p,home)!=NULL)
{
printf("~%s>",len_home+p);
}
else
{
printf("%s>",p);
}
}
int counter=0;
fgets(command,500,stdin);
Ctoken(command,storeColon,";");
//int command_len=strlen(storeColon);
//printf("%d\n",command_len);
while(storeColon[counter]!=NULL)
{
//printf("%s\n",storeColon);
char array[500];
strcpy(array,storeColon[counter]);
//printf("%s\n",array);
if(array[strlen(array)-1]=='\n')
{
//printf("pro\n");
array[strlen(array)-1]='\0';
}
char* new_str[100]={NULL};
Ctoken(array,new_str," \t\n");
int sts,t=0;
while(new_str[t]){
t++;
}
// printf("%d\n",t );
if (new_str[t-1]){
if(new_str[0]!='\0' && (!strcmp(new_str[t-1] ,"&")))
{
new_str[t-1] = NULL ;
printf("bitch\n");
background =1;
}}
if (background){
pid = fork();
if(pid != 0 )
{
printf("Waiting on child (%d).\n", pid);
waitpid (-1,&sts,WNOHANG);
}
else if (pid <0)
exit (1);
else
{
execvp(*new_str , new_str);
exit(1);
}
}
else if(new_str[0]!='\0')
{
if (!strcmp(new_str[0], "exit")) exit(0);
else if(!strcmp(new_str[0],"cd"))
{
checkCd(new_str,home);
}
else if(!strcmp(new_str[0],"pwd"))
{
checkPwd();
}
else if(!strcmp(new_str[0],"ls"))
{
checkLs(new_str);
}
else if(!strcmp(new_str[0],"echo"))
{
checkEcho(new_str);
}
else if(!strcmp(new_str[0],"pinfo"))
{
CheckPinfo(new_str,home);
}
else
{
temp = fork();
int status;
if(temp == 0)
{
if(execvp(*new_str, new_str) < 0)
{
printf("Input error: Command not found!\n");
}
}
else
{
waitpid(temp,&status,WUNTRACED);
}
}
}
counter++;
//printf("%d\n",counter);
}
}
}
return 0;
}