-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.c
More file actions
212 lines (195 loc) · 5.15 KB
/
commands.c
File metadata and controls
212 lines (195 loc) · 5.15 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include "main.h"
void command_fg(char ** tokens ,int tokens_len)
{
if ( tokens_len == 1 || tokens_len > 2)
{
printf("error: incorrect number of arguments\n");
return ;
}
int status ;
int n = atoi(tokens[1]) - 1;
if (n >= 0 && n<p_len && (PROC[n].stat == 1 || PROC[n].stat == 0) )
{
PROC[n].stat = -1;
if(getpid() == PROC[n].ppid )
waitpid(PROC[n].pid, &status, 0);
}
else
{
printf("error : unavailable process\n");
}
}
void command_bg(char ** tokens ,int tokens_len)
{
if ( tokens_len == 1 || tokens_len > 2)
{
printf("error: incorrect number of arguments\n");
return ;
}
int n = atoi(tokens[1]) - 1;
if (n >= 0 && n<p_len && (PROC[n].stat == 1 || PROC[n].stat == 0))
{
if(PROC[n].stat == 1 )
{
PROC[n].stat = 0;
kill(PROC[n].pid,18);
}
}
else
{
printf("error : unavailable process\n");
}
}
void command_kill()
{
for(int i =0 ;i<p_len;i++)
kill(PROC[i].pid,9);
}
void command_kjobs(char ** tokens , int tokens_len)
{
if ( tokens_len == 1 || tokens_len > 3)
{
printf("error: incorrect number of arguments\n");
return ;
}
int no = atoi(tokens[1]) - 1;
int sig = atoi(tokens[2]);
if (no >= p_len || no < 0)
{
printf("unavailable job number\n");
return ;
}
kill(PROC[no].pid,sig);
}
void command_setenv(char ** tokens,int tokens_len)
{
if ( tokens_len == 1 || tokens_len > 3)
{
printf("error: incorrect number of arguments\n");
return ;
}
if (setenv(tokens[1],tokens[2], 1) < 0)
perror("setenv");
}
void command_unsetenv(char ** tokens,int tokens_len)
{
if ( tokens_len == 1 || tokens_len > 2)
{
printf("error: incorrect number of arguments\n");
return ;
}
if (unsetenv(tokens[1]) < 0)
perror("unsetenv");
}
void command_jobs()
{
char t[20];
for(int i = 0;i<p_len;i++)
{
if( PROC[i].stat != -1 && PROC[i].stat != 2)
{
strcpy(t,"");
if( PROC[i].stat == 1)
strcpy(t,"Stopped");
else if (PROC[i].stat == 0)
strcpy(t,"Running");
printf("[%d] %s %s[%d]\n",i+1,t,PROC[i].name,PROC[i].pid);
}
}
}
void command_cd(char ** tokens,int tokens_len,char cwd[MAX_LENGTH],char home[MAX_LENGTH])
{
if( tokens_len == 1)
find_how_back(home,cwd);
else if (tokens_len == 2)
{
if (chdir(tokens[1]) < 0)
printf(" %s directory not changed\n",tokens[1]);
}
else if (tokens_len > 2)
printf("more number of arguments than needed %d\n",tokens_len);
}
void command_pwd(char ** tokens,int tokens_len,char cwd[MAX_LENGTH])
{
if (tokens_len == 1)
printf("%s\n",cwd);
else
printf("more number of arguments than needed\n");
}
void command_echo(char ** tokens,int tokens_len)
{
char * p_str = malloc(MAX_LENGTH*sizeof(char));
strcpy(p_str,"");
for (int i=1;i<tokens_len;i++)
{
strcat(p_str,tokens[i]);
strcat(p_str," ");
}
printf("%s\n",p_str);
free(p_str);
}
char** command_ls(char ** tokens,int tokens_len,int * lsize)
{
int x =0 ;
int * listsize = &x;
char ** list;
if (tokens[1] == NULL)
list=list_out_ls(".",listsize);
else if (strcmp(tokens[1],"-l") == 0 && (tokens[2] == NULL || strcmp(tokens[2],"-a")!=0))
{
char * cur = (tokens[2] != NULL)?tokens[2]:(".");
list = list_all(cur,listsize);
int l = *listsize;
for(int i = 0;i<l;i++)
if (list[i] != NULL && list[i][0] != '.')
{
stat_file(list[i]);
}
}
else if (strcmp(tokens[1],"-a") == 0 && (tokens[2] == NULL || strcmp(tokens[2],"-a")!=0))
{
char * cur = (tokens[2] != NULL)?tokens[2]:(".");
list = list_all(cur,listsize);
for(int i = 0;i<(*listsize);i++)
printf("%s ",list[i]);
}
else if (strcmp(tokens[1],"-al") == 0 || strcmp(tokens[1],"-la") == 0)
{
char * cur = (tokens[2] != NULL)?tokens[2]:(".");
list = list_all(cur,listsize);
for(int i = 0;i<(*listsize);i++)
{
stat_file(list[i]);
}
}
else if ((strcmp(tokens[1],"-a") == 0 && strcmp(tokens[2],"-l")== 0) || (strcmp(tokens[1],"-l")==0 && strcmp(tokens[2],"-a")==0))
{
char * cur = (tokens[3] != NULL)?tokens[3]:(".");
list = list_all(cur,listsize);
for(int i = 0;i<(*listsize);i++)
{
stat_file(list[i]);
}
}
else
{
char * cur = (tokens[1] != NULL)?tokens[1]:(".");
list= list_out_ls(cur,listsize);
}
lsize = (listsize);
return list;
}
void command_pinfo(char * shell_pid , char ** tokens,int tokens_len)
{
if (tokens[1] == NULL)
pid_data(shell_pid);
else
pid_data(tokens[1]);
}