forked from BennettDixon/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realloc.c
52 lines (39 loc) · 811 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
#include <stdlib.h>
/**
* *_realloc - reallocates memory block and copies old->new
* @ptr: points to old memory
* @old_size: size in bytes of old memory
* @new_size: size in bytes of new memory
* Return: pointer to new memory @new
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *new;
char *temp;
unsigned int i;
if (ptr == NULL)
{
ptr = malloc(new_size);
if (ptr == NULL)
return (NULL);
return (ptr);
}
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
new = malloc(new_size);
if (new == NULL)
return (NULL);
old_size = (new_size < old_size) ? new_size : old_size;
temp = ptr;
for (i = 0; i < old_size; i++)
{
*(new + i) = *(temp + i);
}
free(ptr);
return (new);
}