-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_realloc.c
More file actions
36 lines (33 loc) · 756 Bytes
/
_realloc.c
File metadata and controls
36 lines (33 loc) · 756 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
#include "shell.h"
/**
* _realloc - function that takes buffer and increase it's size
*
* @old_buffer: old data of the buffer
* @old_size: size of the buffer before update
* @new_size: size of the buffer after
* Return: buffer after it's being allocated, NULL in error
*/
void *_realloc(void *old_buffer, size_t old_size, size_t new_size)
{
unsigned char *buffer;
unsigned char *char_buffer;
size_t iter;
buffer = malloc(sizeof(unsigned char) * (new_size + 1));
if (!buffer)
return (NULL);
char_buffer = old_buffer;
iter = 0;
while (char_buffer && iter < old_size)
{
buffer[iter] = char_buffer[iter];
iter++;
}
while (iter < new_size)
{
buffer[iter] = 0;
iter++;
}
buffer[iter] = 0;
free(old_buffer);
return (buffer);
}