-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfg.c
109 lines (95 loc) · 2.44 KB
/
fg.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
# include "header.h"
void callingfg(char *str)
{
char *fginp[10];
const char setdelimiters[] = " \t\0";
char *totken = strtok(str, setdelimiters);
ll i=0;
while (totken != NULL)
{
if(i>=2)break;
if(strcmp(totken,"fg")!=0)
{
fginp[i]=totken;
i++;
}
totken = strtok(NULL, setdelimiters);
}
if(i<1)
{
fprintf(stdout,"\033[1;31m--> ERROR : to few arguments \033[0m\n");
exit_fail=1;
// exit(EXIT_FAILURE);
}
if(i>12)
{
fprintf(stdout,"\033[1;31m--> ERROR : too many arguments \033[0m\n");
exit_fail=1;
// exit(EXIT_FAILURE);
}
if(i==1)
{
ll num=0;
ll base=pow(10,strlen(fginp[0])-1);
for(ll o=0;o<strlen(fginp[0]);o++)
{
ll u=fginp[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",fginp[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
{
char * var2=(malloc(sizeof(char)*1005));
sprintf(var2,"/proc/%lld/stat",zcurpid);
FILE *fd = fopen(var2,"r");
if((fd=fopen(var2,"r"))==NULL)
{
printf("No such process in background\n");
}
else
{
cur_pid=zcurpid;
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(STDIN_FILENO, zcurpid);
kill(zcurpid, SIGCONT);
int st;
waitpid(zcurpid, &st, WUNTRACED);
tcsetpgrp(STDIN_FILENO, getpgrp());
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
if (WIFSTOPPED(st)) {
stat_pro[zcurpid]=0;
}
}
}
}
return;
}