forked from Hassanyoung1/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions.c
122 lines (105 loc) · 1.78 KB
/
helper_functions.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
112
113
114
115
116
117
118
119
120
121
122
#include "shell.h"
/**
* _strlen - Returns the length of a string
*
* @str: String integer given to the function
* Return: Returns number of strings
*/
int _strlen(char *str)
{
int count = 0;
while (str[count] != '\0')
{
count++;
}
return (count);
}
/**
* stringcmp - compares 2 strings
*
* @s1: First string to be compared
* @s2: Second string to be compared
* Return: Returns value of comparison (int)
*/
int stringcmp(char *s1, char *s2)
{
int i;
for (i = 0; (s1[i] != '\0' && s2[i] != '\0'); i++)
{
if (s1[i] != s2[i])
{
return (s1[i] - s2[i]);
}
}
return (s1[i] - s2[i]);
}
/**
* _strdup - returns a pointer to copy of given string
*
* @str: Given string
* Return: pointer to copy of string on succes, NULL otherwise
*/
char *_strdup(char *str)
{
char *ptr;
int i, j;
if (str == NULL)
return (NULL);
for (j = 0; str[j] != '\0'; j++)
{
}
ptr = malloc(sizeof(char) * (j + 1));
if (ptr == NULL)
return (NULL);
for (i = 0; str[i] != '\0'; i++)
{
ptr[i] = str[i];
}
ptr[i] = '\0';
return (ptr);
}
/**
* _strcat - Concatenates 2 strings
*
* @src: source to be concatenated from
* @dest: To be concatenated to
* Return: Returns concatenated string
*/
char *_strcat(char *dest, char *src)
{
int i;
int j;
for (i = 0; dest[i] != '\0'; i++)
;
for (j = 0; src[j] != '\0'; j++)
{
dest[i] = src[j];
i++;
}
dest[i] = '\0';
return (dest);
}
/**
* *_strcpy - copies string pointed by SRC to buffer pointed by DEST
*
* @src: points to string to be copied
* @dest: points to destination buffer
* Return: Returns pointer to DEST
*/
char *_strcpy(char *dest, char *src)
{
int i;
if (dest != NULL)
{
for (i = 0; src[i] != '\0'; i++)
{
dest[i] = src[i];
}
dest[i] = '\0';
return (dest);
}
else
{
return (NULL);
}
}