-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathput_utils.c
More file actions
77 lines (67 loc) · 1.86 KB
/
put_utils.c
File metadata and controls
77 lines (67 loc) · 1.86 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* put_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschwabe <jschwabe@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/25 12:40:48 by jschwabe #+# #+# */
/* Updated: 2024/02/21 17:21:29 by jschwabe ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdint.h>
#include <unistd.h>
int dprintf_putchar(char c, int fd)
{
return (write(fd, &c, 1));
}
int dprintf_put_nbr(size_t n, int fd)
{
int size;
size = 0;
if (n >= 10)
{
size += dprintf_put_nbr(n / 10, fd);
if (size < 0)
return (FAIL);
}
size += dprintf_putchar(DECIMAL[n % 10], fd);
return (size);
}
int dprintf_put_hex(unsigned int n, const char *hex, int fd)
{
int size;
size = 0;
if (n >= 16)
{
size += dprintf_put_hex(n / 16, hex, fd);
if (size < 0)
return (FAIL);
}
size += dprintf_putchar(hex[n % 16], fd);
return (size);
}
int dprintf_put_str(const char *s, int fd)
{
size_t len;
if (!s)
s = "(null)";
len = 0;
while (s[len] != '\0')
{
if (dprintf_putchar(s[len], fd) < 0)
return (FAIL);
len++;
}
return (len);
}
int dprintf_ptr(uintptr_t ptr, int fd)
{
int len;
len = dprintf_put_str("0x", fd);
if (ptr == 0)
return (dprintf_putchar('0', fd) + len);
len += dprintf_put_hex(ptr, HEX_LOWER, fd);
return (len);
}