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