-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.c
More file actions
55 lines (53 loc) · 1.32 KB
/
echo.c
File metadata and controls
55 lines (53 loc) · 1.32 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
51
52
53
54
55
#include "echo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define isnormalchar (ip == ' ' || (ip != '\"' && ip != '\''))
void echo(const int argc, char *const *argv, const int max_size_arg) {
char *total_inp = (char *)(malloc(argc * max_size_arg));
strcpy(total_inp, "");
for (int i = 1; i < argc; i++) {
strcat(total_inp, argv[i]);
if (i != argc - 1)
strcat(total_inp, " ");
}
char currstate = 'n';
// Used DFA to evaluate the string to be printed
// States 'n', 'd', 'q', 'c'
for (int i = 0; i < strlen(total_inp); i++) {
char ip = total_inp[i];
switch (currstate) {
case 'n':
if (ip == '\"')
currstate = 'd';
else if (isnormalchar) {
currstate = 'c';
printf("%c", ip);
} else if (ip == '\'')
currstate = 'q';
break;
case 'c':
if (isnormalchar)
printf("%c", ip);
else if (ip == '\'')
currstate = 'q';
else if (ip == '\"')
currstate = 'd';
break;
case 'd':
if (isnormalchar || ip == '\'')
printf("%c", ip);
else if (ip == '\"')
currstate = 'n';
break;
case 'q':
if (isnormalchar || ip == '\"')
printf("%c", ip);
else if (ip == '\'')
currstate = 'n';
break;
}
}
printf("\n");
free(total_inp);
}