-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
124 lines (111 loc) · 1.76 KB
/
_printf.c
File metadata and controls
124 lines (111 loc) · 1.76 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "shell.h"
/**
* print_integer - prints integer
* @num: integer to print
* @n: stream to print to
*
* Return: void
*/
void print_integer(int num, int n)
{
char buffer[32];
int i = 0, j;
if (num == 0)
{
write(n, "0", 1);
return;
}
if (num < 0)
{
write(n, "-", 1);
num = -num;
}
while (num != 0)
{
buffer[i++] = '0' + (num % 10);
num /= 10;
}
for (j = i - 1; j >= 0; j--)
write(n, &buffer[j], 1);
}
/**
* write_string - writes a string to the specified stream
* @n: stream to write to
* @s: string to write
*
* Return: void
*/
void write_string(int n, const char *s)
{
write(n, s, _strlen(s));
}
/**
* print_string - print string lateral
* @s:string to print
* @n: stream
*
* Description: This function prints a string lateral
* Return: void
*/
void print_string(char *s, int n)
{
write_string(n, s);
}
/**
* _isspace - checks if a character is a space
* @c: character to check
*
* Return: 1 if space
*/
int _isspace(int c)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r')
return (1);
else
return (0);
}
/**
* _printf - minature dprintf function
* @stream: stream to write to
* @format: format to write
*
* Return: void
*/
void _printf(int stream, const char *format, ...)
{
va_list args;
fflush(stdout);
fflush(stdin);
va_start(args, format);
while (*format != '\0')
{
if (*format == '%')
{
format++;
switch (*format)
{
case 'd':
{
int num = va_arg(args, int);
print_integer(num, stream);
break;
}
case 's':
{
char *str = va_arg(args, char*);
print_string(str, stream);
break;
}
default:
write(stream, format, 1);
break;
}
}
else
{
write(stream, format, 1);
}
format++;
}
va_end(args);
}