-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.c
More file actions
137 lines (124 loc) · 2.02 KB
/
Copy pathstring.c
File metadata and controls
137 lines (124 loc) · 2.02 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
#include "main.h"
/**
* _strdup - function that returns a pointer to a newly
* allocated space in memory, which contains a copy of
* the string given as a parameter.
* @str: string to be copied
* Return: copy of the string passed.
*/
char *_strdup(const char *str)
{
char *s;
if (str == NULL)
{
s = NULL;
} else
{
int size = _strlen(str);
int i;
s = malloc(sizeof(char) * size + 1);
if (s != NULL)
{
for (i = 0; i <= size; i++)
{
s[i] = str[i];
}
}
}
return (s);
}
/**
* _strcmp - custom string compare function
* @s1: string 1
* @s2: string 2
* Return: returns the difference between
* mismatched characters, 0 if none
*/
int _strcmp(const char *s1, const char *s2)
{
if (s1 != NULL || s2 != NULL)
{
while (*s1 && *s1 == *s2)
{
s1++;
s2++;
}
return ((unsigned char)*s1 - (unsigned char)*s2);
}
return (-1);
}
/**
* _strlen - Function that returns the length of a string
*
* @s: string to be read
* Return: int
*/
int _strlen(const char *s)
{
int len = 0;
if (s != NULL)
{
while (s[len] != '\0')
len++;
}
return (len);
}
/**
* _strtok - Custom _strtok function that
* tokenizes a string
* @str: String to be tokenized
* @delim: Delimiter character
* Return: Pointer token
*/
char *_strtok(char *str, const char *delim)
{
static char *t, *s;
unsigned int a;
if (str != NULL)
s = str;
t = s;
if (t == NULL)
return (NULL);
for (a = 0; t[a] != '\0'; a++)
{
if (check_delim(t[a], delim) == 0)
break;
}
if (s[a] == '\0' || s[a] == '#')
{
s = NULL;
return (NULL);
}
t = s + a;
s = t;
for (a = 0; s[a] != '\0'; a++)
{
if (check_delim(s[a], delim) == 1)
break;
}
if (s[a] == '\0')
s = NULL;
else
{
s[a] = '\0';
s = s + a + 1;
if (*s == '\0')
s = NULL;
}
return (t);
}
/**
* output - function that prints out a string
* @s: string to be printed out
* Return: length of char printed on success,
* -1 on failure
*/
int output(const char *s)
{
int count = -1;
if (s != NULL)
{
count = write(1, s, _strlen(s));
}
return (count);
}