-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.c
More file actions
124 lines (107 loc) · 1.93 KB
/
functions.c
File metadata and controls
124 lines (107 loc) · 1.93 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
#include "shell.h"
/**
* _strcat - A function that concatenates two strings
* @dest: The destination string
* @src: The source string
* Return: A pointer to the string
*/
char *_strcat(char *dest, char *src)
{
int dlen = 0;
int x = 0;
while (dest[dlen])
dlen++;
for (x = 0; src[x] != '\0'; x++)
{
dest[dlen] = src[x];
dlen++;
}
dest[dlen] = '\0';
return (dest);
}
/**
* _strncmp - A function that compares the first character of two strings
* @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 cur_pos = 0;
int diff = 0;
while (cur_pos < len)
{
if (s1[cur_pos] == s2[cur_pos])
{
cur_pos++;
continue;
}
else
{
diff = s1[cur_pos] - s2[cur_pos];
break;
}
cur_pos++;
}
return (diff);
}
/**
* _putchar - A function that writes the character c to stdout
* @c: The character to print
* Return: On success 1.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
/**
* _puts - A function that prints a string
* @str: The string to print
* Return: Nothing
*/
void _puts(char *str)
{
for (; *str; str++)
_putchar(*str);
_putchar('\n');
}
/**
* _realloc - A function that realloctes memory for a pointer
* @ptr: the previous pointer
* @init_size: initial size of the pointer
* @new_size: The new size of the new pointer
* Return: A new pointer to the memory
*/
void *_realloc(void *ptr, unsigned int init_size, unsigned int new_size)
{
char *nptr;
unsigned int i = 0;
if (new_size == init_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);
while (i < init_size && i < new_size)
{
nptr[i] = ((char *)ptr)[i];
i++;
}
free(ptr);
return (nptr);
}