-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils1.c
More file actions
88 lines (72 loc) · 1.9 KB
/
string_utils1.c
File metadata and controls
88 lines (72 loc) · 1.9 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "main.h"
/**
* _memcpy - Copies a block of memory from the source
* pointer to the destination pointer.
* @new_pointer: The destination pointer.
* @pointer: The source pointer.
* @s: The size of the new pointer.
*
* Return: None.
*/
void _memcpy(void *new_pointer, const void *pointer, unsigned int s)
{
char *char_ptr = (char *)pointer;
char *char_newptr = (char *)new_pointer;
unsigned int i;
for (i = 0; i < s; i++)
char_newptr[i] = char_ptr[i];
}
/**
* _realloc - Reallocates a memory block with a new size.
* @pointer: Pointer to the memory previously allocated.
* @old_s: Size, in bytes, of the allocated space for ptr.
* @new_s: New size, in bytes, of the reallocated memory block.
*
* Return: Pointer to the reallocated memory block (ptr).
*/
void *_realloc(void *pointer, unsigned int old_s, unsigned int new_s)
{
void *newptr;
if (pointer == NULL)
return (malloc(new_s));
if (new_s == 0)
{
free(pointer);
return (NULL);
}
if (new_s == old_s)
return (pointer);
newptr = malloc(new_s);
if (newptr == NULL)
return (NULL);
if (new_s < old_s)
_memcpy(newptr, pointer, new_s);
else
_memcpy(newptr, pointer, old_s);
free(pointer);
return (newptr);
}
/**
* _reallocdp - Reallocates a memory block of a double pointer.
* @pointer: Double pointer to the memory previously allocated.
* @old_s: Size, in bytes, of the allocated space for pointer.
* @new_s: New size, in bytes, of the reallocated memory block.
*
* Return: Pointer to the reallocated memory block (pointer).
*/
char **_reallocdp(char **pointer, unsigned int old_s, unsigned int new_s)
{
char **newptr;
unsigned int i;
if (pointer == NULL)
return (malloc(sizeof(char *) * new_s));
if (new_s == old_s)
return (pointer);
newptr = malloc(sizeof(char *) * new_s);
if (newptr == NULL)
return (NULL);
for (i = 0; i < old_s; i++)
newptr[i] = pointer[i];
free(pointer);
return (newptr);
}