-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_helpers.c
73 lines (61 loc) · 1.39 KB
/
memory_helpers.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
#include "main.h"
unsigned int _memcpy(buffer_t *output, const char *src, unsigned int n);
void free_buffer(buffer_t *output);
buffer_t *init_buffer(void);
/**
* _memcpy - Copies n bytes from memory area src to
* the buffer contained in a buffer_t struct.
* @output: A buffer_t struct.
* @src: A pointer to the memory area to copy.
* @n: The number of bytes to be copied.
*
* Return: The number of bytes copied.
*/
unsigned int _memcpy(buffer_t *output, const char *src, unsigned int n)
{
unsigned int index;
for (index = 0; index < n; index++)
{
*(output->buffer) = *(src + index);
(output->len)++;
if (output->len == 1024)
{
write(1, output->start, output->len);
output->buffer = output->start;
output->len = 0;
}
else
(output->buffer)++;
}
return (n);
}
/**
* free_buffer - Frees a buffer_t struct.
* @output: The buffer_t struct to be freed.
*/
void free_buffer(buffer_t *output)
{
free(output->start);
free(output);
}
/**
* init_buffer - Initializes a variable of struct type buffer_t.
*
* Return: A pointer to the initialized buffer_t.
*/
buffer_t *init_buffer(void)
{
buffer_t *output;
output = malloc(sizeof(buffer_t));
if (output == NULL)
return (NULL);
output->buffer = malloc(sizeof(char) * 1024);
if (output->buffer == NULL)
{
free(output);
return (NULL);
}
output->start = output->buffer;
output->len = 0;
return (output);
}