-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbg.c
86 lines (76 loc) · 1.84 KB
/
bg.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
# include "header.h"
void callingbg(char *str){
char *bginp[10];
const char bdelimiters[] = " \t\0";
char *totken = strtok(str, bdelimiters);
ll i=0;
while (totken != NULL)
{
if(i>=2)break;
if(strcmp(totken,"bg")!=0)
{
bginp[i]=totken;
i++;
}
totken = strtok(NULL, bdelimiters);
}
if(i<1)
{
fprintf(stdout,"\033[1;31m--> ERROR : to few arguments \033[0m\n");
exit_fail=1;
// exit(EXIT_FAILURE);
}
else if(i>1)
{
fprintf(stdout,"\033[1;31m--> ERROR : too many arguments \033[0m\n");
exit_fail=1;
// exit(EXIT_FAILURE);
}
else if(i==1)
{
ll num=0;
ll base=pow(10,strlen(bginp[0])-1);
for(ll o=0;o<strlen(bginp[0]);o++)
{
ll u=bginp[0][o]-'0';
if(u>=0 && u<=9){
num+=(u*base);
base/=10;
}
else
{
fprintf(stdout,"\033[1;31m--> ERROR : [%s] is not an integer \033[0m\n",bginp[0]);
exit_fail=1;
break;
}
}
ll zcurpid=-1;
ll pik=0;
for(int j=0;j<job_counter;j++)
{
if(order[j][1]==-1 || stat_pro[order[j][0]]==2 )
{
pik++;
if(pik==num)
{
zcurpid=order[j][0];
break;
}
}
}
if(pik<num)
{
fprintf(stdout,"\033[1;31m--> ERROR : job number does not exists\033[0m\n");
exit_fail=1;
}
else
{
if(kill(zcurpid, SIGCONT)<0)
{
perror("could not run bg process");
exit_fail=1;
}
}
}
return;
}