-
Notifications
You must be signed in to change notification settings - Fork 0
/
_fprint.c
110 lines (103 loc) · 1.97 KB
/
_fprint.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "shell.h"
/**
* _fput_number_helper - function helps to
* print number
*
* @fd: file descriptor of the file that
* the given number will be printed in
* @number: number to be printed
* Return: number of printed digits
*/
int _fput_number_helper(int fd, int number)
{
int ret;
char c;
if (number == 0)
return (0);
ret = _fput_number_helper(fd, number / 10);
c = number % 10 + 48;
write(fd, &c, 1);
return (1 + ret);
}
/**
* _fput_number - function that print
* given number into given file
*
* @fd: file descriptor
* @number: to be printed
* Return: the length of the printed
* characters
*/
int _fput_number(int fd, int number)
{
int printed;
printed = 0;
if (!number)
{
write(fd, "0", 1);
return (1);
}
if (number < 0)
{
write(fd, "-", 1);
printed = 1;
number *= -1;
}
return (printed + _fput_number_helper(fd, number));
}
/**
* _fputs - function that prints string
* into given file descriptor
*
* @fd: file descriptor
* @s: string to be printed
* Return: number of character printed
*/
int _fputs(int fd, const char *s)
{
return (write(fd, s, _strlen(s)));
}
/**
* _fprint - function that allows to
* to print number and string using
* format specifiers
*
*
* @fd: file descriptor
* @format: format to be printed to given fd
* and change place holder to specific values
* Return: number of printed characters
*/
int _fprint(int fd, const char *format, ...)
{
va_list ap;
int index, printed;
int is_percent;
va_start(ap, format);
is_percent = 0;
index = 0;
printed = 0;
while (format[index])
{
if (format[index] == '%')
is_percent = 1;
else
{
if (is_percent)
{
if (format[index] == 'd')
printed += _fput_number(fd, va_arg(ap, int));
else if (format[index] == 's')
printed += _fputs(fd, va_arg(ap, char *));
else
printed += write(fd, &format[index - 1], 2);
}
else
printed += write(fd, format + index, 1);
is_percent = 0;
}
index++;
}
va_end(ap);
return (printed);
}