-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_memcmp.c
More file actions
52 lines (48 loc) · 1.81 KB
/
ft_memcmp.c
File metadata and controls
52 lines (48 loc) · 1.81 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bnoufel <bnoufel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 16:04:25 by bnoufel #+# #+# */
/* Updated: 2020/02/08 19:49:38 by bnoufel ### ########.fr */
/* */
/* ************************************************************************** */
#include "mem.h"
/*
** @param void *s
** @param int c
** @param size_t n
** The ft_memcmp() function compares byte string s1 against byte string s2
** Both strings are assumed to be n bytes long.
************************* RETURN VALUES **********************************
** The ft_memcmp() function returns zero if the two strings are identical,
** otherwise returns the difference between the first two differing bytes
** (treated as uint8_t values, so that `\200' is greater than `\0').
** Zero-length strings are always identical.
** This behavior is not required by C and portable code should only
** depend on the sign of the returned value.
*/
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
uint8_t *p1;
uint8_t *p2;
size_t i;
i = 0;
p1 = (uint8_t *)s1;
p2 = (uint8_t *)s2;
if (s1 == s2 || !n)
return (0);
while (i < n)
{
if (p1[i] != p2[i])
{
if (p1[i] < p2[i])
return (-1);
return (1);
}
i++;
}
return (0);
}