-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring-funcs2.c
139 lines (121 loc) · 2.1 KB
/
string-funcs2.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
#include "shell.h"
/**
* _puts - print a buffer to stdout.
* @buffer: buffer passed
*/
void _puts(char *buffer)
{
int size;
size = _strlen(buffer);
write(STDOUT_FILENO, buffer, size);
}
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*/
void _putchar(char c)
{
write(1, &c, 1);
}
/**
* _atoi - Convert a string to integer.
* @s: char array string
* Description: Adjusted to only take valid positive numbers.
* Return: first integer found in string
*/
int _atoi(char *s)
{
int i;
int value, sign;
/* return -1 (false) if not valid number */
if (s[0] > '9' || s[0] < '0')
return -1;
value = 0;
sign = -1;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] == '-')
sign *= -1;
if (s[i] >= '0' && s[i] <= '9')
{
if (value < 0)
value = (value * 10) - (s[i] - '0');
else
value = (s[i] - '0') * -1;
if (s[i + 1] < '0' || s[i + 1] > '9')
break;
}
}
if (sign < 0)
value *= -1;
return (value);
}
/**
* _puts_num - convert integer to string
* @n: number to convert
*/
void _puts_num(int n)
{
int power;
int hold;
power = 1;
if (n < 0)
{
n *= -1;
_putchar('-');
}
hold = n;
while (hold > 9)
{
power *= 10;
hold /= 10;
}
while (power > 0)
{
if (power > 9)
{
_putchar((n / power % 10) + '0');
power /= 10;
}
if (power == 1)
{
_putchar(n % 10 + '0');
power = 0;
}
}
}
/**
* parse_path - used to parse path variable, but can perform basic tokenization
* @path: string passed to parse
* @dlimtr: delimiter to split string by
* Return: an array of strings
*/
char **parse_path(char *path, char dlimtr)
{
int i, j, hold, indx;
char **paths, *tmp;
if (*path == '\0')
return (NULL);
paths = _malloc(sizeof(char *) * BUFRSIZE);
if (paths == NULL)
return (NULL);
indx = 0;
for (i = 0, hold = 0; path[i]; i++)
{
if (path[i] == dlimtr)
{
tmp = _malloc(sizeof(char) * (i - hold));
if (tmp == NULL)
return (NULL);
for (j = 0; hold < i; j++, hold++)
{
if (path[hold] == dlimtr)
hold++;
tmp[j] = path[hold];
}
tmp[j++] = '\0';
paths[indx++] = tmp;
}
}
return (paths);
}