-
Notifications
You must be signed in to change notification settings - Fork 3
/
commandfilter2.c
163 lines (153 loc) · 2.75 KB
/
commandfilter2.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
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
#include "holberton.h"
/**
* _isespecialchr - show help document of some functions.
* @c: node tha has the builtin command
*
* Return: no return
*/
int _isespecialchr(char c)
{
int i = 0;
char tokens[] = {'&', '|', '\0', ';', '\n', '#', '$', ' ', '\t', -1};
while (tokens[i] != -1)
{
if (tokens[i] == c)
return (1);
i++;
}
return (0);
}
/**
* have_alias - show help document of some functions.
* @buf: node tha has the builtin command
* @i: int
*
* Return: no return
*/
int have_alias(char *buf, int i)
{
int st = i - 1;
int end = i;
int cnt = 0, flag = 0;
char *al = "alias";
if (st < 0)
return (0);
while (st >= 0)
{
if (buf[st] == '\n' || buf[st] == 0
|| buf[st] == ';' || buf[st] == '&'
|| buf[st] == '|')
break;
st--;
}
if (st < 0)
st++;
for (; st <= end; st++)
{
if (buf[st] == 'a')
{
for (cnt = 0; al[cnt]; cnt++, st++)
{
if (al[cnt] != buf[st])
break;
if (!al[cnt + 1] && (buf[st + 1] == ' ' ||
buf[st + 1] == '\t'
|| buf[st + 1] == '$'))
flag = 1;
}
}
}
return (flag);
}
/**
* replace_stat - show help document of some functions.
* @buf: node tha has the builtin command
* @newbuf: newbuf
* @i: int
* @pos: pos size
*
* Return: no return
*/
int replace_stat(char *buf, char *newbuf, int *i, int *pos)
{
int a, j, p;
char *stat;
(void)buf;
(void)i;
a = 0, j = 0;
a = setstatus(NULL);
stat = print_number(a);
p = *pos;
for (; stat[j]; j++, p = p + 1)
newbuf[p] = stat[j];
p = p + 1;
free(stat);
return (p);
}
/**
* replace_pid - show help document of some functions.
* @buf: node tha has the builtin command
* @newbuf: newbuffer
* @i: int i
* @pos: pos position
*
* Return: no return
*/
int replace_pid(char *buf, char *newbuf, int *i, int *pos)
{
int a = setpid(NULL);
char *stat = print_number(a);
int j = 0;
int p;
(void)buf;
(void)i;
p = *pos;
for (; stat[j]; j++, p++)
newbuf[p] = stat[j];
p++;
free(stat);
return (p);
}
/**
* _replacevar - show help document of some functions.
* @buf: node tha has the builtin command
* @newbuf: newbuf
* @i: int i
* @pos: pos position
*
* Return: no return
*/
void _replacevar(char *buf, char *newbuf, int *i, int *pos)
{
char *name = _calloc(60, 1);
char *value = NULL;
int cb = *i, j = 0, p = 0;
cb++;
if (buf[cb] == '$' || buf[cb] == '?')
{
if (buf[cb] == '$')
p = replace_pid(buf, newbuf, i, pos);
else
p = replace_stat(buf, newbuf, i, pos);
cb++;
*i = cb;
*pos = p - 1;
return;
}
else
{
for (; !_isespecialchr(buf[cb]); cb++)
;
for (; (j + 1 + *i) < cb; j++)
name[j] = buf[j + *i + 1];
}
value = _getenvvar(name);
if (value)
{
for (j = 0; value[j]; j++, *pos = *pos + 1)
newbuf[*pos] = value[j];
free(value);
}
free(name);
*i = cb;
}