-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell_helper.c
134 lines (119 loc) · 2.15 KB
/
shell_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
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
#include "holberton.h"
/**
* _strlen - returns length of str s
* @s: string to be measured
*
* Return: length of s
*/
int _strlen(char *s)
{
int count = 0;
while (*(s + count) != '\0')
{
count++;
}
return (count);
}
/**
* _strcmp - compares s1 and s2
* @s1: one of the strings in question
* @s2: one of the strigns in question
*
* Return: the difference between the strings
*/
int _strcmp(char *s1, char *s2)
{
int i;
for (i = 0 ; s1[i] != '\0' ; i++)
{
if (s2[i] == '\0')
return (s1[i]);
if (s2[i] != s1[i])
return (s1[i] - s2[i]);
}
if (s2[i] != '\0')
return (0 - s2[i]);
return (0);
}
/**
* _strcat - Concatanates dest and src
* @dest: One of the strings in question
* @src: One of the strings in question
*
* Return: the concatanated string
*/
char *_strcat(char *dest, char *src)
{
int len, i;
len = _strlen(dest);
for (i = 0 ; *(src + i) != '\0' ; i++)
{
*(dest + (len + i)) = *(src + i);
}
return (dest);
}
/**
* int_arg - integer handling
* @input: Input int
*
* Return: stringified int
*/
char *int_arg(int input)
{
char *output;
int len = 1, store = input, store2 = input;
if (input == 0)
{
output = malloc(sizeof(char) * 2);
output[0] = '0';
output[1] = '\0';
return (output);
}
if (input < 0)
len += 1;
for ( ; store /= 10 ; len++)
;
output = malloc(sizeof(char) * (len + 1));
if (output == NULL)
exit(-1);
output[len] = '\0';
len--;
while (store2)
{
if (store2 < 0)
output[len] = ((store2 % 10) * -1) + '0';
else
output[len] = (store2 % 10) + '0';
store2 /= 10;
len--;
}
if (input < 0)
output[0] = '-';
return (output);
}
/**
* _strspn - counts bytes of s which consists entirely of bytes in accept
* @s: string to be checked for consecutive acceptable characters
* @accept: acceptable characters
*
* Return: number of consecutive bytes of s which consist of bytes of accept
*/
unsigned int _strspn(char *s, const char *accept)
{
int i, j;
unsigned int bytes = 0;
for (i = 0; s[i] != '\0' ; i++)
{
for (j = 0; accept[j] != '\0'; j++)
{
if (s[i] == accept[j])
{
bytes++;
break;
}
}
if (accept[j] == '\0')
break;
}
return (bytes);
}