-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_function.c
71 lines (69 loc) · 989 Bytes
/
echo_function.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
#include "echo_function.h"
void echo_function()
{
char envvar[1000]={};
int el=0;
int ps=0;
int lst=i;
while(1)
{
if(arg[lst][0]==';')break;
else lst++;
}
for(k=i+1;k<lst;k++)
{
if(arg[k][0]=='$')
{
for(j=1;j<strlen(arg[k]);j++)
envvar[el++]=arg[k][j];
if(getenv(envvar)!=NULL)
printf("%s",getenv(envvar));
else
ps=1;
}
else
{
for(j=0;j<strlen(arg[k]);j++)
{
if(arg[k][j]!='\"' && arg[k][j]!='\'')
{
printf("%c",arg[k][j]);
}
}
}
if(ps==0)
printf(" ");
else
ps=0;
}
printf("\n");
i=lst+1;
return;
}
void gen_echo_child()
{
int lst=i-1;
while(1)
{
if(arg[lst][0]==';')break;
else lst++;
}
lst --;
for(k=i+1;k<lst;k++)
{
for(j=0;j<strlen(arg[k]);j++)
{
if(arg[k][j]!='\"' && arg[k][j]!='\'')
{
printf("%c",arg[k][j]);
}
}
printf(" ");
}
printf("\n");
i=lst+1;
printf(RESET "echo with pid %d exited normally\n" RESET,getpid() );
kill(getpid(),SIGKILL);
ampcount--;
exit(0);
}