forked from Ana-Morales/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaux_func.c
More file actions
138 lines (131 loc) · 2.03 KB
/
aux_func.c
File metadata and controls
138 lines (131 loc) · 2.03 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "holberton.h"
/**
* str_concat - concatenates two strings
* @s1: string 1
* @s2: string 2 to concatenate to s1
*
* Return: Pointer to newly allocated space in mem with s1 + s2
*/
char *str_concat(char *s1, char *s2)
{
char *conc;
unsigned int len1 = 0, len2 = 0, i;
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
while (s1[len1] != '\0')
{
len1++;
}
while (s2[len2] != '\0')
{
len2++;
}
conc = malloc(sizeof(char) * (len1 + (len2 + 1)));
if (conc == NULL)
return (NULL);
i = 0;
while (i < len1)
{
conc[i] = s1[i];
i++;
}
i = 0;
while (i <= len2)
{
conc[len1] = s2[i];
i++;
len1++;
}
return (conc);
free(conc);
}
/**
* _strlen - returns the length of a string
* @s: string to get its length
* Return: the length of the string
*/
int _strlen(const char *s)
{
int i, len;
i = 0;
while (s[i] != '\0')
{
len = i + 1;
i++;
}
return (len);
}
/**
* _strcmp - compares two strings.
* @s1: string 1
* @s2: string 2
* Return: 0 if s1 == s2, pos if s1 < s2, neg if s1 > s2
*/
int _strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
s1++;
s2++;
if (*s1 == '\0')
return (0);
}
return (*s1 - *s2);
}
/**
* _strdup - returns a pointer to a newly allocated space in memory
* which contains a copy of the string given as a parameter.
* @str: string to copy
*
* Return: Pointer to newly allocated space in memory
*/
char *_strdup(char *str)
{
char *s;
unsigned int i, len;
if (str == NULL)
return (NULL);
len = 0;
while (str[len] != '\0')
{
len++;
}
s = malloc(sizeof(char) * (len + 1));
if (s == NULL)
return (NULL);
i = 0;
while (len > 0)
{
s[i] = str[i];
i++;
len--;
}
s[i] = '\0';
return (s);
}
/**
*_strchr - a function that fills memory with a constant byte.
*@s: string to detect
*@c: character to detect on s
*
*Return: a pointer to the memory area dest.
*/
int _strchr(char *s, char c)
{
int cont = 1;
int i = 0;
while (s[i] != '\0')
{
cont += 1;
i++;
}
while (cont--)
{
s++;
if (*s == c)
return (1);
}
return (0);
}