-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
152 lines (135 loc) · 2.6 KB
/
utils.c
File metadata and controls
152 lines (135 loc) · 2.6 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "hsh.h"
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/**
* split - splits a string using a delimiter and returns a
* malloced array pointer with the last value set to NULL.
*
* @str: string to split.
* @delim: elimiting character to use.
*
* Return: malloced array pointer with the last value set to NULL.
*/
char **split(char *str, char *delim)
{
char **arr = { NULL };
char *token;
int i = 0;
size_t len;
if (str == NULL)
return (arr);
/* Loop through the string */
while (1)
{
token = strtok(str, delim);
if (token == NULL || token[0] == '#')
break;
if (i == 0)
len = 0;
else
len = sizeof(char *) * (i + 1);
arr = _realloc(arr, len, sizeof(char *) * (i + 2));
arr[i] = _strdup(token);
str = NULL;
i++;
arr[i] = NULL;
}
return (arr);
}
/**
* _realloc - reallocates a memory block using malloc and free
*
* @ptr: pointer to memory previously allocated with
* a call to malloc
* @old_size: the size, in bytes, of the allocated space for ptr
* @new_size: the new size, in bytes of the new memory block
*
* Return: ponter to reallocated memory
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *p = NULL;
unsigned int i = 0;
if (old_size == new_size)
return (ptr);
if (ptr != NULL && new_size == 0)
{
free(ptr);
return (NULL);
}
p = malloc(new_size);
if (p != NULL && ptr != NULL)
{
for (i = 0; i < old_size; i++)
*((char *)p + i) = *((char *)ptr + i);
free(ptr);
}
return (p);
}
/**
* _atoi - convert a string to an integer.
*
* @str: string to convert
*
* Return: integer from string
*/
int _atoi(char *str)
{
int value = 0, sign = 1, i = 0;
if (str == NULL)
return (0);
while (str[i] != '\0')
{
if (str[i] == '-')
sign *= -1;
else if (str[i] >= '0' && str[i] <= '9')
{
if (value == 0)
value = str[i] - '0';
else
{
value *= 10;
value += str[i] - '0';
}
}
else
{
if (value == 0)
value = -1;
break;
}
i++;
}
return (value * sign);
}
/**
* to_string - convert integer to string
*
* @n: number to convert
*
* Return: malloced string pointer or NULL if n < 0
*/
char *to_string(int n)
{
char *str = NULL, ch;
int len = 0, i = 0;
str = malloc(sizeof(char));
/* using do..while loop to make sure 0 is returned */
do {
str = _realloc(str, sizeof(char) * (len + 1), sizeof(char) * (len + 2));
str[len] = '0' + n % 10;
n /= 10;
len++;
} while (n > 0);
str[len] = '\0';
for (i = 0; i < len / 2; i++)
{
ch = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = ch;
}
return (str);
}