-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhelper.c
103 lines (86 loc) · 1.69 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
#include "shell.h"
/**
* tokenizer - tokenizes input and stores it into an array
*@input_string: input to be parsed
*@delim: delimiter to be used, needs to be one character string
*
*Return: array of tokens
*/
char **tokenizer(char *input_string, char *delim)
{
int num_delim = 0;
char **av = NULL;
char *token = NULL;
char *save_ptr = NULL;
token = _strtok_r(input_string, delim, &save_ptr);
while (token != NULL)
{
av = _realloc(av, sizeof(*av) * num_delim, sizeof(*av) * (num_delim + 1));
av[num_delim] = token;
token = _strtok_r(NULL, delim, &save_ptr);
num_delim++;
}
av = _realloc(av, sizeof(*av) * num_delim, sizeof(*av) * (num_delim + 1));
av[num_delim] = NULL;
return (av);
}
/**
*print - prints a string to stdout
*@string: string to be printed
*@stream: stream to print out to
*
*Return: void, return nothing
*/
void print(char *string, int stream)
{
int i = 0;
for (; string[i] != '\0'; i++)
write(stream, &string[i], 1);
}
/**
*remove_newline - removes new line from a string
*@str: string to be used
*
*
*Return: void
*/
void remove_newline(char *str)
{
int i = 0;
while (str[i] != '\0')
{
if (str[i] == '\n')
break;
i++;
}
str[i] = '\0';
}
/**
*_strcpy - copies a string to another buffer
*@source: source to copy from
*@dest: destination to copy to
*
* Return: void
*/
void _strcpy(char *source, char *dest)
{
int i = 0;
for (; source[i] != '\0'; i++)
dest[i] = source[i];
dest[i] = '\0';
}
/**
*_strlen - counts string length
*@string: string to be counted
*
* Return: length of the string
*/
int _strlen(char *string)
{
int len = 0;
if (string == NULL)
return (len);
for (; string[len] != '\0'; len++)
;
return (len);
}