This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
forked from roryfahy/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.c
120 lines (106 loc) · 1.93 KB
/
helper.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
#include "shell.h"
/**
* _strcpy - copy from src into dest
* @dest: string to copy into
* @src: string to copy
* Return: pointer to dest
*/
char *_strcpy(char *dest, const char *src)
{
char *dest_cpy = dest;
if (!dest || !src)
return (NULL);
for (; *src; src++, dest_cpy++)
*dest_cpy = *src;
*dest_cpy = '\0';
return (dest);
}
/**
* _strlen - find length of string
* @str: pointer to incoming string
* Return: string length
*/
size_t _strlen(char *str)
{
int i = 0;
while (str[i] != '\0')
i++;
return (i);
}
/**
* str_concat - concatenates two strings
* @s1: pointer to string 1
* @s2: pointer to string 2
* Return: pointer to malloc'd space containing concatenated string
*/
char *str_concat(char *s1, char *s2)
{
char *cat_str, *cpy1, *cpy2;
int count1 = 0;
int count2 = 0;
int i = 0;
int j;
int forward_slash = 0;
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
cpy1 = s1;
cpy2 = s2;
for (; *cpy1; cpy1++, count1++)
;
if (*(cpy1 - 1) != '/')
count1++, forward_slash = 1;
for (; *cpy2; cpy2++, count2++)
;
count2++;
cat_str = malloc(sizeof(*s1) * count1 + sizeof(*s2) * count2);
if (cat_str == NULL)
return (cat_str);
for (; s1[i]; i++)
cat_str[i] = s1[i];
if (forward_slash)
cat_str[i++] = '/';
for (j = 0; j < count2; i++, j++)
cat_str[i] = s2[j];
return (cat_str);
}
/**
* _strcmp - compares two strings
* @s1: points to string 1
* @s2: points to string 2
* Return: 0 on success, any other number on failure
*/
int _strcmp(char *s1, char *s2)
{
int x = 0;
for (; *s1 || *s2; s1++, s2++)
if (*s1 != *s2)
{
x = *s1 - *s2;
break;
}
return (x);
}
/**
* *_strcat - concatenates two strings
* @dest: points to address char string
* @src: points to address char string
*
* Return: pointer to char string dest
*/
char *_strcat(char *dest, char *src)
{
char *cptr = dest;
while (*cptr)
{
cptr++;
}
while (*src)
{
*cptr = *src;
cptr++;
src++;
}
return (dest);
}