-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils_funcs3.c
124 lines (106 loc) · 2.2 KB
/
utils_funcs3.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
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
#include "shell.h"
/**
* _atoi - Converts a string to an integer.
* @str: The string to convert.
*
* Return: The integer value of the string.
*/
int _atoi(const char *str)
{
int i, sign;
unsigned int num;
i = 0;
sign = 1;
num = 0;
while (str[i] != '\0')
{
if (str[i] == '-')
sign *= -1;
else if (str[i] >= '0' && str[i] <= '9')
num = (num * 10) + (str[i] - '0');
else
break;
i++;
}
return (num * sign);
}
/**
**_memset - fills memory with a constant byte
*@s: the pointer to the memory area
*@b: the byte to fill *s with
*@n: the amount of bytes to be filled
*Return: (s) 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);
}
/**
* _memcpy - function that copies memory area
*
* @dest: buffer where we will copy to
* @src: what we are to copy
* @n: n bytes of @src
*
* Return: Always 0 (Success)
*/
char *_memcpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
for (i = 0; i < n; i++)
dest[i] = src[i];
return (dest);
}
/**
* _realloc - reallocates a block of memory
* @ptr: pointer to previous malloc'ated block
* @old_size: byte size of previous block
* @new_size: byte size of new block
*
* Return: pointer to da ol'block nameen.
*/
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);
}
/**
* _calloc - a function that allocates
* memory for an array using malloc
*
* It is basically the equivalent to
* malloc followed by memset
*
* @nmemb: size of array
* @size: size of each element
*
* Return: pointer with new allocated memory
* or NULL if it fails
*/
void *_calloc(unsigned int nmemb, unsigned int size)
{
char *p;
if (nmemb == 0 || size == 0)
return (NULL);
p = malloc(nmemb * size);
if (p == NULL)
return (NULL);
_memset(p, 0, nmemb * size);
return (p);
}