-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnumbs.c
82 lines (71 loc) · 1.58 KB
/
numbs.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "main.h"
/**
* convert - converter function, a clone of itoa
* @num: number
* @base: base
* @flags: argument flags
* @params: paramater struct
*
* Return: string
*/
char *convert(long int num, int base, int flags, params_t *params)
{
static char *array;
static char buffer[50];
char sign = 0;
char *ptr;
unsigned long n = num;
(void)params;
if (!(flags & CONVERT_UNSIGNED) && num < 0)
{
n = -num;
sign = '-';
}
array = flags & CONVERT_LOWERCASE ? "0123456789abcdef" : "0123456789ABCDEF";
ptr = &buffer[49];
*ptr = '\0';
do {
*--ptr = array[n % base];
n /= base;
} while (n != 0);
if (sign)
*--ptr = sign;
return (ptr);
}
/**
* print_unsigned - prints unsigned integer numbers
* @ap: argument pointer
* @params: the parameters struct
*
* Return: bytes printed
*/
int print_unsigned(va_list ap, params_t *params)
{
unsigned long l;
if (params->l_modifier)
l = (unsigned long)va_arg(ap, unsigned long);
else if (params->h_modifier)
l = (unsigned short int)va_arg(ap, unsigned int);
else
l = (unsigned int)va_arg(ap, unsigned int);
params->unsign = 1;
return (print_number(convert(l, 10, CONVERT_UNSIGNED, params), params));
}
/**
* print_address - prints address
* @ap: argument pointer
* @params: the parameters struct
*
* Return: bytes printed
*/
int print_address(va_list ap, params_t *params)
{
unsigned long int n = va_arg(ap, unsigned long int);
char *str;
if (!n)
return (_puts("(nil)"));
str = convert(n, 16, CONVERT_UNSIGNED | CONVERT_LOWERCASE, params);
*--str = 'x';
*--str = '0';
return (print_number(str, params));
}