-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_stdlib.c
More file actions
152 lines (125 loc) · 2.58 KB
/
c_stdlib.c
File metadata and controls
152 lines (125 loc) · 2.58 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "main.h"
/**
* _strtok - splits a string by some delimiter.
* @str: input string.
* @delim: delimiter.
*
* Return: string splited.
*/
char *_strtok(char *str, char *delim)
{
static char *next_token;
char *token_start;
if (str != NULL)
next_token = str;
if (!next_token || *next_token == '\0')
return (NULL);
while (*next_token != '\0' && is_delimiter(*next_token, delim))
next_token++;
if (*next_token == '\0')
return (NULL);
token_start = next_token;
while (*next_token != '\0' && !is_delimiter(*next_token, delim))
next_token++;
if (*next_token != '\0')
{
*next_token = '\0';
next_token++;
}
return (token_start);
}
/**
* _memcpy - copy a fixed byte
* @dest: pointer to copy to
* @src: poiter to copy from
* @n: size to copy
* Return: dest pointer
*/
char *_memcpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
for (i = 0; i < n; i++)
{
dest[i] = src[i];
}
return (dest);
}
/**
* fill_an_array - rfill an array witha fixed byte
* @a: new pointer to array
* @len: len of array
* @el: pointer to array
*
* Return: new pointer to array
*/
void *fill_an_array(void *a, int el, unsigned int len)
{
char *p = a;
unsigned int i = 0;
while (i < len)
{
*p = el;
p++;
i++;
}
return (a);
}
/**
* _realloc - reallocates a memory block of a double pointer.
* @ptr: double pointer to the memory previously allocated.
* @old_size: size, in bytes, of the allocated space of ptr.
* @new_size: new size, in bytes, of the new memory block.
*
* Return: new pointer to dest
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *result;
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr)
{
free(ptr);
return (NULL);
}
result = malloc(new_size);
if (result == NULL)
return (NULL);
if (ptr == NULL)
{
fill_an_array(result, '\0', new_size);
free(ptr);
}
else
{
_memcpy(result, ptr, old_size);
free(ptr);
}
return (result);
}
/**
* _reallocdp - reallocates a memory block of a double pointer.
* @ptr: double pointer to the memory previously allocated.
* @src: size, in bytes, of the allocated space of ptr.
* @dest: new size, in bytes, of the new memory block.
*
* Return: new pointer to dest
*/
char **_reallocdp(char **ptr, size_t src, size_t dest)
{
char **newptr = (char **)malloc(dest * sizeof(char *));
size_t i;
if (ptr == NULL)
return (newptr); /* line 53 */
if (ptr != NULL && dest == 0)
{
free(ptr);
return (NULL);
}
if (newptr == NULL)
return (NULL);
for (i = 0; i < src; i++)
newptr[i] = ptr[i]; /* line 66 */
free(ptr);
return (newptr);
}