-
Notifications
You must be signed in to change notification settings - Fork 0
/
_realloc.c
50 lines (46 loc) · 892 Bytes
/
_realloc.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
#include "sshell.h"
/**
* _realloc - change the size and copy the content
* @ptr: malloc pointer to reallocate
* @old_size: old number of bytes
* @new_size: new number of Bytes
* Return: nothing
*/
void *_realloc(char *ptr, unsigned int old_size, unsigned int new_size)
{
char *p = NULL;
unsigned int i;
if (new_size == old_size)
return (ptr);
if (ptr == NULL)
{
p = _calloc(new_size + 1, sizeof(char));
if (!p)
return (NULL);
return (p);
}
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
if (new_size > old_size)
{
p = _calloc(new_size + 1, sizeof(char));
if (!p)
return (NULL);
for (i = 0; i < old_size; i++)
p[i] = *((char *)ptr + i);
free(ptr);
}
else
{
p = _calloc(new_size + 1, sizeof(char));
if (!p)
return (NULL);
for (i = 0; i < new_size; i++)
p[i] = *((char *)ptr + i);
free(ptr);
}
return (p);
}