-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.c
More file actions
181 lines (165 loc) · 2.48 KB
/
args.c
File metadata and controls
181 lines (165 loc) · 2.48 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
#include "headers.h"
extern int proc_no;
void get_args(char* args[],char* cur)
{
int l=0;
while(cur!=NULL)
{
args[l++] = cur;
cur = strtok(NULL,"\t\n ");
}
args[l]=NULL;
return;
}
int check_ptype(char* copy[])
{
int type = 0;
int i=0;
while(copy[i]!=NULL)
{
if(strcmp(copy[i++],"&")==0)
{
type=1;
break;
}
}
if(type==1)
{
if(copy[i]!=NULL)
type=0;
}
return type;
}
void exec_pwd(char *cur[])
{
char pwd[300];
char * ret = getcwd(pwd,300);
if(ret==NULL)
{
perror("pwd");
return;
}
printf("%s\n",pwd);
return;
}
void exec_echo(char *args[])
{
if(args[1]==NULL)
{
perror("No argument");
return;
}
int k=1;
while(args[k]!=NULL)
printf("%s ",args[k++]);
printf("\n");
fflush(stdout);
return;
}
void sort_proc(int proc_arr[])
{
while(1)
{
int flag=0;
for(int i=0;i<proc_no-1;i++)
{
if(proc_arr[i]>proc_arr[i+1])
{
int temp = proc_arr[i+1];
proc_arr[i+1] = proc_arr[i];
proc_arr[i] = temp;
flag=1;
}
}
if(flag==0)
break;
}
return;
}
void get_name(char name[],int procid)
{
char dir[1000]="/proc/";
char pno[1000];
snprintf(pno,100,"%d",procid);
strcat(dir,pno);
strcat(dir,"/cmdline");
int fd = open(dir,O_RDONLY);
if(fd<0)
{
perror("error is readind file");
return;
}
read(fd,name,1000);
close(fd);
return;
}
void exec_jobs(int proc_arr[])
{
sort_proc(proc_arr);
int count=1;
for(int i=0;i<proc_no;i++)
{
if(proc_arr[i]<0)
continue;
int j = 1;
char dir[1000]="/proc/";
char pno[1000];
snprintf(pno,100,"%d",proc_arr[i]);
strcat(dir,pno);
strcat(dir,"/status");
int fd = open(dir,O_RDONLY);
if(fd<0)
{
continue;
}
char out[100000];
read(fd,out,100000);
char name[1500];
get_name(name,proc_arr[i]);
char* token = strtok(out,"\t\n ");
while(strcmp(token,"State:")!=0&&token!=NULL)
token = strtok(NULL,"\t\n ");
token = strtok(NULL,"\t\n ");
printf("[%d] ",count++);
if(strcmp(token,"T")==0)
{
printf("Stopped ");
}
else
{
printf("Running ");
}
printf("%s %d\n",name,proc_arr[i]);
close(fd);
}
return;
}
int get_jobs(int proc_arr[],int jobno)
{
sort_proc(proc_arr);
int reqjob=-1;
for(int i=0;i<proc_no&&jobno>0;i++)
{
if(proc_arr[i]>0)
{
reqjob = proc_arr[i];
jobno--;
}
}
if(jobno>0)
reqjob = -1;
return reqjob;
}
void add_fore(int p_id,int proc_arr[])
{
printf("%d\n",p_id);
for(int i=0;i<proc_no;i++)
{
if(proc_arr[i]==p_id)
{
proc_arr[i]=-3;
break;
}
}
return;
}