-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_putchar.c
More file actions
73 lines (65 loc) · 1013 Bytes
/
_putchar.c
File metadata and controls
73 lines (65 loc) · 1013 Bytes
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
#include "main.h"
/**
* _putchar - function prints char
*
* @c: input character
*
* Return: 1 Success, -1 Error
*/
int _putchar(char c)
{
return (buffer_write(c, 0));
}
/**
* mem_set - function fills string with given char
*
* @str: input string
* @n: input len
* @c: char to add
*/
void mem_set(char *str, int n, char c)
{
int i = 0;
for (; str != NULL && i < n; i++)
*(str + i) = c;
}
/**
* buffer_write - function writes char to the buffer
*
* @c: char to write
* @x: action to be done
*
* Return: 1 (Success)
*/
int buffer_write(char c, char x)
{
static int i;
static int char_count;
static char buffer[1024];
static char o;
if (i < 1024 && x == 0)
{
o = char_count < 1 ? 1 : o;
buffer[i] = c;
i++;
char_count++;
}
if (i >= 1024 || x == 1)
{
o = write(1, buffer, i);
fflush(stdout);
i = 0;
mem_set(buffer, 1024, 0);
}
if (x == -1)
{
i = 0;
char_count = 0;
mem_set(buffer, 1024, 0);
}
if (x == -2)
{
return (char_count);
}
return (o);
}