-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions2.c
executable file
·79 lines (71 loc) · 1.35 KB
/
functions2.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
#include "main.h"
/**
* _memcpy - Copies a memory area.
* @dest: The destination memory area.
* @src: The source memory block.
* @n: The number of bytes to be copied.
*
* Return: A pointer to the array dest.
*/
char *_memcpy(char *dest, char *src, unsigned int n)
{
int size = n; /* To only accept possitive integers */
int idx = 0;
if (size > 0)
{
for (; idx < size; idx++)
{
dest[idx] = src[idx];
}
}
return (dest);
}
/**
* _strdup - duplicates a str in the heap memory.
* @s: Type char pointer str
* Return: duplicated str
*/
char *_strdup(char *s)
{
char *new;
size_t len;
len = _strlen(s);
new = malloc(sizeof(char) * (len + 1));
if (new == NULL)
return (NULL);
_memcpy(new, s, len + 1);
return (new);
}
/**
* trim_spaces - Removes leading and trailing whitespace from a string.
* @cmd: The string to be trimmed.
* Return: A pointer to the trimmed string.
*/
char *trim_spaces(char *cmd)
{
int i = 0, n;
/**Remove leading spaces*/
while (isspace(cmd[i]))
i++;
n = _strlen(cmd + i);
if (n == 0)
{
free(cmd);
return (NULL);
}
/**Remove trailing spaces*/
while (isspace(cmd[i + n - 1]))
n--;
cmd[i + n] = '\0';
return (cmd);
}
/**
* _getline - to read line
* @line_ptr: command read
* @n: size
* Return: num of chars read
*/
ssize_t _getline(char **line_ptr, size_t *n)
{
return (getline(line_ptr, n, stdin));
}