-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_modify.c
More file actions
122 lines (104 loc) · 2.64 KB
/
Copy pathstr_modify.c
File metadata and controls
122 lines (104 loc) · 2.64 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
#include "strings.h"
/**
* overlaps_greater_indices - checks if a block of memory overlaps
* another on its higher indices.
* @mem1: potentially overlapping memory block..
* @mem2: other memory block.
* @bytes: size of the memory blocks.
*
* Return: 1 if mem1 overlaps mem2's higher indices, 0 otherwise.
*/
static unsigned char overlaps_greater_indices(
const char *const mem1, const char *const mem2, const intmax_t bytes)
{
return (mem1 == mem2 || (mem1 > mem2 && mem1 < (mem2 + bytes)));
}
/**
* _memcpy - copies `n` bytes from one memory block to another.
* @dest: the destination memory.
* @src: source memory block.
* @n: number of bytes to copy.
*
* Return: returns pointer to `dest`.
*/
void *_memcpy(void *const dest, const void *const src, const intmax_t n)
{
const char *s = src;
char *d = dest;
intmax_t i;
if (!d || !s || n <= 0)
return (d);
if (overlaps_greater_indices(d, s, n) == 1)
{
for (i = n; i > 0; --i)
d[i - 1] = s[i - 1];
}
else
{
for (i = 0; i < n; ++i)
d[i] = s[i];
}
return (d);
}
/**
* _memset - fills a memory block with a constant byte.
* @mem_block: pointer to the memory block.
* @byte: byte to fill block with.
* @size: size in bytes to fill.
*
* Return: pointer to the modified memory block.
*/
void *_memset(void *const mem_block, const char byte, intmax_t size)
{
char *const mem = mem_block;
if (!mem || size <= 0)
return (mem);
while (size > 0)
{
--size;
mem[size] = byte;
}
return (mem);
}
/**
* _strdup - duplicate a string in memory.
* @str: the string to duplicate.
* @size: size of the string.
*
* Return: pointer to the duplicated string, NULL on error.
*/
char *_strdup(const char *const str, intmax_t size)
{
char *new_str = NULL;
if (!str || size < 1)
return (NULL);
new_str = _memcpy(_malloc(sizeof(*str) * size + 1), str, size);
new_str[size] = '\0';
return (new_str);
}
/**
* _strncat - concatenates two strings by appending at most `n` bytes of `s`
* to the end of `dest` and then terminates with a '\0'.
* @dest: pointer to a memory block with the first string and enough space for
* atleast `n` + 1 bytes of the second.
* @s: the second string.
* @n: maximum number of bytes from `s` to append.
*
* Return: pointer to the concatenated strings, NULL if both pointers are NULL.
*/
char *_strncat(char *dest, const char *const s, intmax_t n)
{
intmax_t dest_len = _strlen(dest);
intmax_t s_len = _strlen(s);
if (!dest && !s)
return (NULL);
if (dest_len < 1)
return ((char *)s);
if (s_len < 1 || n < 1)
return (dest);
if (s_len > n)
s_len = n;
dest = _memcpy(dest + dest_len, s, s_len);
dest[dest_len + s_len] = '\0';
return (dest);
}