-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandling.c
131 lines (113 loc) · 1.94 KB
/
handling.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
121
122
123
124
125
126
127
128
129
130
131
#include "main.h"
/**
* split_line - split a line into strings separated
*
* @line: the line to split
* @argv: the variable where the strings will be stored
*
* Return: void
*/
void split_line(char *line, char **argv)
{
int i = 0;
argv[i] = strtok(line, " \n");
while (argv[i] != NULL)
{
i++;
argv[i] = strtok(NULL, "\n");
}
argv[i] = NULL;
}
/**
* print_string - Print string
* @format: The string to print
*
* Return: The length of the output
*/
int print_string(const char *format)
{
int len = 0;
if (format == NULL)
return (write(1, "", 1));
while (format[len])
len++;
return (write(1, format, len));
}
/**
* _strncmp - compares two strings of n characters
* @s1: char*
* @s2: char*
* @n: number
* Return: 0 (s1 == s2 || c == n) -1 otherwise
*/
int _strncmp(char *s1, char *s2, int n)
{
int c = 0;
while (((s1[c] != '\0') && (s2[c] != '\0')) && s1[c] == s2[c])
{
c++;
}
if (c == n)
return (0);
else if (s1[c] == s2[c])
return (0);
else
return (-1);
}
/**
* _strdup - return a copy of the string given as a parameter
* @str: the string given
*
* Return: a pointer
*/
char *_strdup(char *str)
{
int n = 0, i;
char *buffer;
if (str == NULL)
return (NULL);
while (str[n] != '\0')
{
n++;
}
buffer = (char *)malloc((n * sizeof(char)) + 1);
if (buffer == NULL)
return (NULL);
for (i = 0; i < n; i++)
{
buffer[i] = str[i];
}
buffer[n] = '\0';
return (buffer);
}
char *_strtok(char *buffer, char *delim)
{
static char *next_token = NULL;
int k;
char *buf_cpy;
if (buffer != NULL)
next_token = buffer;
while (*next_token)
{
while (*delim)
{
if (*next_token == *delim)
break;
delim++;
}
if (*delim == '\0')
break;
next_token++;
}
buf_cpy = buffer;
if (buf_cpy[0] == '\0')
return (NULL);
while (*next_token != '\0' && strchr(delim, *next_token) == NULL)
next_token++;
if (*next_token != '\0')
{
*next_token = '\0';
next_token++;
}
return (buf_cpy);
}