-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_helper_functions2.c
147 lines (139 loc) · 2.79 KB
/
string_helper_functions2.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "main.h"
/**
* split - function that split string into arguments
* @string: the string to split
* @breaks: array of breaks characters
* Return: array of arguments
*/
char **split(char *string, char *breaks)
{
char *argument;
char **arguments;
int position = 0, size = _strlen(string);
arguments = malloc((size + 1) * sizeof(char *));
if (!arguments)
{
perror("Error:failed to allocate memory");
exit(EXIT_FAILURE);
}
/*start split the string*/
argument = strtok(string, breaks);
while (argument != NULL)
{
arguments[position++] = argument;
argument = strtok(NULL, breaks);
/* NULL here meaning that continue in the same string*/
}
/*the final argument should be NULL for execve function*/
arguments[position] = NULL;
return (arguments);
}
/**
* _atoi - converts string to int
* @s: string to convert
* Description: converts string to int considering all negatives
* Return: integer
**/
int _atoi(char *s)
{
int i, val, sign;
val = 0;
sign = 1;
for (i = 0; s[i] != '\0' && !(s[i] >= '0' && s[i] <= '9'); i++)
{
if (s[i] == '-')
sign = sign * -1;
}
for (i = 0; s[i] != 0; i++)
{
if (s[i] >= '0' && s[i] <= '9')
val = val * 10 + sign * (s[i] - '0');
if (val != 0 && !(s[i] >= '0' && s[i] <= '9'))
return (val);
}
return (val);
}
/**
* cleanStr - clean string from new line and comment
* @str: string from user or pipe to clean
* Return: NULL if new line or string without comment
*/
char *cleanStr(char *str)
{
/* handle new line in begining */
/* handle # in begining */
if (*str == '\n' || *str == '#')
{
return (NULL);
}
str[_strlen(str) - 1] = '\0';
remove_comment(str);
return (str);
}
/**
* string_realloc - reallocate string
* @str: pointer of array of character to reallocate
* @new_size: number of bytes to allocate
*/
void string_realloc(char *str, size_t new_size)
{
char *new_str = NULL;
if (str == NULL)
{
return;
}
new_str = malloc(new_size);
if (new_str == NULL)
{
return;
}
new_str = _strcopy(str);
free(str);
str = new_str;
}
/**
* _getline - Implement the getline function
* @string: the string pointer to pass the characters to
* @string_size: number of bytes that readed
* Return: the number of bytes read
*/
ssize_t _getline(char **string, size_t *string_size)
{
size_t buffer_size = BUFSIZ, position = 0;
char *buffer = NULL;
char c;
/*handling error*/
if (string == NULL || string_size == NULL)
{
return (-1);
}
buffer = malloc(buffer_size);
if (buffer == NULL)
{
/* Memory allocation error*/
return (-1);
}
while (read(STDIN_FILENO, &c, 1) == 1)
{
if (position >= buffer_size - 1)
{
/*Double the buffer size if it's not enough*/
buffer_size *= 2;
string_realloc(buffer, buffer_size);
}
buffer[position++] = c;
if (c == '\n')
{
break;
}
if (position == 0 && c == EOF)
{
free(buffer);
return (-1);
}
}
buffer[position] = '\0';
*string = buffer;
*string_size = buffer_size;
return (position);
}