-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibft_utils.c
More file actions
85 lines (76 loc) · 1.9 KB
/
libft_utils.c
File metadata and controls
85 lines (76 loc) · 1.9 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bschwitz <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/15 15:36:12 by bschwitz #+# #+# */
/* Updated: 2022/02/15 17:21:52 by bschwitz ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
int ft_isdigit(int c)
{
if (c >= 48 && c <= 57)
return (1);
else
return (0);
}
int ft_atoi(const char *str)
{
long i;
long neg;
long num;
i = 0;
neg = 0;
num = 0;
while (str[i] != '\0' && (str[i] == 32 || str[i] == '\t' || str[i] == '\n'
|| str[i] == '\r' || str[i] == '\v' || str[i] == '\f'))
i++;
if (str[i] != '\0' && str[i] == '-')
{
neg = 1;
i++;
}
else if (str[i] == '+')
i++;
while (str[i] != '\0' && ft_isdigit(str[i]))
num = (num * 10) + (str[i++] - '0');
if (neg == 1)
return (-num);
return (num);
}
void *ft_memset(void *b, int c, size_t len)
{
size_t i;
i = 0;
while (i < len)
{
((unsigned char *)b)[i] = c;
i++;
}
return (b);
}
void ft_putnbr_fd(int n, int fd)
{
unsigned int nbr;
if (n < 0)
{
nbr = (unsigned int)n * -1;
ft_putchar_fd('-', fd);
}
else
nbr = (unsigned int)n;
if (nbr > 9)
{
ft_putnbr_fd(nbr / 10, fd);
ft_putnbr_fd(nbr % 10, fd);
}
else
ft_putchar_fd(nbr + '0', fd);
}