-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.c
More file actions
176 lines (167 loc) · 3.88 KB
/
utility.c
File metadata and controls
176 lines (167 loc) · 3.88 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
#include "headers.h"
void command_selector(char * input_str,char * add_root)
{
char command[1000],temp[1000];
strcpy(temp,input_str);
char * token = strtok(temp," ");
strcpy(command,token);
if(strcmp(command,"echo")==0)
{
echo(input_str);
}
else if(strcmp(command,"cd")==0)
{
cd(input_str,add_root);
}
else if(strcmp(command,"pwd")==0)
{
pwd();
}
else if(strcmp(command,"ls")==0)
{
ls(input_str,add_root);
}
else if(strcmp(command,"pinfo")==0)
{
pinfo(input_str,add_root);
}
else if(strcmp(command,"quit")==0 || strcmp(command,"q")==0 || strcmp(command,"exit")==0)
{
save_history();
exit(1);
}
else if(strcmp(command,"history")==0)
{
history(input_str);
}
else if(strcmp(command,"setenv")==0)
{
setenvi(input_str);
}
else if(strcmp(command,"unsetenv")==0)
{
unsetenvi(input_str);
}
else if(strcmp(command,"jobs")==0)
{
jobs_func();
}
else if(strcmp(command,"kjob")==0)
{
kjob(input_str);
}
else if(strcmp(command,"fg")==0)
{
fg(input_str);
}
else if(strcmp(command,"bg")==0)
{
bg(input_str);
}
else if(strcmp(command,"overkill")==0)
{
overkill();
}
else if(command==NULL)
{
exit(0);
}
else
{
sys_cmd(input_str);
}
}
char * trim(char * input_str)
{
long long int i,j,k,len=strlen(input_str);
char * output_str = (char*)malloc(sizeof(char)*1000);
for(j=len-1;j>=0;j--)
{
if(input_str[j]!='\n' && input_str[j]!=' ')
break;
}
for(i=0;i<len;i++)
{
if(input_str[i]!=' ')
break;
}
if(j!=-1)
{ k=0;
for(;i<=j;i++)
{
output_str[k++]=input_str[i];
}
output_str[k]='\0';
}
return output_str;
}
// void check_job_stat()
// {
// long long int i,j,k,flag,tmp;
// size_t len = 1000;
// char * pid;
// int pid_int;
// FILE * file;
// file = fopen("communicate.txt","r");
// if(!file)
// {
// printf("Error in process Termination\n");
// return;
// }
// i=0;
// for(;;)
// {
// flag=getline(&pid,&len,file);
// if(flag==-1)
// {
// break;
// }
// pid_int = atoi(pid);
// // printf("%d",pid_int);
// i+=1;
// for(tmp=0;tmp<job_seq_no;tmp++)
// {
// // printf("%d\n",jobs[tmp].pid);
// if(jobs[tmp].pid == pid_int)
// {
// jobs[tmp].status = 0;
// // printf("%d",jobs[tmp].id);
// break;
// }
// }
// }
// fclose(file);
// file = fopen("communicate.txt","w");
// fclose(file);
// }
void child_killed()
{
long long int i,j,exit_status;
int status;
long long int pid = waitpid(-1, &status, WNOHANG | WUNTRACED);
if(pid > 0)
{
if (WIFEXITED(status))
{
for(i=0;i<job_seq_no;i++)
{
if(jobs[i].pid == pid)
{
exit_status = WEXITSTATUS(status);
if (exit_status != 0)
{
// printf("NORMAL");
printf("Process %s with pid %lld exited with exit status %lld\n", jobs[i].command, pid, exit_status);
}
else
{
printf("Process %s with pid %lld exited normally\n", jobs[i].command, pid);
// printf("STATUS");
}
jobs[i].status = 0;
printf("Press [ENTER] to continue\n ");
}
}
}
}
}