-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_realloc.c
50 lines (44 loc) · 798 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
50
#include "main.h"
/**
* _realloc - function that reallocates a memory block using
* @ptr: pointer to array
* @old_size: old size
* @new_size: new size
*
* Return: A pointer to the allocated memory
*
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *s;
char *ptr1;
unsigned int i;
ptr1 = (char *)ptr;
if (ptr == NULL)
return (malloc(new_size));
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
if (new_size == old_size)
return (ptr);
s = malloc((new_size) * sizeof(char));
if (s == NULL)
{
free(s);
return (NULL);
}
if (new_size > old_size)
{
for (i = 0; i < old_size; i++)
s[i] = ptr1[i];
}
if (new_size < old_size)
{
for (i = 0; i < new_size; i++)
s[i] = ptr1[i];
}
free(ptr1);
return (s);
}