-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss-utils.c
79 lines (69 loc) · 1.47 KB
/
ss-utils.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
#include "shell.h"
/**
* from_terminal - returns true if shell is interactive mode
* @info: struct address
*
* Return: 1 if from_terminal mode else 0
*/
int from_terminal(info_s *info)
{
return (isatty(STDIN_FILENO) && info->fd_read <= 2);
}
/**
* _isalpha - checks for alphabetic character
* @c: The character to input
* Return: 1 if c is alphabetic else 0 if not
*/
int _isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
else
return (0);
}
/**
* _atoi - converts a string to an integer
* @s: the string to be converted
* Return: 0 if no numbers in string else converted integer
*/
int _atoi(char *s)
{
int i, sign = 1, flag = 0, output;
unsigned int result = 0;
for (i = 0; s[i] != '\0' && flag != 2; i++)
{
if (s[i] == '-')
sign *= -1;
if (s[i] >= '0' && s[i] <= '9')
{
flag = 1;
result *= 10;
result += (s[i] - '0');
}
else if (flag == 1)
flag = 2;
}
if (sign == -1)
output = -result;
else
output = result;
return (output);
}
/**
* is_delimiter - checks if a character is a delimiter
* @c: Character to be checked.
* @delimiters: String of charaters to be compated.
*
* This function compares a character to one or more of delimiters
* to see if the character matches any of the delimiters.
*
* Return: True if it is a delimiter, else false.
*/
bool is_delimiter(char c, char *delimiters)
{
int s;
for (s = 0; delimiters[s]; s++)
if (c == delimiters[s])
return (true);
return (false);
}