-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_realloc.c
More file actions
36 lines (35 loc) · 699 Bytes
/
_realloc.c
File metadata and controls
36 lines (35 loc) · 699 Bytes
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
#include "shell.h"
/**
* _realloc - reallocates a memory block
* @ptr: initial pointer
* @old_size: initial size
* @new_size: new size
* Return: pointer to array
*/
char *_realloc(char *ptr, unsigned int old_size, unsigned int new_size)
{
char *clone, *relloc, *aux;
unsigned int i;
if (ptr != NULL)
clone = ptr;
else
{
aux = malloc(sizeof(char) * new_size);
return (aux);
}
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr != NULL)
{ free(ptr);
return (0); }
relloc = malloc(sizeof(char) * new_size);
if (relloc == NULL)
return (0);
for (i = 0; i < old_size; i++)
{
*(relloc + i) = clone[i];
}
*(relloc + i) = '\0';
free(ptr);
return (relloc);
}