-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealloc.c
More file actions
executable file
·63 lines (55 loc) · 1.25 KB
/
Copy pathrealloc.c
File metadata and controls
executable file
·63 lines (55 loc) · 1.25 KB
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
55
56
57
58
59
60
61
62
63
#include "shell.h"
/**
* _memset - Fills a block of memory with a specific byte value.
* @s: A pointer to the memory area.
* @b: The byte value to be set.
* @n: The number of bytes to set.
*
* Return: A pointer to the memory area @s.
*/
char *_memset(char *s, char b, unsigned int n)
{
unsigned int i;
for (i = 0; i < n; i++)
s[i] = b;
return (s);
}
/**
* ffree - Frees a dynamically allocated array of strings.
* @pp: A pointer to the array of strings.
*/
void ffree(char **pp)
{
char **a = pp;
if (!pp)
return;
while (*pp)
free(*pp++);
free(a);
}
/**
* _realloc - Reallocates a block of memory.
* @ptr: Pointer to the previously allocated memory block.
* @old_size: Size of the previous memory block in bytes.
* @new_size: Size of the new memory block in bytes.
*
* Return: A pointer to the new memory block, or NULL on failure.
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *p;
if (!ptr)
return (malloc(new_size));
if (!new_size)
return (free(ptr), NULL);
if (new_size == old_size)
return (ptr);
p = malloc(new_size);
if (!p)
return (NULL);
old_size = old_size < new_size ? old_size : new_size;
while (old_size--)
p[old_size] = ((char *)ptr)[old_size];
free(ptr);
return (p);
}