-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_realloc.c
53 lines (50 loc) · 846 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
51
52
53
#include "holberton.h"
/**
* _realloc - reallocate memory from old block to new
* @ptr: pointer to old allocated memory
* @old_size: size of old allocation
* @new_size: size of new allocation
* Return: pointer to new memory
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *new;
unsigned int i;
char *ptrcpy;
if (ptr == NULL)
{
new = malloc(new_size);
if (new == NULL)
{
return (NULL);
}
return (new);
}
if (new_size == old_size)
{
return (ptr);
}
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
new = malloc(sizeof(char) * (new_size));
ptrcpy = ptr;
if (new_size > old_size)
{
for (i = 0; i < old_size; i++)
{
new[i] = ptrcpy[i];
}
}
else
{
for (i = 0; i < new_size; i++)
{
new[i] = ptrcpy[i];
}
}
free(ptrcpy);
return (new);
}