-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.c
More file actions
50 lines (50 loc) · 1.36 KB
/
echo.c
File metadata and controls
50 lines (50 loc) · 1.36 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
void echo(char cmd[]){
// printf("\n||%s||\n",cmd);
cmd=strtok(cmd,"|><");
char Print[MAX];
int pos=0;
int quotes[2]={0,0}; // DOES not contain actual number of "" or '' . Used for even odd. odd within even cases also. CONTAINS " & ' outside each of other.
int new_line=0;
for(int i=0;cmd[i];i++){
if(cmd[i]==' ' && quotes[0]%2==0 && quotes[1]%2==0){
Print[pos++]=cmd[i];
cmd=rem_leading_spaces(cmd+i);
i=-1; // i++ will make it 0 next step.
}
else if(cmd[i]=='\''){
if(quotes[1]%2==0){ // if not within the " " , then
quotes[0]++; //dont print
}
else{
Print[pos++]=cmd[i];
}
}
else if(cmd[i]=='"'){
if(quotes[0]%2==0){
quotes[1]++;
}
else{
Print[pos++]=cmd[i];
}
}
else if(cmd[i]=='\n'){
new_line=1;
Print[pos++]='\n';
}
else{
Print[pos++]=cmd[i];
}
}
Print[pos++]='\0';
if(quotes[0]%2 || quotes[1]%2){
printf("Multiline echo not supported yet. Missing ' or \".\n");
}
else{
if(new_line){
printf("%s",Print);
}
else{
printf("%s\n",Print);
}
}
}