-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.c
More file actions
111 lines (97 loc) · 1.75 KB
/
Copy pathutility.c
File metadata and controls
111 lines (97 loc) · 1.75 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
#include "shell.h"
/**
* _strcat_dir - concatenates two strings with a slash for directories
*
* @dest: first string
* @src: second string
* Return: dest which will have concatenated string
*/
char *_strcat_dir(char *dest, char *src)
{
char *cat;
int i, k = 0;
cat = malloc(sizeof(char) * (_strlen(dest) + _strlen(src) + 2));
if (!cat)
return (NULL);
for (i = 0; i < _strlen(dest); i++)
*(cat + k++) = *(dest + i);
*(cat + k++) = '/';
for (i = 0; i < _strlen(src); i++)
*(cat + k++) = *(src + i);
*(cat + k) = '\0';
return (cat);
}
/**
* _strlen - return length of a string
* @s: char pointer for string to measure length
* Return: length of string, n
*/
int _strlen(char *s)
{
int n, i;
n = 0;
for (i = 0; *(s + i) != '\0'; i++)
n++;
return (n);
}
/**
* *_strcmp - compares two string
*
* @s1: first string
* @s2: second string
* Return: int difference between s1 and s2
*/
int _strcmp(char *s1, char *s2)
{
if (!s1 || !s2)
return (1);
while (*s1 || *s2)
{
if (*s1 - *s2 != 0)
return (*s1 - *s2);
if (*(s1))
s1++;
if (*(s2))
s2++;
}
return (0);
}
/**
* _atoi - return a num inside of a string
*
* @s: pointer for string to parse
* Return: int pulled from string, 0 if none
*/
int _atoi(char *s)
{
unsigned int num = 0;
int flag = -1, i = 0, neg = 1;
if (*s == '\0')
flag = 1;
for (i = 0; flag == -1; i++)
{
if ((*(s + i) >= '0') && (*(s + i) <= '9'))
{
if (num != 0)
num = ((num * 10) + (*(s + i) - '0'));
else
num = (*(s + i) - '0');
}
else
{
num = -1;
neg = 1;
break;
}
if (*(s + i + 1) == '\0')
flag = 1;
if (*(s + i) == '-')
neg *= -1;
if (num != 0)
{
if ((*(s + i + 1) < '0') || (*(s + i + 1) > '9'))
flag = 0;
}
}
return (neg * num);
}