forked from glyif/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib_funcs.c
109 lines (94 loc) · 1.63 KB
/
stdlib_funcs.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
#include "header.h"
/**
* _isspace - checks if input char is a space character
* @c: input character
*
* Return: 1 on success, 0 on failure
*/
char _isspace(char c)
{
int i;
switch (c)
{
case ' ': case '\t': case '\n':
case '\v': case '\f': case '\r':
i = 1;
break;
default:
i = 0;
break;
}
return (i);
}
/**
* _atoi - returns integer values from string
* @s: input string
*
* Return: will return integer
*/
int _atoi(char *s)
{
int result = 0, sign = 0, c;
for (c = 0; s[c] != '\0'; c++)
{
if (s[c] == '-')
sign++;
if (s[c] > 47 && s[c] < 58)
{
while (s[c] > 47 && s[c] < 58)
result = result * 10 - (s[c++] - 48);
break;
}
}
return (result *= sign % 2 == 0 ? -1 : 1);
}
/**
* _perror - custom perror
* @string: input error string to write to stderr
*
* Return: void
*/
void _perror(char *string)
{
unsigned int len;
len = _strlen(string);
write(STDERR_FILENO, string, len);
}
/**
* _memmove - shifting an array
* @dest: dest
* @src: source
* @n: bytes to move
*/
void _memmove(void *dest, void *src, size_t n)
{
char *copy_source, *copy_dest, *temp;
size_t i;
copy_source = (char *)src;
copy_dest = (char *)dest;
temp = malloc(sizeof(char) * 1024);
for (i = 0; i < n; i++)
temp[i] = copy_source[i];
for (i = 0; i < n; i++)
copy_dest[i] = temp[i];
free(temp);
}
/**
* is_uint - checks if input string is unsigned int
* @num: the input number
* Return: TRUE or FALSE
*/
int is_uint(char *num)
{
int i = 0;
while (num[i])
{
if (num[i] > 47 && num[i] < 58)
i++;
else if (num[i] == 45 && i == 0)
return (FALSE);
else
return (FALSE);
}
return (TRUE);
}