-
Notifications
You must be signed in to change notification settings - Fork 6
/
more_utilities.c
111 lines (94 loc) · 1.65 KB
/
more_utilities.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
#include "shell.h"
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
/**
* _puts - Prints a string
* @str: The string to print
*
* Return: Nothing to returns
*/
void _puts(char *str)
{
while (*str)
{
_putchar(*str);
str++;
}
_putchar('\n');
}
/**
* _strncmp - Compares at most the first n bytes of str1 and str2
* @s1: The first string
* @s2: The second string
* @len: The limit bytes of comparison
*
* Return: int value
*/
int _strncmp(const char *s1, const char *s2, size_t len)
{
unsigned int pos = 0;
int diff = 0;
while (pos < len)
{
if (s1[pos] == s2[pos])
{
pos++;
continue;
}
else
{
diff = s1[pos] - s2[pos];
break;
}
pos++;
}
return (diff);
}
/**
* _realloc - Resize the ptr that was previously allocated
* @ptr: The pointer previously allocated
* @old_size: The old size of the pointer
* @new_size: The new size of the new pointer
*
* Return: A new pointer to the memory block allocated with the new size
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *nptr;
unsigned int i;
if (new_size == old_size)
return (ptr);
if (ptr == NULL)
{
nptr = malloc(new_size);
if (nptr == NULL)
return (NULL);
return (nptr);
}
else
{
if (new_size == 0)
{
free(ptr);
return (NULL);
}
}
nptr = malloc(new_size);
if (nptr == NULL)
return (NULL);
for (i = 0; i < old_size && i < new_size; i++)
{
nptr[i] = ((char *) ptr)[i];
}
free(ptr);
return (nptr);
}