forked from svilladaniel/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_functions.c
More file actions
112 lines (108 loc) · 1.89 KB
/
str_functions.c
File metadata and controls
112 lines (108 loc) · 1.89 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
#include "holberton.h"
/**
* _strlen - find the length of a string
* @s: entry string
* Return: number of bytes
**/
int _strlen(char *s)
{
int i;
for (i = 0; s[i]; i++)
{
}
return (i);
}
/**
* _strcat - concatenate directories with the 1st position of the array
* @directory: directories
* @slash: /
* @arg: array of commands.
* Return: str
**/
char *_strcat(char *directory, char *slash, char *arg)
{
char *str = NULL;
int l1, l2, i, k;
l1 = _strlen(directory);
l2 = _strlen(arg);
str = malloc(sizeof(char) * (l1 + l2 + 2));
if (!str)
return (NULL);
for (i = 0; directory[i]; i++)
str[i] = directory[i];
str[i] = *slash;
k = i + 1;
for (i = 0; arg[i]; i++)
str[k + i] = arg[i];
str[k + i] = '\0';
return (str);
}
/**
* _strtok - breaks a string into a sequence
* @str: string
* @delim: specific character to break the string
* Return: NULL if fails or pointer to divided string.
*/
char *_strtok(char *str, char *delim)
{
int i = 0, j = 0, a = 0;
static char *tracker;
if (!delim || (!str && !tracker))
return (NULL);
str == NULL ? str = tracker : str;
for (; str[j] == ' ' ; j++)
{
if (str[j + 1] == '\0')
return (NULL);
}
for (i = j ; str[i] != '\0' ; i++)
{
if (str[i] == *delim)
{
a = 1;
break;
}
}
for (; str[i] != '\0' ; i++)
if (str[i] != delim[0])
break;
else if (str[i + 1] == '\0')
{
a = 0, str[i] = '\0';
break;
}
else
str[i] = '\0';
if (a == 0)
{
tracker = NULL;
return (str + j);
}
if (str + i)
tracker = str + i;
else
tracker = NULL;
return (str + j);
}
/**
* *_strcpy - copies the string pointed to another pointer
* @src: original pointer
* @dest: destination pointer
* Return: return a pointer char
*/
char *_strcpy(char *dest, char *src)
{
int a, b;
for (a = 0; src[a] != '\0' ; a++)
{
}
for (b = 0; b < a ; b++)
{
dest[b] = src[b];
}
for (; b <= a ; b++)
{
dest[b] = '\0';
}
return (dest);
}