-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities1.c
99 lines (83 loc) · 1.46 KB
/
utilities1.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
#include "shell.h"
/**
* _strdup - Creates a duplicate of a string.
* @str: The input string.
*
* Return: The pointer to the duplicated string, or NULL if allocation fails.
*/
char *_strdup(char *str)
{
char *dup = NULL;
size_t len = _strlen(str) + 1;
dup = malloc(len);
if (dup == NULL)
return (NULL);
return (_memcpy(dup, str, len));
}
/**
* _strcat - Concatenates two strings.
* @dest: The destination string.
* @src: The source string.
*
* Return: A pointer to the resulting string.
*/
char *_strcat(char *dest, char *src)
{
int i = 0, c = 0;
while (dest[i] != '\0')
i++;
while (src[c] != '\0')
{
dest[i] = src[c];
i++;
c++;
}
dest[i] = '\0';
return (dest);
}
/**
* _strlen - Calculates the length of a string.
* @s: The input string.
*
* Return: The length of the string.
*/
int _strlen(const char *s)
{
int c = 0;
for (c = 0; *s != '\0'; s++)
c++;
return (c);
}
/**
* print_env - Prints the environment variables.
*
* Return: 0 on success, -1 on failure.
*/
int print_env(void)
{
int i;
if (!environ)
return (-1);
for (i = 0; environ[i] != NULL; i++)
{
write(STDOUT_FILENO, environ[i], _strlen(environ[i]));
write(STDOUT_FILENO, "\n", 1);
}
return (0);
}
/**
* _strcmp - Compares two strings.
* @s1: The first string.
* @s2: The second string.
*
* Return: The difference between strings.
*/
int _strcmp(const char *s1, char *s2)
{
while (*s1 && (*s1 == *s2))
{
s1++;
s2++;
}
return (*s1 - *s2);
}