forked from leinefran/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
129 lines (112 loc) · 2.19 KB
/
Copy pathstring.c
File metadata and controls
129 lines (112 loc) · 2.19 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
#include "shell.h"
/**
* stringdup - function to return pointer to string (duplicate string)
* @str: string to duplicate
* Return: pointer to string or NULL if failure to allocate
*/
char *stringdup(const char *str)
{
int i, j;
char *ptr;
if (str == NULL)
return (NULL);
for (i = 0; str[i] != '\0'; i++)
;
ptr = malloc(sizeof(char) * (i + 1));
if (!ptr)
return (NULL);
for (j = 0; j < i; j++)
ptr[j] = str[j];
return (ptr);
}
/**
* _strlen - This function returns the length of a string
* @s: Variable that contains a character array to be counted
* Return: Length of the string
*/
int _strlen(char *s)
{
int i;
for (i = 0; s[i] != '\0'; i++)
continue;
return (i);
}
/**
* str_comp - compare two strings
* @arr: array of string pointers
* @size: size of the array
* Return: 0 on success, -1 on failure
*/
int str_comp(char **arr, int size)
{
char *command[4] = {"exit", "env", ".", NULL};
char *ptr = arr[0];
int i = 0, j = 0, flag = 0;
if (size != 1)
return (-1);
if (!ptr)
return (1);
while (command[i] != NULL)
{
j = 0;
while (command[i][j] == ptr[j])
{
if (command[i][j] == '\0' || ptr[j] == '\0')
break;
j++;
}
if (command[i][j] == ptr[j])
return(flag);
i++;
flag++;
}
return (-1);
}
/**
* str_concat - concatenates two strings
* The returned pointer points to a newly allocated space in memory
* which contains the contents.
*
* @s1: string to be modified.
* @s2: string to be appended.
* @flag: integer flag for handling path or error function
* ls: linked_string (after concatenate).
*
* Return: char.
*/
char *str_concat(char *s1, char *s2, int flag)
{
int len1 = 0, len2 = 0, i, j;
char *ptr;
if (s1 == NULL)
{
s1 = malloc(sizeof(char));
if (s1 == NULL)
return (NULL);
*s1 = '\0';
}
if (s2 == NULL)
{
s2 = malloc(sizeof(char));
if (s2 == NULL)
return (NULL);
*s2 = '\0';
}
while (s1[len1] != '\0')
len1++;
while (s2[len2] != '\0')
len2++;
ptr = malloc(sizeof(char) * (len1 + len2 + 1 + flag));
if (ptr == NULL)
return (NULL);
for (i = 0 ; s1[i] != '\0' ; i++)
ptr[i] = s1[i];
if (flag == 1)
{
ptr[i] = '/';
i++;
}
for (j = 0 ; j < (len2 + 1) ; j++, i++)
ptr[i] = s2[j];
return (ptr);
}