-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.c
63 lines (56 loc) · 1.52 KB
/
match.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
/*
** EPITECH PROJECT, 2018
** match Yoann ABBES
** File description:
** match Yoann ABBES
*/
#include <unistd.h>
#include "my.h"
int handle_error(char const *s1, char const *s2)
{
int error_m = 2;
int size1 = my_strlen(s1);
if ((s1 == NULL && s2 == NULL) || (size1 == 0 && my_strlen(s2) == 0))
error_m = 1;
else if ((s1 == NULL && s2 != NULL) || (s1 != NULL && s2 == NULL))
error_m = 0;
else
error_m = size_error(s1, s2, error_m);
return (error_m);
}
int size_error(char const *s1, char const *s2, int error_size)
{
int size1 = my_strlen(s1);
int size2 = my_strlen(s2);
if ((size1 == 0 && size2 > 0) || (size1 > 0 && size2 == 0))
error_size = 0;
if (size1 == 0 && size2 == 1 && s2[0] == '*')
error_size = 1;
if (size2 == 0 && size1 == 1 && s1[0] == '*')
error_size = 1;
return (error_size);
}
int do_cmp(char const *s1, char const *s2)
{
if (*s1 == *s2) {
if (*s2 != '\0')
return (do_cmp(s1 + 1, s2 + 1));
else
return (1);
} else {
if (*s2 == '*' && *s1 == '\0' && *(s2 + 1) == '\0')
return (1);
if (*s2 == '*' && *s1 == '\0' && *(s2 + 1) != '\0')
return (do_cmp(s1, s2 + 1));
if (*s2 == '*' && *s1 != '\0')
return (do_cmp(s1 + 1, s2) || do_cmp(s1, s2 + 1));
}
return (0);
}
int match(char const *s1, char const *s2)
{
if (handle_error(s1, s2) == 2)
return (do_cmp(s1, s2));
else
return (handle_error(s1, s2));
}